fix(session): recover cache tokens from openai-compatible metadata usage fallback#36861
Open
ulises-jeremias wants to merge 3 commits into
Open
Conversation
…age fallback
Custom baseURL providers using openai-compatible SDK may report cache tokens
through provider metadata (e.g. prompt_tokens_details.cached_tokens) rather
than the standard usage.{cacheReadInputTokens,cacheWriteInputTokens} fields.
Without the metadata fallback, these providers report zero cache tokens for
subagent sessions.
The fix extends getUsage to probe provider metadata for cache read/write
token counts when the standard usage fields are missing. Existing metadata
fallbacks (anthropic, vertex, bedrock, venice) are preserved.
Closes anomalyco#30663
Contributor
|
The following comment was made by an LLM, it may be inaccurate: Potential duplicate found:
|
Contributor
Author
|
Thanks for flagging. These PRs are complementary, not conflicting:
They handle different stages of the pipeline. If #36752 merges first, #36861 still adds value for the metadata path (especially |
6 tasks
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.
Issue for this PR
Closes #30663
Type of change
What does this PR do?
Custom baseURL providers using the openai-compatible SDK may report cache tokens through provider metadata (e.g.
prompt_tokens_details.cached_tokens) rather than the standardusage.cacheReadInputTokens/usage.cacheWriteInputTokensfields. Without the metadata fallback, these providers report zero cache tokens for subagent sessions, leading to incorrect cost reporting.The fix extends
getUsageto probe provider metadata for cache read/write token counts when the standard usage fields are missing. It adds helper functionsmetadataNumber,metadataRecord, andmetadataUsageto safely navigate deeply nested metadata structures. Existing metadata fallbacks (anthropic, vertex, bedrock, venice) are preserved.How did you verify your code works?
bun test test/session/llm.test.ts— 30 tests pass including 3 new tests for metadata cache token extractionbun typecheck— cleanScreenshots / recordings
N/A (no UI changes)
Checklist