From b0aef80f6c6bc3fa770d4df6963a39501cc6de57 Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 6 Oct 2026 15:28:27 +0800 Subject: [PATCH] claude: Claude Code on a magpie model is told the longest reply the model can write, CLAUDE_CODE_MAX_OUTPUT_TOKENS (H20 on Discord: DeepSeek through WorkBuddy stopped at "Claude's response exceeded the 32000 output token maximum") Claude Code 2.1.291 asks a model it doesn't know (any but Claude's own names) for 32000 tokens at most (dte: default dv=32000, upper dN=128000), whatever the model can write, and raises that error when the reply stops on max_tokens; only CLAUDE_CODE_MAX_OUTPUT_TOKENS raises it (n4t, capped at the upper limit). A reasoning model's thinking counts toward the reply, so DeepSeek V4 on a long turn ran into it with plenty of quota left. magpie's translation doesn't make it worse: max_tokens goes upstream as Claude Code sent it (clamped only for a group's member, to its own limit), and finish_reason "length" maps to max_tokens as it should. Now, as magpie writes CLAUDE_CODE_MAX_CONTEXT_TOKENS, it writes CLAUDE_CODE_MAX_OUTPUT_TOKENS with Claude Code's model: the least known reply limit of the models it runs on (main, tiers, subagents; WorkBuddy's DeepSeek V4 is 128000), since one value serves them all. It is left out when the main model is a Claude one, which Claude Code knows; when one of the others has no known limit; and when the least is no more than 32000. A value the user set, before magpie or over magpie's, is theirs and never touched; magpie's is taken out when Claude Code goes back to Claude's models or off magpie. TestClaudeOutputTokens fails on the old code (mid: "") and passes; go test ./internal/agent/, -race -count=5 on the Claude context/output/compact tests, go vet (darwin, windows) and darwin/linux(nogui)/windows builds pass. Not tried with a real Claude Code on DeepSeek. --- internal/agent/claude.go | 86 ++++++++++++++++++- internal/agent/claude_output_test.go | 124 +++++++++++++++++++++++++++ 2 files changed, 207 insertions(+), 3 deletions(-) create mode 100644 internal/agent/claude_output_test.go diff --git a/internal/agent/claude.go b/internal/agent/claude.go index 979f90b56..02f73938b 100644 --- a/internal/agent/claude.go +++ b/internal/agent/claude.go @@ -146,6 +146,19 @@ const claudeContextEnv = "CLAUDE_CODE_MAX_CONTEXT_TOKENS" // model, which Anthropic runs to its whole window. const claudeCompactEnv = "CLAUDE_CODE_AUTO_COMPACT_WINDOW" +// claudeOutputEnv is the longest reply Claude Code asks a model for. One it +// doesn't know (any but Claude's own names) it asks for 32000 at most +// (claudeUnknownOutput), whatever the model can write, so a model that +// writes more — a reasoning one, whose thinking counts toward the reply — +// stopped at "Claude's response exceeded the 32000 output token maximum" +// (H20 on Discord, DeepSeek through WorkBuddy). Claude Code caps the value +// at 128000 for such a model, and at its own maximum for a Claude model. +const claudeOutputEnv = "CLAUDE_CODE_MAX_OUTPUT_TOKENS" + +// claudeUnknownOutput is the reply Claude Code asks a model it doesn't know +// for without claudeOutputEnv (2.1.291). +const claudeUnknownOutput = 32000 + // claudeCapsEnv tells Claude Code what a model it doesn't know can do, as // "=effort,xhigh_effort;=…" (a trailing * a prefix, [1m] // taken off the model first, the model lowercased, the pattern not). Run on @@ -385,6 +398,20 @@ func claudeIn(at place) *Agent { } return nil } + // the reply length magpie last wrote, kept apart from the user's own + // in the same way + outputKey := at.key("claude.output_tokens") + outputOurs := func() bool { + cur := env(claudeOutputEnv) + return cur != "" && cur == stashLoad()[outputKey] + } + dropOutput := func() error { + defer forget(outputKey) + if outputOurs() { + return edit.DelJSON(path, "env."+claudeOutputEnv) + } + return nil + } // the capabilities magpie last wrote, kept apart from the user's own in // the same way: theirs is left as it is, magpie's adds to nothing capsKey := at.key("claude.capabilities") @@ -541,6 +568,9 @@ func claudeIn(at place) *Agent { if err := dropWindow(); err != nil { return "", err } + if err := dropOutput(); err != nil { + return "", err + } if err := dropCompact(); err != nil { return "", err } @@ -691,14 +721,31 @@ func claudeIn(at place) *Agent { } else { forget(windowKey) } - if err := edit.SetJSON(path, kvs...); err != nil { - return err - } models := []string{main} for _, t := range claudeTiers { m, _ := tierAt(tiers[t]) models = append(models, m) } + outModels := models + if sub != "" { + m, _ := tierAt(sub) + outModels = append(slices.Clip(models), m) + } + // the longest reply the models can write, past the 32000 Claude + // Code would ask them for; one the user set is theirs + if env(claudeOutputEnv) == "" || outputOurs() { + if n := claudeOutput(outModels...); n > 0 { + kvs = append(kvs, edit.KV{Path: "env." + claudeOutputEnv, Value: strconv.Itoa(n)}) + stash(map[string]string{outputKey: strconv.Itoa(n)}) + } else if err := dropOutput(); err != nil { + return err + } + } else { + forget(outputKey) + } + if err := edit.SetJSON(path, kvs...); err != nil { + return err + } if err := writeCompact(); err != nil { return err } @@ -1348,6 +1395,39 @@ func claudeWindow(main string, tiers map[string]string) int { return w } +// claudeOutput is the reply length to tell Claude Code for the models it +// runs on (main first), 0 to leave it to Claude Code: when the main model is +// a Claude one, which Claude Code knows; when a model it runs on that isn't +// has no known limit, as one value serves them all; and when the least of +// theirs is no more than what Claude Code asks for anyway. +func claudeOutput(models ...string) int { + if len(models) == 0 || claudeModel(models[0]) { + return 0 + } + output := map[string]int{} + for _, m := range magpieModels("claude") { + output[m.ID] = m.Output + } + n := 0 + for _, ref := range models { + if ref == "" || claudeModel(ref) { + continue + } + ref = strings.TrimSuffix(ref, "[1m]") + o := cmp.Or(output[ref], catalog.OutputOf(ref)) + if o <= 0 { + return 0 + } + if n == 0 || o < n { + n = o + } + } + if n <= claudeUnknownOutput { + return 0 + } + return n +} + // claudeManaged is where an administrator's Claude Code settings live; a var // so tests can point it elsewhere. var claudeManaged = func() string { diff --git a/internal/agent/claude_output_test.go b/internal/agent/claude_output_test.go new file mode 100644 index 000000000..f0004afe9 --- /dev/null +++ b/internal/agent/claude_output_test.go @@ -0,0 +1,124 @@ +package agent + +import ( + "path/filepath" + "testing" + + "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/edit" + "github.com/yetone/magpie/internal/provider" +) + +// TestClaudeOutputTokens: Claude Code asks a model it doesn't know for a +// reply of 32000 tokens at most, so a reasoning model stopped at "Claude's +// response exceeded the 32000 output token maximum" (H20, DeepSeek through +// WorkBuddy). magpie tells it the longest reply the models it runs on can +// write (CLAUDE_CODE_MAX_OUTPUT_TOKENS): the least of theirs, none when one +// isn't known, is no more than 32000 or the main model is Claude's own, and +// never over a value the user set. +func TestClaudeOutputTokens(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config")) + t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache")) + if err := provider.Save(provider.Provider{ID: "v", Name: "V", Chat: "https://example.test/v1", Key: "k", + Models: []string{"deep", "mid", "short", "plain"}}); err != nil { + t.Fatal(err) + } + if err := catalog.SaveLive("v", "https://example.test/v1", []catalog.Model{ + {ID: "deep", Context: 1000000, Output: 384000}, {ID: "mid", Context: 400000, Output: 64000}, + {ID: "short", Context: 128000, Output: 8192}, {ID: "plain"}, + }); err != nil { + t.Fatal(err) + } + path := filepath.Join(home, ".claude", "settings.json") + writeFile(t, path, `{"theme":"dark"}`) + a := claude(home) + output := func() string { v, _ := edit.GetJSON(path, "env."+claudeOutputEnv); return v } + set := func(key, v string) { + t.Helper() + if err := a.Field(key).Set(v); err != nil { + t.Fatal(err) + } + } + + set("model", "v/mid") + if output() != "64000" { + t.Fatalf("mid: %q", output()) + } + set("model", "v/deep") + if output() != "384000" { + t.Fatalf("switched to deep: %q", output()) + } + // one value serves every model Claude Code runs on: a tier's smaller + // limit bounds it + set("haiku", "v/mid") + if output() != "64000" { + t.Fatalf("haiku on mid beside deep: %q", output()) + } + // no more than Claude Code asks for anyway, or not known: left out + set("haiku", "v/short") + if output() != "" { + t.Fatalf("haiku on an 8K model: %q", output()) + } + set("haiku", "v/plain") + if output() != "" { + t.Fatalf("haiku on a model of no known limit: %q", output()) + } + set("haiku", "v/deep") + set("model", "v/plain") + if output() != "" { + t.Fatalf("a main model of no known limit: %q", output()) + } + set("model", "v/deep") + if output() != "384000" { + t.Fatalf("back on deep: %q", output()) + } + // Claude's own model, which Claude Code knows, and Claude Code as + // installed + set("model", "claude-opus-4-8") + if output() != "" { + t.Fatalf("Claude's own model kept magpie's value: %q", output()) + } + set("model", "v/deep") + set("model", "") + if output() != "" { + t.Fatalf("reset kept magpie's value: %q", output()) + } + if v, _ := edit.GetJSON(path, "theme"); v != "dark" { + t.Fatal("theme lost") + } + + // the user's own value is theirs, set before magpie or over magpie's + if err := edit.SetJSON(path, edit.KV{Path: "env." + claudeOutputEnv, Value: "64000"}); err != nil { + t.Fatal(err) + } + for _, v := range []string{"v/deep", "v/plain", "v/mid", "claude-opus-4-8", ""} { + set("model", v) + if output() != "64000" { + t.Fatalf("the user's value after %q: %q", v, output()) + } + } + if err := edit.DelJSON(path, "env."+claudeOutputEnv); err != nil { + t.Fatal(err) + } + set("model", "v/deep") + if err := edit.SetJSON(path, edit.KV{Path: "env." + claudeOutputEnv, Value: "100000"}); err != nil { + t.Fatal(err) + } + set("model", "v/mid") + set("model", "") + if output() != "100000" { + t.Fatalf("a value the user changed magpie's to: %q", output()) + } + // and one the same as magpie would write, but not written by it + if err := edit.SetJSON(path, edit.KV{Path: "env." + claudeOutputEnv, Value: "64000"}); err != nil { + t.Fatal(err) + } + set("model", "v/mid") + set("model", "") + if output() != "64000" { + t.Fatalf("the user's 64000 was taken for magpie's: %q", output()) + } +}