Skip to content

fix(session): recover cache tokens from openai-compatible metadata usage fallback#36861

Open
ulises-jeremias wants to merge 3 commits into
anomalyco:devfrom
ulises-jeremias:fix/opencode-subagent-cache-token-accounting-v2
Open

fix(session): recover cache tokens from openai-compatible metadata usage fallback#36861
ulises-jeremias wants to merge 3 commits into
anomalyco:devfrom
ulises-jeremias:fix/opencode-subagent-cache-token-accounting-v2

Conversation

@ulises-jeremias

Copy link
Copy Markdown
Contributor

Issue for this PR

Closes #30663

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

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 standard usage.cacheReadInputTokens / usage.cacheWriteInputTokens fields. Without the metadata fallback, these providers report zero cache tokens for subagent sessions, leading to incorrect cost reporting.

The fix extends getUsage to probe provider metadata for cache read/write token counts when the standard usage fields are missing. It adds helper functions metadataNumber, metadataRecord, and metadataUsage to 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 extraction
  • bun typecheck — clean

Screenshots / recordings

N/A (no UI changes)

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

…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
@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Potential duplicate found:

@ulises-jeremias

Copy link
Copy Markdown
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 cacheReadInputTokens from prompt_tokens_details.cached_tokens, which #36752 doesn't handle).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Custom baseURL providers report zero prompt cache tokens for subagent sessions

1 participant