Skip to content

Build your personal knowledge base with Trilium Notes

fix(llm): thinking not working on newer Anthropic models

Elian Doran2223ec431572
diff --git a/apps/server/src/services/llm/providers/anthropic.spec.ts b/apps/server/src/services/llm/providers/anthropic.spec.ts
index bbaee44462..989832f160 100644
--- a/apps/server/src/services/llm/providers/anthropic.spec.ts
+++ b/apps/server/src/services/llm/providers/anthropic.spec.ts
@@ -163,6 +163,7 @@ describe("AnthropicProvider tool handling", () => {
it("sets the thinking budget and agentic options on the extended-thinking path with tools", () => {
const provider = new AnthropicProvider("sk-ant-test");
provider.chat([{ role: "user", content: "hi" }], {
+ model: "claude-sonnet-4-5-20250929",
enableExtendedThinking: true,
enableNoteTools: true,
thinkingBudget: 12000,
@@ -182,13 +183,32 @@ describe("AnthropicProvider tool handling", () => {
it("defaults the thinking budget and floors maxOutputTokens when unset", () => {
const provider = new AnthropicProvider("sk-ant-test");
- provider.chat([{ role: "user", content: "hi" }], { enableExtendedThinking: true });
+ provider.chat([{ role: "user", content: "hi" }], {
+ model: "claude-sonnet-4-5-20250929",
+ enableExtendedThinking: true
+ });
const opts = streamTextMock.mock.calls[0][0] as any;
expect(opts.providerOptions.anthropic.thinking.budgetTokens).toBe(10000);
// max(8096, 10000 + 4000) = 14000
expect(opts.maxOutputTokens).toBe(14000);
});
+
+ it("uses adaptive thinking on Opus 4.7+ / Sonnet 4.6, which reject manual budgets", () => {
+ const provider = new AnthropicProvider("sk-ant-test");
+
+ // Default model (Sonnet 4.6) plus the newest Opus tiers are adaptive-only.
+ for (const model of ["claude-opus-4-8", "claude-opus-4-7", "claude-sonnet-4-6"]) {
+ streamTextMock.mockClear();
+ provider.chat([{ role: "user", content: "hi" }], { model, enableExtendedThinking: true });
+
+ const opts = streamTextMock.mock.calls[0][0] as any;
+ expect(opts.providerOptions.anthropic.thinking).toEqual({
+ type: "adaptive",
+ display: "summarized"
+ });
+ }
+ });
});
/**
diff --git a/apps/server/src/services/llm/providers/anthropic.ts b/apps/server/src/services/llm/providers/anthropic.ts
index 02354211c3..069a79e79c 100644
--- a/apps/server/src/services/llm/providers/anthropic.ts
+++ b/apps/server/src/services/llm/providers/anthropic.ts
@@ -8,6 +8,14 @@ import { BaseProvider, buildModelList, buildModelMessage } from "./base_provider
/** Anthropic ephemeral prompt-caching breakpoint. */
const CACHE_CONTROL = { anthropic: { cacheControl: { type: "ephemeral" as const } } };
+/**
+ * Models that support adaptive extended thinking. Opus 4.7 / 4.8 reject the
+ * manual `{ type: "enabled", budgetTokens }` form with a 400 and accept adaptive
+ * only; Opus 4.6 and Sonnet 4.6 accept both but prefer adaptive. Everything else
+ * (older Opus/Sonnet, Haiku 4.5) only supports the manual budget form.
+ */
+const ADAPTIVE_THINKING_MODELS = /^claude-(?:opus-4-[678]|sonnet-4-6)/;
+
/**
* Available Anthropic models with pricing (USD per million tokens).
* Source: https://docs.anthropic.com/en/docs/about-claude/models
@@ -165,23 +173,25 @@ export class AnthropicProvider extends BaseProvider {
const chatMessages = this.applyNoteHint(messages.filter(m => m.role !== "system"), config);
const coreMessages = this.buildMessages(chatMessages);
+ const model = config.model || this.defaultModel;
const thinkingBudget = config.thinkingBudget || 10000;
const maxTokens = Math.max(config.maxTokens || 8096, thinkingBudget + 4000);
+ // Adaptive-capable models let Claude decide depth (and stream visible
+ // reasoning via `display: "summarized"`); older models take a manual budget.
+ const thinking = ADAPTIVE_THINKING_MODELS.test(model)
+ ? { type: "adaptive" as const, display: "summarized" as const }
+ : { type: "enabled" as const, budgetTokens: thinkingBudget };
+
const streamOptions: Parameters<typeof streamText>[0] = {
- model: this.createModel(config.model || this.defaultModel),
+ model: this.createModel(model),
system: this.buildSystemMessage(systemPrompt),
messages: coreMessages,
maxOutputTokens: maxTokens,
// Reject any system message smuggled into `messages` (prompt injection guard).
allowSystemInMessages: false,
providerOptions: {
- anthropic: {
- thinking: {
- type: "enabled",
- budgetTokens: thinkingBudget
- }
- }
+ anthropic: { thinking }
}
};