feat: serialize prompt-cache creation tokens on LlmResponse - #6821
Open
gurjot-05 wants to merge 1 commit into
Open
feat: serialize prompt-cache creation tokens on LlmResponse#6821gurjot-05 wants to merge 1 commit into
gurjot-05 wants to merge 1 commit into
Conversation
Commit d0b33a0 extracts cache-creation (write) tokens and attaches them to the GenerateContentResponseUsageMetadata object via object.__setattr__ so they reach OpenTelemetry. But google.genai's usage type forbids extra fields, so that attribute is dropped by model_dump() and never appears in serialized events -- the dev UI, persisted sessions, and SSE consumers cannot see cache-write tokens. Surface the same count on a serializable LlmResponse.cache_creation_token_count field (inherited by Event, emitted as cacheCreationTokenCount, omitted when None). It is the cache-write counterpart to usage_metadata.cached_content_token_count. Populated from the already-extracted value in the Anthropic and LiteLLM paths (streaming and non-streaming); the OpenTelemetry path is left unchanged.
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.
What
Adds a serializable
LlmResponse.cache_creation_token_countfield (inherited byEvent, emitted ascacheCreationTokenCount) for prompt-cache write tokens.Why
#5835 / d0b33a0 extract cache-creation tokens and attach them to
GenerateContentResponseUsageMetadataviaobject.__setattr__for OpenTelemetry. Butgoogle.genai's usage type isextra='forbid', so that attribute is dropped bymodel_dump()— it never reaches serialized events. So the Dev UI, persisted sessions, and SSE consumers can't see cache-write (only OTel / in-memorygetattrcan).This surfaces the same value on the serializable
LlmResponse— the cache-write counterpart tousage_metadata.cached_content_token_count(read) — next to the existing top-levelcache_metadatafield. (It can't live insideusage_metadata: an extra key there makes event reload raise a ValidationError, breaking session persistence.)Changes
llm_response.py: newcache_creation_token_count: Optional[int]field.anthropic_llm.py/lite_llm.py: populate it from the already-extracted value (streaming + non-streaming). OTel path unchanged.Companion PR
Dev-UI counterpart that displays this value: google/adk-web#492 — please merge together.