Skip to content

Commit e9b767f

Browse files
author
liyongjie.103
committed
fix(anthropic): apply provider default reasoning effort when caller omits it
Anthropic-compatible models that require explicit thinking now work when the caller omits reasoning.effort, as long as the provider declares modelDefaultReasoningEfforts. Before this, those defaults were visible in model metadata but the adapter did not apply them to outbound /v1/messages requests, so always-thinking gateways could reject otherwise valid calls. Explicit caller reasoning still wins over the provider default.
1 parent a60d517 commit e9b767f

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

‎src/adapters/anthropic.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { CLAUDE_CODE_HEADERS, claudeCodeSessionId } from "./client-fingerprint";
2727
import { buildNonOpenAIToolCatalogNudgeForTools } from "./tool-catalog-nudge";
2828
import { decodeServerSentEvents } from "../lib/sse-decoder";
2929
import { isTranslatorBudgetExceededError, retainTranslatedEventBatch, type TranslatorBudget } from "../lib/translator-budget";
30+
import { modelRecordValue } from "../reasoning-effort";
3031

3132
/** Map a user content part to an Anthropic content block (text or image source). */
3233
function toAnthropicContentPart(p: OcxContentPart): unknown {
@@ -518,6 +519,11 @@ function adaptiveEffort(effort: string): string {
518519
return effort === "minimal" ? "low" : effort;
519520
}
520521

522+
function defaultReasoningEffort(provider: OcxProviderConfig, modelId: string): string | undefined {
523+
const value = modelRecordValue(provider.modelDefaultReasoningEfforts, modelId);
524+
return typeof value === "string" && value.trim() ? value.trim() : undefined;
525+
}
526+
521527
function usageFromAnthropic(usage: Record<string, number> | undefined): OcxUsage | undefined {
522528
if (!usage) return undefined;
523529
const hasCache = usage.cache_read_input_tokens !== undefined || usage.cache_creation_input_tokens !== undefined;
@@ -926,16 +932,17 @@ export function createAnthropicAdapter(provider: OcxProviderConfig, cacheRetenti
926932
// anyway, and thinking shares the caller's `max_tokens` — which truncates a small-budget
927933
// request before it can emit its stop sequence (#545). Say "disabled" out loud where the
928934
// model both defaults to thinking and accepts being told not to.
929-
if (parsed.options.reasoning === "none" && supportsExplicitThinkingDisable(parsed.modelId)) {
935+
const effectiveReasoning = parsed.options.reasoning ?? defaultReasoningEffort(provider, parsed.modelId);
936+
if (effectiveReasoning === "none" && supportsExplicitThinkingDisable(parsed.modelId)) {
930937
body.thinking = { type: "disabled" };
931-
} else if (typeof parsed.options.reasoning === "string" && parsed.options.reasoning !== "none") {
938+
} else if (typeof effectiveReasoning === "string" && effectiveReasoning !== "none") {
932939
if (usesAdaptiveThinking(parsed.modelId)) {
933940
// Adaptive-thinking models replace the token budget with an effort knob and reject
934941
// `thinking.type: "enabled"` outright. `max_tokens` still caps thinking plus visible
935942
// output, so high effort needs the same total-token headroom as budget thinking or a
936943
// default 8192-token request can spend everything on thought and return empty text.
937944
body.thinking = { type: "adaptive" };
938-
const effort = adaptiveEffort(parsed.options.reasoning);
945+
const effort = adaptiveEffort(effectiveReasoning);
939946
body.output_config = { effort };
940947
const explicitMaxOut = parsed.options.maxOutputTokens;
941948
const wantBudget = reasoningBudget(effort);
@@ -951,7 +958,7 @@ export function createAnthropicAdapter(provider: OcxProviderConfig, cacheRetenti
951958
// 400s ("max_tokens must be greater than thinking.budget_tokens"). Size them so max_tokens
952959
// always exceeds the budget within a model-safe ceiling, reserving room for visible output.
953960
const maxOut = parsed.options.maxOutputTokens ?? DEFAULT_MAX_TOKENS;
954-
const wantBudget = reasoningBudget(parsed.options.reasoning);
961+
const wantBudget = reasoningBudget(effectiveReasoning);
955962
const maxTokens = Math.min(REASONING_MAX_TOKENS_CEILING, Math.max(maxOut, wantBudget + OUTPUT_HEADROOM));
956963
const budget = Math.max(MIN_THINKING_BUDGET, Math.min(wantBudget, maxTokens - OUTPUT_FLOOR));
957964
body.max_tokens = maxTokens;

‎tests/anthropic-reasoning.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@ describe("anthropic extended-thinking gate", () => {
4040
expect(b.top_p).toBe(0.8);
4141
});
4242

43+
test("modelDefaultReasoningEfforts supplies reasoning when caller omits it", async () => {
44+
const b = await bodyOf(parsed(undefined, { temperature: 0.5, topP: 0.8 }, "always-thinking-model"), {
45+
...provider,
46+
modelDefaultReasoningEfforts: { "always-thinking-model": "high" },
47+
});
48+
const thinking = b.thinking as { type: string; budget_tokens: number } | undefined;
49+
expect(thinking?.type).toBe("enabled");
50+
expect(typeof thinking?.budget_tokens).toBe("number");
51+
expect(b.temperature).toBeUndefined();
52+
expect(b.top_p).toBeUndefined();
53+
});
54+
55+
test("explicit reasoning overrides modelDefaultReasoningEfforts", async () => {
56+
const b = await bodyOf(parsed("low", {}, "always-thinking-model"), {
57+
...provider,
58+
modelDefaultReasoningEfforts: { "always-thinking-model": "high" },
59+
});
60+
const thinking = b.thinking as { type: string; budget_tokens: number } | undefined;
61+
expect(thinking?.type).toBe("enabled");
62+
expect(thinking?.budget_tokens).toBe(4096);
63+
});
64+
4365
test("reasoning 'high' enables thinking and drops sampling (extended-thinking rule)", async () => {
4466
const b = await bodyOf(parsed("high", { temperature: 0.3, topP: 0.9 }));
4567
const thinking = b.thinking as { type: string; budget_tokens: number } | undefined;

0 commit comments

Comments
 (0)