Skip to content

Commit 0166099

Browse files
author
liyongjie.103
committed
fix: apply anthropic default reasoning effort
1 parent 6ae83b1 commit 0166099

2 files changed

Lines changed: 34 additions & 5 deletions

File tree

‎src/adapters/anthropic.ts‎

Lines changed: 12 additions & 5 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 {
@@ -513,6 +514,11 @@ function supportsExplicitThinkingDisable(modelId: string): boolean {
513514
return meetsFamilyMinimum(modelId, EXPLICIT_THINKING_DISABLE_FAMILY_MINIMUMS);
514515
}
515516

517+
function defaultReasoningEffort(provider: OcxProviderConfig, modelId: string): string | undefined {
518+
const value = modelRecordValue(provider.modelDefaultReasoningEfforts, modelId);
519+
return typeof value === "string" && value.trim() ? value.trim() : undefined;
520+
}
521+
516522
/** `output_config.effort` accepts low|medium|high|xhigh|max — "minimal" is rejected with a 400. */
517523
function adaptiveEffort(effort: string): string {
518524
return effort === "minimal" ? "low" : effort;
@@ -929,18 +935,19 @@ export function createAnthropicAdapter(provider: OcxProviderConfig, cacheRetenti
929935
// anyway, and thinking shares the caller's `max_tokens` — which truncates a small-budget
930936
// request before it can emit its stop sequence (#545). Say "disabled" out loud where the
931937
// model both defaults to thinking and accepts being told not to.
932-
if (parsed.options.reasoning === "none" && supportsExplicitThinkingDisable(parsed.modelId)) {
938+
const effectiveReasoning = parsed.options.reasoning ?? defaultReasoningEffort(provider, parsed.modelId);
939+
if (effectiveReasoning === "none" && supportsExplicitThinkingDisable(parsed.modelId)) {
933940
body.thinking = { type: "disabled" };
934-
} else if (typeof parsed.options.reasoning === "string" && parsed.options.reasoning !== "none") {
941+
} else if (typeof effectiveReasoning === "string" && effectiveReasoning !== "none") {
935942
if (usesAdaptiveThinking(parsed.modelId)) {
936943
// Adaptive-thinking models replace the token budget with an effort knob and reject
937944
// `thinking.type: "enabled"` outright. `max_tokens` still caps thinking plus visible
938945
// output, so high effort needs the same total-token headroom as budget thinking or a
939946
// default 8192-token request can spend everything on thought and return empty text.
940947
body.thinking = { type: "adaptive" };
941-
body.output_config = { effort: adaptiveEffort(parsed.options.reasoning) };
948+
body.output_config = { effort: adaptiveEffort(effectiveReasoning) };
942949
const explicitMaxOut = parsed.options.maxOutputTokens;
943-
const wantBudget = reasoningBudget(parsed.options.reasoning);
950+
const wantBudget = reasoningBudget(effectiveReasoning);
944951
const floor = wantBudget + OUTPUT_HEADROOM;
945952
// Preserve explicit caller limits as-is; for omitted limits use the adaptive ceiling
946953
// so effort=max (budget=32k) still leaves OUTPUT_HEADROOM tokens for visible output.
@@ -953,7 +960,7 @@ export function createAnthropicAdapter(provider: OcxProviderConfig, cacheRetenti
953960
// 400s ("max_tokens must be greater than thinking.budget_tokens"). Size them so max_tokens
954961
// always exceeds the budget within a model-safe ceiling, reserving room for visible output.
955962
const maxOut = parsed.options.maxOutputTokens ?? DEFAULT_MAX_TOKENS;
956-
const wantBudget = reasoningBudget(parsed.options.reasoning);
963+
const wantBudget = reasoningBudget(effectiveReasoning);
957964
const maxTokens = Math.min(REASONING_MAX_TOKENS_CEILING, Math.max(maxOut, wantBudget + OUTPUT_HEADROOM));
958965
const budget = Math.max(MIN_THINKING_BUDGET, Math.min(wantBudget, maxTokens - OUTPUT_FLOOR));
959966
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)