Skip to content

Conversation

@shelvick
Copy link
Contributor

@shelvick shelvick commented Jan 4, 2026

Summary

  • Fixes incorrect cache token clamping for Anthropic API responses
  • Anthropic's input_tokens only contains NEW tokens (excludes cached), while OpenAI's prompt_tokens includes cached tokens
  • The old code clamped cache values to input_tokens, causing cached_tokens: 5484 to become cached_tokens: 12

Solution

Detects API format based on field presence (not provider names):

  • OpenAI format (has prompt_tokens_details.cached_tokens): input INCLUDES cached, so clamp cached <= input
  • Anthropic format (has cache_read_input_tokens): input EXCLUDES cached, no clamping needed (values are additive)

Test plan

  • All 1885 existing tests pass
  • Added test for Anthropic cache semantics with real-world example
  • Updated cost calculation tests for both OpenAI and Anthropic semantics

Anthropic's input_tokens only contains NEW tokens (excludes cached),
while OpenAI's prompt_tokens includes cached tokens. The old code
incorrectly clamped cache values to input_tokens, causing cached_tokens
to be truncated (e.g., 5484 to 12).

The fix detects API format based on field presence:
- OpenAI format (has prompt_tokens_details.cached_tokens): clamp cached <= input
- Anthropic format (has cache_read_input_tokens): no clamping (values are additive)

Co-authored-by: Scott Helvick <4016577+shelvick@users.noreply.github.com>
Amp-Thread-ID: https://ampcode.com/threads/T-019bc40f-f436-712b-89dd-e28a616bc850
Co-authored-by: Amp <amp@ampcode.com>
@mikehostetler mikehostetler force-pushed the fix/clamp-tokens-anthropic branch from be5533e to 49151e2 Compare January 16, 2026 00:11
@mikehostetler mikehostetler merged commit 7c2c89c into agentjido:main Jan 16, 2026
6 checks passed
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.

2 participants