Skip to content

Cover cache-aware effective cost with unit tests and fix Anthropic cache-read field - #32

Open
devin-ai-integration[bot] wants to merge 2 commits into
mainfrom
devin/1789677923-effective-cost-tests
Open

devin-ai-integration[bot] wants to merge 2 commits into
mainfrom
devin/1789677923-effective-cost-tests

Conversation

@devin-ai-integration

Copy link
Copy Markdown

Summary

get_effective_cost had no test asserting its output: the mocked cost tests hard-code cached_tokens = 0 and only assert token totals, so the cache-read/cache-write arithmetic was multiplied by zero in every non-pricy test. This adds unit tests that assert exact effective-cost floats for both providers with non-zero cache tokens, and fixes the bug the tests immediately exposed.

Bug: the Anthropic branch read cache-read tokens from a field the Anthropic API does not emit, so all cache-read cost was silently dropped:

-cache_read_tokens = getattr(usage, "cache_input_tokens", 0)
+cache_read_tokens = getattr(usage, "cache_read_input_tokens", None)
+if cache_read_tokens is None:
+    cache_read_tokens = getattr(usage, "cache_input_tokens", 0)

The old name is kept as a fallback so any caller/response still using it keeps working.

New tests in tests/llm/test_tracking.py drive a tiny _CostModel(TrackAPIPricingMixin) with fixed input_cost/output_cost (no provider, no pricing fetch) and cover:

  • Anthropic with non-zero cache_read_input_tokens + cache_creation_input_tokens, and without cache tokens
  • OpenAI chat-completion branch (prompt_tokens_details) with cached tokens and with prompt_tokens_details = None
  • OpenAI responses branch (input_tokens_details) with cached tokens
  • usage is None → 0.0, unsupported provider → 0.0, litellm → completion_cost
  • __call__ accumulating stats["effective_cost"] across two calls

Plain MagicMock cannot be used for the OpenAI dispatch (hasattr(usage, "prompt_tokens_details") is always true on a mock), hence the small _FakeUsage helper, which also supports dict(usage) as TrackAPIPricingMixin.__call__ requires.

Devin-Org: engineering

Link to Devin session: https://app.devin.ai/sessions/25cba9f25dfd4bd6985865c065a92a08
Open in Devin Desktop: https://app.devin.ai/desktop/session/25cba9f25dfd4bd6985865c065a92a08?variant=devin

@devin-ai-integration

Copy link
Copy Markdown
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

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.

0 participants