Skip to content

fix(ai): report OpenAI cache writes - #38735

Merged
kitlangton merged 1 commit into
v2from
sol-cache-usage
Jul 24, 2026
Merged

kitlangton merged 1 commit into
v2from
sol-cache-usage

Conversation

@kitlangton

Copy link
Copy Markdown
Contributor

What

Report GPT-5.6 and later OpenAI prompt-cache writes in normalized AI usage for both Responses and Chat Completions.

GPT-5.6 cache writes are billed at 1.25x uncached input, but the native protocol decoders currently discard cache_write_tokens. This makes every write appear as zero and overstates fresh input.

Adds a patch changeset for @opencode-ai/ai.

Before / After

Before

Given provider usage with 5 total input tokens, 1 cache-read token, and 2 cache-write tokens, normalized usage reported:

input:       5
fresh:       4
cache read:  1
cache write: undefined

After

The same payload preserves the provider's complete non-overlapping breakdown:

input:       5
fresh:       2
cache read:  1
cache write: 2

The invariant fresh + cache read + cache write = input remains true.

How

  • packages/ai/src/protocols/open-responses.ts decodes input_tokens_details.cache_write_tokens and maps it into Usage.cacheWriteInputTokens.
  • packages/ai/src/protocols/openai-chat.ts does the same for prompt_tokens_details.cache_write_tokens.
  • Both protocols subtract reads and writes when deriving nonCachedInputTokens from the provider's inclusive input total.
  • Existing stream-fixture tests now cover the complete GPT-5.6 usage shape.

Scope

This PR changes response accounting only. It does not alter request bodies, enable GPT-5.6 explicit cache breakpoints, or change automatic cache policy.

Testing

  • bun run test test/provider/openai-responses.test.ts test/provider/openai-chat.test.ts: 116 passed, 0 failed
  • bun run test in packages/ai: 444 passed, 28 skipped, 0 failed
  • bun typecheck in packages/ai: passed
  • pre-push bun turbo typecheck --concurrency=3: 33 tasks passed
  • git diff --check: passed

@kitlangton
kitlangton enabled auto-merge (squash) July 24, 2026 18:06
@kitlangton
kitlangton merged commit b09a066 into v2 Jul 24, 2026
12 of 13 checks passed
@kitlangton
kitlangton deleted the sol-cache-usage branch July 24, 2026 18:26
jinhuang712 pushed a commit to jinhuang712/opencode that referenced this pull request Sep 26, 2026
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.

1 participant