mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 13:38:31 +00:00
fix(claude): reconcile max_tokens vs thinking budget and lift per-model ceiling (#2381)
On the translated OpenAI->Claude path, adjustMaxTokens capped max_tokens before applyThinking set thinking.budget_tokens, so max-effort budget (128000) could exceed a 64k-clamped max_tokens -> Anthropic 400. prepareClaudeRequest now reconciles after the budget is known: prefer raising max_tokens, only shrink budget when it meets/exceeds the ceiling. Also lift the global 64000 cap: the ceiling is now the model's real maxOutput, so high-output models (fable/mythos, opus-4.8/sonnet-4.6) get their full budget. adjustMaxTokens gains an optional ceiling arg (default unchanged, callers untouched); openai-to-claude passes the model maxOutput. Native Claude Code passthrough is unaffected. Co-Authored-By: Claude <noreply@anthropic.com> Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
committed by
decolua
co-authored by
Claude
Cursor
parent
5041494e1c
commit
46e6c01a01
@@ -67,6 +67,101 @@ describe("OpenAI → Claude context mapping", () => {
|
||||
expect(JSON.stringify(out), "remote image dropped").toContain("pic.png");
|
||||
});
|
||||
|
||||
// prepareClaudeRequest reconciles max_tokens vs thinking.budget_tokens.
|
||||
// applyThinking runs after adjustMaxTokens caps max_tokens, so a claude-budget
|
||||
// model at "max" effort (budget 128000) can exceed the clamped max_tokens and
|
||||
// trip Anthropic's "max_tokens > budget_tokens" rule (400). See claude.js.
|
||||
describe("max_tokens vs thinking.budget_tokens reconciliation", () => {
|
||||
// 64k-ceiling model (maxOutput 64000) + max-effort budget 128000: budget alone
|
||||
// exceeds the ceiling → cap max_tokens at 64000 and shrink budget below it.
|
||||
it("max effort budget on a 64k model → budget < max_tokens ≤ 64000", () => {
|
||||
const out = prepareClaudeRequest({
|
||||
model: "claude-opus-4-20250514",
|
||||
max_tokens: 64000,
|
||||
thinking: { type: "enabled", budget_tokens: 128000 },
|
||||
messages: [{ role: "user", content: "q" }],
|
||||
}, "anthropic");
|
||||
expect(out.max_tokens).toBe(64000);
|
||||
expect(out.thinking.budget_tokens).toBeLessThan(out.max_tokens);
|
||||
expect(out.thinking.budget_tokens).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
// Budget fits under the ceiling but exceeds a small client max_tokens →
|
||||
// raise max_tokens to fit, preserving the requested thinking depth.
|
||||
it("xhigh budget with a low client max_tokens → raise max_tokens, preserve budget", () => {
|
||||
const out = prepareClaudeRequest({
|
||||
model: "claude-opus-4-20250514",
|
||||
max_tokens: 16000,
|
||||
thinking: { type: "enabled", budget_tokens: 32768 },
|
||||
messages: [{ role: "user", content: "q" }],
|
||||
}, "anthropic");
|
||||
expect(out.thinking.budget_tokens).toBe(32768);
|
||||
expect(out.max_tokens).toBe(33792); // 32768 + 1024, under the 64000 ceiling
|
||||
});
|
||||
|
||||
// Budget already below max_tokens → nothing to reconcile.
|
||||
it("high budget under max_tokens → both unchanged", () => {
|
||||
const out = prepareClaudeRequest({
|
||||
model: "claude-opus-4-20250514",
|
||||
max_tokens: 64000,
|
||||
thinking: { type: "enabled", budget_tokens: 24576 },
|
||||
messages: [{ role: "user", content: "q" }],
|
||||
}, "anthropic");
|
||||
expect(out.max_tokens).toBe(64000);
|
||||
expect(out.thinking.budget_tokens).toBe(24576);
|
||||
});
|
||||
|
||||
// Non-budget thinking shapes (adaptive / disabled) carry no budget_tokens →
|
||||
// the reconciliation must never touch them.
|
||||
it("adaptive thinking (no budget_tokens) is left untouched", () => {
|
||||
const out = prepareClaudeRequest({
|
||||
model: "claude-opus-4-20250514",
|
||||
max_tokens: 64000,
|
||||
thinking: { type: "adaptive" },
|
||||
messages: [{ role: "user", content: "q" }],
|
||||
}, "anthropic");
|
||||
expect(out.max_tokens).toBe(64000);
|
||||
expect(out.thinking).toEqual({ type: "adaptive" });
|
||||
});
|
||||
|
||||
// Lifted ceiling: a claude-budget model whose caps declare maxOutput 128000
|
||||
// (e.g. fable) may use the full budget at max effort instead of being pinned
|
||||
// to the conservative 64000 default.
|
||||
it("max effort budget on a 128k model → max_tokens up to 128000, budget preserved just under", () => {
|
||||
const out = prepareClaudeRequest({
|
||||
model: "claude-fable-5",
|
||||
max_tokens: 64000,
|
||||
thinking: { type: "enabled", budget_tokens: 128000 },
|
||||
messages: [{ role: "user", content: "q" }],
|
||||
}, "anthropic");
|
||||
expect(out.max_tokens).toBe(128000);
|
||||
expect(out.thinking.budget_tokens).toBe(126976); // 128000 - 1024
|
||||
expect(out.thinking.budget_tokens).toBeLessThan(out.max_tokens);
|
||||
});
|
||||
|
||||
// Regression: a default 64k-ceiling model still clamps an over-large client
|
||||
// max_tokens down to 64000 (the lift is per-model, not global).
|
||||
it("over-large client max_tokens on a 64k model is still clamped to 64000", () => {
|
||||
const out = prepareClaudeRequest({
|
||||
model: "claude-opus-4-20250514",
|
||||
max_tokens: 120000,
|
||||
messages: [{ role: "user", content: "q" }],
|
||||
}, "anthropic");
|
||||
expect(out.max_tokens).toBe(64000);
|
||||
});
|
||||
|
||||
// Lifted ceiling for a 128k model: a large client max_tokens is now allowed
|
||||
// through instead of being clamped to 64000.
|
||||
it("large client max_tokens on a 128k model is allowed up to maxOutput", () => {
|
||||
const out = prepareClaudeRequest({
|
||||
model: "claude-fable-5",
|
||||
max_tokens: 100000,
|
||||
messages: [{ role: "user", content: "q" }],
|
||||
}, "anthropic");
|
||||
expect(out.max_tokens).toBe(100000);
|
||||
});
|
||||
});
|
||||
|
||||
it("DeepSeek Claude transport adds a thinking placeholder before tool_use in thinking mode", () => {
|
||||
const out = prepareClaudeRequest({
|
||||
model: "deepseek-v4-pro",
|
||||
|
||||
Reference in New Issue
Block a user