Add cached tokens to Unified API response#136412
Merged
Conversation
- Updated OpenAiUnifiedStreamingProcessor to parse optional cached_tokens from prompt_tokens_details - Added ConstructingObjectParser for prompt_tokens_details nested object - Added tests for both cached tokens present and absent scenarios - Maintains backward compatibility with responses without cached tokens
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR adds support for cached tokens in the Unified Chat Completion API response to align with OpenAI's specification. The implementation adds a cachedTokens field to the Usage record and includes optional prompt_tokens_details serialization when cached token information is available.
- Added
cachedTokensfield to theUsagerecord with proper serialization support - Updated JSON parsing to handle
prompt_tokens_details.cached_tokensfield - Added comprehensive test coverage for both scenarios with and without cached tokens
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| StreamingUnifiedChatCompletionResults.java | Added cachedTokens field to Usage record and updated serialization logic |
| OpenAiUnifiedStreamingProcessor.java | Updated parser to handle prompt_tokens_details with cached_tokens field |
| StreamingUnifiedChatCompletionResultsTests.java | Added test coverage for cached tokens serialization scenarios |
| OpenAiUnifiedStreamingProcessorTests.java | Added test coverage for usage parsing with and without cached tokens |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
- Updated OpenAiServiceTests.testUnifiedCompletionInfer to expect cached_tokens:0 in response - Updated HuggingFaceServiceTests.testUnifiedCompletionInfer to expect cached_tokens:0 in response - Including cached_tokens:0 provides meaningful information (caching available but not used) - Distinguishes from null (no caching information available)
Collaborator
|
Hi @jaybcee, I've created a changelog YAML for you. |
Contributor
jonathan-buttner
left a comment
There was a problem hiding this comment.
Looking good, left a few comments about the transport version check.
...va/org/elasticsearch/xpack/core/inference/results/StreamingUnifiedChatCompletionResults.java
Outdated
Show resolved
Hide resolved
...va/org/elasticsearch/xpack/core/inference/results/StreamingUnifiedChatCompletionResults.java
Outdated
Show resolved
Hide resolved
...va/org/elasticsearch/xpack/core/inference/results/StreamingUnifiedChatCompletionResults.java
Outdated
Show resolved
Hide resolved
Wrap cachedTokens read/write operations in transport version checks to ensure backward compatibility with older nodes that haven't been upgraded yet.
Collaborator
|
Pinging @elastic/ml-core (Team:ML) |
jonathan-buttner
approved these changes
Oct 22, 2025
fzowl
pushed a commit
to voyage-ai/elasticsearch
that referenced
this pull request
Nov 3, 2025
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.
This PR adds support for cached tokens in the Unified Chat Completion API response, allowing users to track prompt caching from EIS and OpenAI services.
Testing
OpenAiUnifiedStreamingProcessorTests.java: Added comprehensive tests for both scenarios (with and without cached tokens)Response Format
When cached tokens are present, the response includes:
{ "completion_tokens": 150, "prompt_tokens": 55, "total_tokens": 205, "prompt_tokens_details": { "cached_tokens": 20 } }The
prompt_tokens_detailsobject is optional and only appears when cached token information is available from the EIS service, following the OpenAI specification.I did not implement this for non open-ai providers. We don't really need this field at the moment, it just helps with O11y tools like Phoenix.