Skip to content

fix(usage): expose cache write accounting semantics#4193

Open
seakee wants to merge 5 commits into
router-for-me:devfrom
seakee:fix/openai-cache-write-usage
Open

fix(usage): expose cache write accounting semantics#4193
seakee wants to merge 5 commits into
router-for-me:devfrom
seakee:fix/openai-cache-write-usage

Conversation

@seakee

@seakee seakee commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Summary

Parse OpenAI Chat Completions cache creation tokens and expose optional cache-input accounting semantics to usage consumers.

The accounting metadata is backward compatible: consumers can use cache_input_mode when present and continue inferring provider semantics when older queue producers omit it.

Changes

  • Recognize prompt_tokens_details.cache_write_tokens and retain the Responses API fallback.
  • Mark OpenAI, Gemini, and Interactions cache tokens as included in input tokens.
  • Mark Claude cache read and creation tokens as separate from input tokens.
  • Publish the optional tokens.cache_input_mode field through the Redis usage queue.
  • Preserve existing token fields and the zero-value behavior for SDK consumers.

Testing

  • go test -race ./internal/runtime/executor/helps ./internal/redisqueue ./sdk/cliproxy/usage
  • go build -o /tmp/cpa-cache-accounting-server ./cmd/server
  • go test ./... — the unchanged catalog test currently fails TestModelsWithClientVersionReturnsCodexCatalog with custom priority = 143, want 129; the modified packages pass.

Compatibility

  • The new field is optional and omitted when unknown.
  • Existing plugins and queue consumers remain compatible.
  • CPA Manager Plus does not require this PR; it supports current released CPA payloads and treats this metadata as an accuracy enhancement.

Related

Refs #4180.
Refs seakee/CPA-Manager-Plus#338.

This PR does not modify internal/translator/**; translator propagation remains maintainer-owned.

Read cache_write_tokens from prompt token details before falling back to response input details.
Preserve generic OpenAI usage handling across non-streaming and streaming responses.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds support for parsing prompt_tokens_details.cache_write_tokens in OpenAI-style usage metrics, falling back to input_tokens_details.cache_write_tokens if not present, and updates the corresponding unit tests. Feedback on the changes points out that renaming TestParseOpenAIStreamUsageResponsesFields to TestParseOpenAIStreamUsageChatCompletionsFields and changing its payload results in a loss of test coverage for older Responses/Codex-style fields in streaming responses. It is recommended to keep both tests to ensure both formats are covered.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread internal/runtime/executor/helps/usage_helpers_test.go
seakee added 4 commits July 10, 2026 20:45
Restore the Responses-style streaming test alongside the new Chat Completions coverage.
Prevents regressions in input_tokens detail parsing while addressing the review feedback.
Add an optional cache input mode to the core usage detail contract.

The field states whether cache read and creation tokens are already included in input tokens.

Its zero value remains unknown so existing plugins and consumers stay backward compatible.
Mark OpenAI, Gemini, and Interactions usage as included-input accounting and Claude usage as separate-input accounting.

This removes ambiguity for consumers while preserving all existing token fields and parser behavior.

Parser regression tests cover both included and separate cache semantics.
Include the optional cache_input_mode field in usage queue token payloads.

Consumers can use the explicit mode when available and continue inferring semantics when older producers omit it.

Queue tests verify the new metadata without changing existing JSON fields.
@seakee seakee changed the title fix(usage): parse cache writes from chat completions fix(usage): expose cache write accounting semantics Jul 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant