Skip to content

[codex] fix anthropic thinking max_tokens floor#36

Merged
clifton merged 1 commit intomainfrom
x/fix-anthropic-thinking-max-tokens
Feb 13, 2026
Merged

[codex] fix anthropic thinking max_tokens floor#36
clifton merged 1 commit intomainfrom
x/fix-anthropic-thinking-max-tokens

Conversation

@clifton
Copy link
Owner

@clifton clifton commented Feb 12, 2026

Summary

This PR fixes issue #26 where Anthropic requests with Claude extended thinking could fail with:

Invalid request: max_tokens must be greater than thinking.budget_tokens

User impact

When users enabled thinking_level on Claude 4.x models without manually setting max_tokens, rstructor sent max_tokens=1024 by default while thinking budgets can be 1024/2048/4096/8192. That could produce immediate API errors for Minimal, Low, Medium, and High thinking levels.

Root cause

The Anthropic request builders hard-coded max_tokens to self.config.max_tokens.unwrap_or(1024) and did not enforce Anthropic's requirement that thinking.budget_tokens < max_tokens.

Anthropic docs specify this constraint in the extended thinking API docs:
https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking#the-thinking-parameter

Fix

  • Added shared helper effective_max_tokens(configured_max_tokens, thinking_config) in src/backend/anthropic.rs.
  • Enforced max_tokens > thinking.budget_tokens whenever thinking is enabled.
  • Applied this logic to both:
    • structured output path (materialize_internal)
    • raw generation path (generate_with_metadata)
  • Added a warning log when max_tokens is auto-adjusted.
  • Updated thinking_level docs to state this auto-adjust behavior.

Tests

Added new unit tests in src/backend/anthropic.rs covering:

  • default behavior without thinking
  • configured behavior without thinking
  • auto-adjust when default max tokens is too low
  • auto-adjust when configured max tokens equals budget
  • preserving configured value when already valid
  • saturation edge case for extreme budget values

Validation

  • cargo fmt --all
  • cargo clippy --all-targets --all-features -- -D warnings
  • cargo test --all-features

Closes #26.

@clifton clifton marked this pull request as ready for review February 12, 2026 22:35
@clifton clifton merged commit 750d700 into main Feb 13, 2026
8 checks passed
@clifton clifton deleted the x/fix-anthropic-thinking-max-tokens branch February 13, 2026 00:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Claude extended thinking: max_tokens must be > thinking.budget_tokens

1 participant