Repository navigation
test: cover cache-aware effective cost; fix Anthropic cache-read field name - #47
Open
giladsamuels-cognition wants to merge 2 commits into
Open
giladsamuels-cognition wants to merge 2 commits into
giladsamuels-cognition wants to merge 2 commits into
Conversation
…ut_tokens Co-Authored-By: Gilad Samuels <gilad.samuels@cognition.ai>
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
Co-Authored-By: Gilad Samuels <gilad.samuels@cognition.ai>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds unit tests for
TrackAPIPricingMixin.get_effective_costand its Anthropic/OpenAI variants using non-zero cached tokens, and fixes the bug the tests caught. Closes code-scan findingsfind-ba28615afd004752ba3b8515683639d9.Production change (Anthropic path only): the code read cache-read tokens from a field that doesn't exist, so the
0default applied and cache-read cost was always dropped fromeffective_cost.anthropic.types.Usagehascache_read_input_tokens,cache_creation_input_tokens,input_tokens,output_tokensand nocache_input_tokens. Both cache fields areOptional[int], so a null value now falls back to 0.The
create_mock_anthropic_responsehelper intests/llm/test_response_api.pyalso set the old field name. It now sets the real one, otherwise the MagicMock would return a mock for the missing attribute.New tests in
tests/llm/test_tracking.pybuild usage objects with the real SDK types (anthropic.types.Usage,openaiCompletionUsage/ResponseUsage) and fixed prices (input_cost=2e-6,output_cost=1e-5). They assert exact costs:1.4e-3. Also checks that cache reads are billed at the discount, and that missing or null (None) cache fields fall back to 0.prompt_tokens_detailspresent): 1000 prompt tokens, 600 of them cached (0.5x), + 100 output →2.4e-3. Also coversprompt_tokens_details=None.input_tokens_detailsbranch): same numbers →2.4e-3.usage is Noneor missing →0.0; unsupported provider (openrouter, unknown,None) →0.0;litellmdelegates tocompletion_cost(patched).TrackAPIPricingMixin.__call__:stats["effective_cost"],usage_cached_tokensandn_api_callsadd up across calls. The trackercoststill bills all prompt tokens at the full price.Without the field-name fix, the Anthropic cache-read tests fail. With it,
uv run pytest tests/llmpasses andblack --check src/is clean.Devin-Org: engineering
Link to Devin session: https://app.devin.ai/sessions/190c921121af47b2956a84e336f23eef
Open in Devin Desktop: https://app.devin.ai/desktop/session/190c921121af47b2956a84e336f23eef?variant=devin
Requested by: @giladsamuels-cognition