fix: [AIPLAT-1489]: stop double-counting LiteLLM cache tokens as gen_ai input - #22
Open
shreyas-n-harness wants to merge 3 commits into
Open
fix: [AIPLAT-1489]: stop double-counting LiteLLM cache tokens as gen_ai input#22shreyas-n-harness wants to merge 3 commits into
shreyas-n-harness wants to merge 3 commits into
Conversation
…r LiteLLM LiteLLM prompt_tokens is cache-inclusive. Copying it onto gen_ai.usage.input_tokens while also exporting cache read/write double-counted prompt cache against GCP-style disjoint buckets. Anthropic-shaped input_tokens is already exclusive and is left unchanged. Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Current litellm + pydantic cannot rebuild Message on 3.10, which broke CI. Use a SimpleNamespace fake for patched completions and skip tests that call real completion/mock_response on 3.10. Co-authored-by: Cursor <cursoragent@cursor.com>
| if input_tokens is None: | ||
| cache_read = _get_value(usage, "cache_read_input_tokens") | ||
| if cache_read is None: | ||
| cache_read = _get_value(prompt_details, "cached_tokens") |
Collaborator
There was a problem hiding this comment.
I believe this should be handled at backend. SDK should generate the info that is needed.
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
prompt_tokensis cache-inclusive. The LiteLLM instrumentor copied it togen_ai.usage.input_tokensand setgen_ai.usage.cache_read.input_tokens/cache_creation.input_tokens, so GenAI traces inflated input the same way LMS Prometheusllm_tokens{tokenType=request}did._set_response_attributesnow reports uncached input:max(prompt_tokens - cache_read - cache_creation, 0). Stream and non-stream both use this helper.input_tokens(noprompt_tokens) is already cache-exclusive; those spans are not subtracted.response.usageis not mutated, so callers that price withcost_per_token()still see inclusiveprompt_tokens.Test plan
pytest test/instrumentation/litellm/litellm_instrumentation_test.pygen_ai.usage.input_tokensis exclusive; cache series still present;total_tokensunchanged (LiteLLM total).input_tokensleft as-is whenprompt_tokensis absent.input_tokensstill equalsprompt_tokens.Made with Cursor