Repository navigation
fix(session): recover cache tokens from openai-compatible metadata usage fallback - #36861
ulises-jeremias wants to merge 3 commits into
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
|
The following comment was made by an LLM, it may be inaccurate: Potential duplicate found:
|
|
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 |
|
Automated PR Cleanup Thank you for contributing to opencode. Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions. This PR was closed because it matched the following cleanup criteria:
PRs created within the last month are not affected by this cleanup. If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate. Thanks again for taking the time to contribute. |
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