fix: cap thinking.budget_tokens when >= max_tokens#143
Open
flwrenn wants to merge 1 commit intogriffinmartin:mainfrom
Open
fix: cap thinking.budget_tokens when >= max_tokens#143flwrenn wants to merge 1 commit intogriffinmartin:mainfrom
flwrenn wants to merge 1 commit intogriffinmartin:mainfrom
Conversation
OpenCode may set thinking.budget_tokens equal to (or greater than) max_tokens for custom models that aren't in its built-in registry. The Anthropic API requires max_tokens > thinking.budget_tokens and rejects such requests with a 400 error. Cap budget_tokens to 80% of max_tokens when the constraint is violated, matching the ratio used by OpenCode's own Anthropic provider when it correctly configures thinking.
There was a problem hiding this comment.
Pull request overview
This PR adds a defensive transformation to avoid Anthropic API 400s when thinking.budget_tokens is configured to be greater than or equal to max_tokens (a situation observed with custom model IDs outside OpenCode’s built-in model registry).
Changes:
- Parse and surface
max_tokensintransformBody’s request-body shape. - Cap
thinking.budget_tokenstoMath.floor(max_tokens * 0.8)whenbudget_tokens >= max_tokensto satisfy the API’s strict inequality requirement. - Add three unit tests covering equal/greater-than/no-op scenarios for the cap logic.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/transforms.ts | Adds runtime guard to ensure max_tokens > thinking.budget_tokens by capping the budget when invalid. |
| src/transforms.test.ts | Adds targeted regression tests for the new capping behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Owner
|
Thanks, will try to take a look at this tonight. |
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.
Summary
thinking.budget_tokensto 80% ofmax_tokenswhen OpenCode sendsbudget_tokens >= max_tokens, which the Anthropic API rejects with a 400 errorclaude-opus-4-6) not in OpenCode's built-in registry, wheremax_tokensandbudget_tokensmay be set to the same valuetransformBodyProblem
When using
claude-opus-4-6(or other custom model IDs) through OpenCode, the Go binary may setthinking.budget_tokensequal tomax_tokens. The Anthropic API requires strict inequality (max_tokens > thinking.budget_tokens) and rejects these requests:Fix
In
transformBody, after the existing model override logic, check ifbudget_tokens >= max_tokensand cap it toMath.floor(max_tokens * 0.8). The 0.8 ratio matches what OpenCode's own Anthropic provider uses when it correctly configures thinking for built-in models.Tests
Three new test cases covering:
budget_tokens == max_tokens(the observed failure case)budget_tokens > max_tokensbudget_tokens < max_tokens(no-op, left unchanged)