feat: derive Anthropic max_tokens from context budget instead of hardcoding 4096#559
Merged
nashsu merged 2 commits intoJul 15, 2026
Conversation
…coding 4096 The response reserve computed by computeContextBudget() was a passive reservation — it sized the context budget correctly but never enforced a matching max_tokens on the wire. Anthropic API calls defaulted to 4096 tokens regardless of the configured context window. Now getProviderConfig() computes anthropicBudgetTokens from the same 15% response reserve (responseReserve / 3 chars-per-token, capped at 16 384) and passes it as the default max_tokens for all three Anthropic- wire providers (anthropic, minimax, custom/anthropic_messages). Explicit overrides from callers (e.g. max_tokens: 300 for routing decisions) are spread after the budget value so they still take precedence. Effect on a 200K-char context: default max_tokens 4096 → 10 240.
The passive-reservation comment said passing max_tokens was an unfinished follow-up; getProviderConfig() now derives it from responseReserve, so update the doc to match.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
getProviderConfig()now derives the default Anthropicmax_tokensfrom the configured context window instead of letting the wire request fall back to 4096.This completes a follow-up that
context-budget.tsalready documents as pending:Why
computeContextBudget()sizes a 15% response reserve so the model has room to answer, but that reserve was passive — it only capped how full we packed the context. The actual Anthropic request still defaulted tomax_tokens: 4096regardless of the configured context window, so a large context could be assembled but the response was silently clipped to 4096 tokens.How
anthropicBudgetTokensfrom the sameresponseReserve(chars → tokens at ~3 chars/token), capped at 16 384 to stay within typical per-request limits.max_tokensfor all three Anthropic-wire providers (anthropic,minimax,custom/anthropic_messages).max_tokens: 300for routing decisions) are spread after the default, so they still take precedence.context-budget.tsdoc comment to reflect that the follow-up is done.Effect on a 200K-char context: default
max_tokens4096 → 10 240.Testing
src/lib/__tests__/llm-providers.test.ts— 50 pass / 0 fail (updated to assert the derived default and that overrides win).