Skip to content

fix(anthropic): include cache tokens in total usage - #6771

Closed
gambletan wants to merge 1 commit into
crewAIInc:mainfrom
gambletan:fix/anthropic-cache-token-total
Closed

fix(anthropic): include cache tokens in total usage#6771
gambletan wants to merge 1 commit into
crewAIInc:mainfrom
gambletan:fix/anthropic-cache-token-total

Conversation

@gambletan

Copy link
Copy Markdown

Summary

  • include Anthropic cache-read and cache-creation tokens in provider-level total usage
  • preserve the cache-inclusive total through shared UsageMetrics normalization and Flow aggregation
  • keep OpenAI-style cached prompt accounting unchanged by applying the extra counters only to the Anthropic input_tokens shape

Problem

Anthropic reports cache reads and cache writes separately from input_tokens. CrewAI exposed those counters but calculated total_tokens from input_tokens + output_tokens only, substantially undercounting cached workloads. The shared UsageMetrics normalizer then recomputed the same incomplete total, so public LLM summaries and Flow metrics were affected as well.

Testing

  • 76 Anthropic provider and Flow usage tests passed
  • 29 usage-shape and LLM usage-event tests passed
  • ruff check passed for all changed files
  • ruff format --check passed for changed source files
  • git diff --check passed
  • two independent Codex review passes; the first identified the shared normalizer gap, which is covered by the final tests

Fixes #6768

@gambletan

Copy link
Copy Markdown
Author

Closing as a duplicate of #6770, which was opened first and covers the same issue and end-to-end UsageMetrics path. This PR was created after a final duplicate-check/create command did not abort when the issue timeline returned #6770. Thanks @Anai-Guo for the thorough fix.

@gambletan gambletan closed this Aug 2, 2026
@coderabbitai

coderabbitai Bot commented Aug 2, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: ca5349df-3a00-49ed-bce9-3b573c43437e

📥 Commits

Reviewing files that changed from the base of the PR and between c8f441c and 7d5bbd0.

📒 Files selected for processing (4)
  • lib/crewai/src/crewai/llms/providers/anthropic/completion.py
  • lib/crewai/src/crewai/types/usage_metrics.py
  • lib/crewai/tests/llms/anthropic/test_anthropic.py
  • lib/crewai/tests/test_flow_usage_metrics.py

📝 Walkthrough

Walkthrough

Changes

Anthropic usage accounting

Layer / File(s) Summary
Include cache tokens in provider totals
lib/crewai/src/crewai/llms/providers/anthropic/completion.py, lib/crewai/src/crewai/types/usage_metrics.py
Anthropic totals now include input, output, cache-read, and cache-creation tokens. Normalized usage preserves the cache counters.
Validate cache token accounting
lib/crewai/tests/llms/anthropic/test_anthropic.py, lib/crewai/tests/test_flow_usage_metrics.py
Anthropic fixtures include cache fields. Tests verify extraction, aggregate totals, and normalized usage metrics.

Suggested reviewers: lorenzejay

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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.

Conversational Flow golden use case improvements - Anthropic total_tokens omits cache read/write tokens, undercounting cost

1 participant