fix(anthropic): include cache tokens in totals - #6801
Conversation
|
AI assistance disclosure: this contribution was developed with AI assistance and personally reviewed and tested. GitHub denied my attempt to add the required llm-generated label because external contributors do not have AddLabelsToLabelable permission. Maintainers, please apply the llm-generated label. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThe native Anthropic token extractor now includes cache-read and cache-creation tokens in ChangesAnthropic token usage
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
lib/crewai/tests/llms/anthropic/test_anthropic.py (1)
1667-1668: 🗄️ Data Integrity & Integration | 🔵 Trivial | ⚡ Quick winExercise the public Anthropic call path in this test.
The test invokes
_extract_anthropic_token_usageand_track_token_usage_internaldirectly. It therefore bypasses the provider path that must extract and record cache-inclusive usage. Patchllm._client.messages.create, callllm.call("Hello"), and assert the resultingUsageMetrics.As per coding guidelines, tests under
**/tests/**/*.pymust focus on behavior rather than implementation details.Suggested test adjustment
- llm._track_token_usage_internal(usage) + with patch.object( + llm._client.messages, "create", return_value=mock_response + ): + assert llm.call("Hello") == "test response"🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@lib/crewai/tests/llms/anthropic/test_anthropic.py` around lines 1667 - 1668, Update the test around _track_token_usage_internal to exercise the public Anthropic path instead of calling internal extraction or tracking helpers directly: patch llm._client.messages.create with a suitable response, invoke llm.call("Hello"), and assert the resulting UsageMetrics includes the expected cache-inclusive usage.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@lib/crewai/tests/llms/anthropic/test_anthropic.py`:
- Around line 1667-1668: Update the test around _track_token_usage_internal to
exercise the public Anthropic path instead of calling internal extraction or
tracking helpers directly: patch llm._client.messages.create with a suitable
response, invoke llm.call("Hello"), and assert the resulting UsageMetrics
includes the expected cache-inclusive usage.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 7d277841-dc2b-44ab-8e13-69f1ecd45f5e
📒 Files selected for processing (2)
lib/crewai/src/crewai/llms/providers/anthropic/completion.pylib/crewai/tests/llms/anthropic/test_anthropic.py
|
Addressed the review suggestion in |
|
Hey @Linxiushen, I think it would be better to add a doc sharing how exactly we are calculating the total tokens, Just a one liner would be fine at this moment. |
|
Added the requested one-line calculation note in 5f25218. It documents that Anthropic normalized input includes uncached, cache-read, and cache-creation input tokens, and clarifies that the two cache metrics are separate breakdowns rather than values to add to total_tokens again. |
|
I don't think so that it's just a case for anthropic, I think it general way we have decided for each provider, can you check and update ? |
|
Checked the shared normalization path and updated the note in a9a70d0. It now states the provider-independent rule: |
Summary
Closes #6788
Testing
AI assistance
Developed with AI assistance and reviewed and tested by the contributor. External contributors cannot apply labels in this repository, so I have requested the required llm-generated label in a comment below.