Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(agent-runtime): send adaptive thinking to effort-only Claude models (#949)

onerentopcb8a87d99d80
diff --git a/docs/spec/03-runtime/11-provider-model-system.md b/docs/spec/03-runtime/11-provider-model-system.md
index 038c5d179..39150bcb0 100644
--- a/docs/spec/03-runtime/11-provider-model-system.md
+++ b/docs/spec/03-runtime/11-provider-model-system.md
@@ -107,6 +107,14 @@ missing reasoning is filled with a documented placeholder instead of `""`
ADR 0256 / #296). Official `deepseek.com` rows keep empty-string fill (#223).
The overlay does not change `thinkingFormat`.
+Anthropic Messages requests set `forceAdaptiveThinking: true` when the
+models.dev record publishes a reasoning `effort` option and no
+`budget_tokens` option (for example Opus 4.7+, Opus 5.x, Fable). Those models
+reject `thinking.type=enabled` with HTTP 400, and models.dev carries no pi-ai
+compat record, so without the flag pi-ai would fall back to budget thinking.
+Models that still publish `budget_tokens` keep budget thinking, and an
+explicit catalog `compat` record is preserved.
+
## 5. Built-in vendor matrix (ship intent)
> Model metadata follows the bundled/in-memory models.dev catalog. Provider adapters remain
diff --git a/docs/zh-CN/spec/03-runtime/11-provider-model-system.md b/docs/zh-CN/spec/03-runtime/11-provider-model-system.md
index 23b52b5e7..1af2ff7ef 100644
--- a/docs/zh-CN/spec/03-runtime/11-provider-model-system.md
+++ b/docs/zh-CN/spec/03-runtime/11-provider-model-system.md
@@ -102,6 +102,13 @@ pi-ai 去发出 `x-opencode-session`。每个提供商行(AI 服务或 OAuth
见 ADR 0256 / #296)。官方 `deepseek.com` 行仍使用空串回填(#223)。该覆盖不改
`thinkingFormat`。
+当 models.dev 记录发布了推理 `effort` 选项且没有 `budget_tokens` 选项时
+(例如 Opus 4.7+、Opus 5.x、Fable),Anthropic Messages 请求会设置
+`forceAdaptiveThinking: true`。这些模型会以 HTTP 400 拒绝
+`thinking.type=enabled`,而 models.dev 不携带 pi-ai 的 compat 记录,缺少该标志时
+pi-ai 会回落到 budget 思考。仍发布 `budget_tokens` 的模型保持 budget 思考,显式的
+目录 `compat` 记录会被保留。
+
## 5. 内置供应商矩阵(发货意图)
> 确切的可用性取决于引脚版本的 pi-ai 支持;产品必须公开所有受支持的产品,并为其余产品保持与 OpenAI 兼容的路径开放。
diff --git a/packages/agent-runtime/src/provider-binding.test.ts b/packages/agent-runtime/src/provider-binding.test.ts
index 817047111..90fd71c87 100644
--- a/packages/agent-runtime/src/provider-binding.test.ts
+++ b/packages/agent-runtime/src/provider-binding.test.ts
@@ -96,6 +96,93 @@ describe("Anthropic runtime endpoint", () => {
});
});
+describe("Anthropic adaptive thinking from models.dev reasoning options", () => {
+ function anthropicProvider(
+ modelId: string,
+ reasoningOptions: ModelConfig["reasoningOptions"],
+ ): RuntimeProviderConfig {
+ return {
+ ...keyedProvider,
+ id: "anthropic",
+ name: "Anthropic",
+ baseUrl: "https://api.anthropic.com",
+ modelId,
+ apiStyle: "anthropic_messages",
+ supportsReasoning: true,
+ supportedThinkingLevels: ["low", "medium", "high"],
+ modelConfig: {
+ source: "models.dev",
+ name: modelId,
+ baseUrl: "https://api.anthropic.com",
+ reasoning: true,
+ reasoningOptions,
+ supportedThinkingLevels: ["low", "medium", "high"],
+ input: ["text"],
+ contextWindow: 1_000_000,
+ maxTokens: 128_000,
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
+ },
+ };
+ }
+
+ async function thinkingRequest(provider: RuntimeProviderConfig) {
+ const model = buildProviderModel(provider);
+ const requests: Record<string, unknown>[] = [];
+ const fetch = vi.fn(async (_input: RequestInfo | URL, init?: RequestInit) => {
+ requests.push(JSON.parse(String(init?.body)) as Record<string, unknown>);
+ return new Response("bad gateway", { status: 502 });
+ });
+ await createProviderModels(provider, model)
+ .streamSimple(
+ model,
+ {
+ systemPrompt: "system",
+ messages: [{ role: "user", content: "hello", timestamp: Date.now() }],
+ tools: [],
+ },
+ { reasoning: "medium", fetch, maxRetries: 0 },
+ )
+ .result();
+ return requests[0];
+ }
+
+ it("sends adaptive thinking to effort-only Claude models such as Opus 5.5", async () => {
+ const request = await thinkingRequest(
+ anthropicProvider("claude-opus-5-5", [
+ { type: "effort", values: ["low", "medium", "high", "xhigh", "max"] },
+ ]),
+ );
+
+ expect(request?.thinking).toMatchObject({ type: "adaptive" });
+ expect(request?.thinking).not.toHaveProperty("budget_tokens");
+ expect(request?.output_config).toEqual({ effort: "medium" });
+ });
+
+ it("keeps budget thinking for models that publish budget_tokens", async () => {
+ const request = await thinkingRequest(
+ anthropicProvider("claude-sonnet-4-5", [{ type: "budget_tokens", min: 1024 }]),
+ );
+
+ expect(request?.thinking).toMatchObject({ type: "enabled" });
+ expect(request?.thinking).toHaveProperty("budget_tokens");
+ });
+
+ it("preserves an explicit catalog adaptive override", () => {
+ const provider = anthropicProvider("claude-opus-4-6", [
+ { type: "effort", values: ["low", "medium", "high", "max"] },
+ { type: "budget_tokens", min: 1024 },
+ ]);
+ provider.modelConfig = {
+ ...provider.modelConfig!,
+ compat: { forceAdaptiveThinking: true },
+ };
+
+ expect(buildProviderModel(provider).compat).toMatchObject({
+ forceAdaptiveThinking: true,
+ });
+ });
+});
+
describe("buildProviderModel OpenAI-compatible role compatibility", () => {
const reasoningProvider: RuntimeProviderConfig = {
...keyedProvider,
diff --git a/packages/agent-runtime/src/provider-binding.ts b/packages/agent-runtime/src/provider-binding.ts
index 2b331cb25..77a842e94 100644
--- a/packages/agent-runtime/src/provider-binding.ts
+++ b/packages/agent-runtime/src/provider-binding.ts
@@ -195,6 +195,24 @@ export function copilotRequestHeaders(
});
}
+/**
+ * Claude models that publish an effort ladder without a `budget_tokens`
+ * option (Opus 4.7+, Opus 5.x, Fable, ...) reject `thinking.type=enabled`
+ * with a 400. pi-ai only sends adaptive thinking when
+ * `compat.forceAdaptiveThinking` is set, and models.dev carries no compat
+ * record, so derive the flag from the published reasoning options.
+ */
+function requiresAdaptiveThinking(
+ model: Pick<ModelConfig, "reasoning" | "reasoningOptions">,
+): boolean {
+ const options = model.reasoningOptions ?? [];
+ return (
+ model.reasoning &&
+ options.some((option) => option.type === "effort") &&
+ !options.some((option) => option.type === "budget_tokens")
+ );
+}
+
export function buildProviderModel(
provider: RuntimeProviderConfig,
): Model<Api> {
@@ -239,7 +257,9 @@ export function buildProviderModel(
...(deepseekCompat ?? {}),
supportsDeveloperRole: catalogModel.compat?.supportsDeveloperRole === true,
}
- : catalogModel.compat;
+ : binding.api === "anthropic-messages" && requiresAdaptiveThinking(catalogModel)
+ ? { ...(catalogModel.compat ?? {}), forceAdaptiveThinking: true }
+ : catalogModel.compat;
return {
...catalogModel,
id: provider.modelId,