Skip to content

LLM Wiki is a cross-platform desktop application that turns your documents into an organized, interlinked knowledge base — automatically. Instead of traditional RAG (retrieve-and-answer from scratch every time), the LLM incrementally builds and maintains a persistent wiki from your sources。

TypeScript

feat: derive Anthropic max_tokens from context budget instead of hardcoding 4096

Dongmin, Yu3580282bfe1a
diff --git a/src/lib/__tests__/llm-providers.test.ts b/src/lib/__tests__/llm-providers.test.ts
index 385c0d61..7ec6930e 100644
--- a/src/lib/__tests__/llm-providers.test.ts
+++ b/src/lib/__tests__/llm-providers.test.ts
@@ -42,10 +42,11 @@ describe("MiniMax Provider", () => {
expect(body.stream).toBe(true)
})
- it("includes max_tokens (required by Anthropic wire)", () => {
+ it("includes max_tokens derived from context budget (required by Anthropic wire)", () => {
const cfg = getProviderConfig(makeConfig())
const body = cfg.buildBody([]) as Record<string, unknown>
- expect(body.max_tokens).toBe(4096)
+ // 204 800 chars × 15% reserve ÷ 3 chars-per-token = 10 240; capped at 16 384
+ expect(body.max_tokens).toBe(10240)
})
it("carries the model in the body", () => {
diff --git a/src/lib/llm-providers.ts b/src/lib/llm-providers.ts
index 9285bcf8..5a7c5bf8 100644
--- a/src/lib/llm-providers.ts
+++ b/src/lib/llm-providers.ts
@@ -1,4 +1,5 @@
import type { LlmConfig, ReasoningConfig } from "@/stores/wiki-store"
+import { computeContextBudget } from "@/lib/context-budget"
import {
AZURE_OPENAI_API_VERSION,
buildAzureOpenAiUrl,
@@ -801,6 +802,15 @@ function buildGoogleBody(
export function getProviderConfig(config: LlmConfig): ProviderConfig {
const { provider, apiKey, model, ollamaUrl, customEndpoint } = config
+ // Default max_tokens for Anthropic-wire providers, derived from the
+ // configured context window rather than a hardcoded 4096. Converts the
+ // character-based responseReserve to tokens at ~3 chars/token and caps
+ // at 16 384 to stay within typical per-request limits. Explicit
+ // overrides from callers (e.g. max_tokens: 300 for routing decisions)
+ // always take precedence because they are spread after this value.
+ const { responseReserve } = computeContextBudget(config.maxContextSize)
+ const anthropicBudgetTokens = Math.min(16_384, Math.floor(responseReserve / 3))
+
switch (provider) {
case "openai":
return {
@@ -824,7 +834,7 @@ export function getProviderConfig(config: LlmConfig): ProviderConfig {
buildBody: (messages, overrides) => {
assertMiniMaxImageSupport(url, model, messages)
return {
- ...buildAnthropicBodyWithReasoning(config, messages, overrides),
+ ...buildAnthropicBodyWithReasoning(config, messages, { max_tokens: anthropicBudgetTokens, ...overrides }),
model,
}
},
@@ -907,7 +917,7 @@ export function getProviderConfig(config: LlmConfig): ProviderConfig {
buildBody: (messages, overrides) => {
assertMiniMaxImageSupport(url, model, messages)
return {
- ...buildAnthropicBodyWithReasoning(config, messages, overrides),
+ ...buildAnthropicBodyWithReasoning(config, messages, { max_tokens: anthropicBudgetTokens, ...overrides }),
model,
}
},
@@ -939,7 +949,7 @@ export function getProviderConfig(config: LlmConfig): ProviderConfig {
buildBody: (messages, overrides) => {
assertMiniMaxImageSupport(url, model, messages)
return {
- ...buildAnthropicBodyWithReasoning(config, messages, overrides),
+ ...buildAnthropicBodyWithReasoning(config, messages, { max_tokens: anthropicBudgetTokens, ...overrides }),
model,
}
},