chore(ai-proxy): add support of recording the cache token count in metrics and logging#14802
Open
git-hulk wants to merge 4 commits intoKong:masterfrom
Open
chore(ai-proxy): add support of recording the cache token count in metrics and logging#14802git-hulk wants to merge 4 commits intoKong:masterfrom
git-hulk wants to merge 4 commits intoKong:masterfrom
Conversation
…emini Currently, ai-proxy only supports the prompt/completions token without the prompt cache tokens, which might be helpful for users to observe the cache hit ratio and improve the performance. This PR introduce `prompt_cache_tokens` to record the cache for Gemini and OpenAI.
There was a problem hiding this comment.
Pull request overview
This PR adds support for recording prompt cache token counts for Gemini and OpenAI LLM providers, enabling users to observe cache hit ratios and improve performance monitoring.
Key Changes:
- Added
prompt_cache_tokensfield to the metrics schema and analytics serialization - Implemented cache token extraction for Gemini (via
cachedContentTokenCount) and OpenAI (viaprompt_tokens_details.cache_tokens) - Updated test expectations to include
prompt_cache_tokens = 0in expected chat statistics
Reviewed changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| kong/llm/plugin/observability.lua | Added llm_prompt_cache_tokens_count to the metrics schema |
| kong/llm/drivers/gemini.lua | Introduced extract_usage helper function to extract cache tokens from Gemini's cachedContentTokenCount |
| kong/llm/adapters/gemini.lua | Added prompt_cache_tokens and total_tokens extraction from Gemini response metadata |
| kong/llm/drivers/shared.lua | Added cache tokens to analytics container and observability metrics |
| kong/llm/plugin/shared-filters/parse-json-response.lua | Added cache token extraction for both native formats (Gemini) and OpenAI format (via prompt_tokens_details.cache_tokens) |
| kong/llm/plugin/shared-filters/serialize-analytics.lua | Included prompt_cache_tokens in serialized usage analytics |
| kong/plugins/ai-request-transformer/filters/transform-request.lua | Added llm_prompt_cache_tokens_count to filter output schema and context |
| kong/plugins/ai-response-transformer/filters/transform-response.lua | Added llm_prompt_cache_tokens_count to filter output schema and context |
| kong/reports.lua | Added AI_PROMPT_CACHE_TOKENS_COUNT_KEY constant and tracking for cache token counts |
| kong/plugins/prometheus/exporter.lua | Added Prometheus metric collection for prompt_cache_tokens |
| spec/03-plugins/38-ai-proxy/02-openai_integration_spec.lua | Updated expected stats to include prompt_cache_tokens = 0 |
| spec/03-plugins/38-ai-proxy/09-streaming_integration_spec.lua | Updated expected stats to include prompt_cache_tokens = 0 |
| spec/03-plugins/38-ai-proxy/11-gemini_integration_spec.lua | Updated expected stats to include prompt_cache_tokens = 0 |
| spec/03-plugins/39-ai-request-transformer/02-integration_spec.lua | Updated expected stats to include prompt_cache_tokens = 0 |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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
Currently, ai-proxy only supports the prompt/completions token, not the prompt cache tokens, which might help users observe the cache hit ratio and improve performance.
This PR introduces
prompt_cache_tokensto record the cache for Gemini and OpenAI:Checklist
changelog/unreleased/kongorskip-changeloglabel added on PR if changelog is unnecessary. README.md