fix: add cache write accounting to OpenAI interceptors by pawbana · Pull Request #28716 · coder/coder · GitHub
Skip to content

fix: add cache write accounting to OpenAI interceptors - #28716

Merged
mtojek merged 3 commits into
release/2.36from
pawel/token-accounting-fixes-2-36
Aug 27, 2026
Merged

fix: add cache write accounting to OpenAI interceptors#28716
mtojek merged 3 commits into
release/2.36from
pawel/token-accounting-fixes-2-36

Conversation

@pawbana

@pawbana pawbana commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Cherry-picks:

Compatibility fix:

  • ae737db bumps OpenAI SDK + minimal changes, bump required by cherry picks, based on c97f4da3ac

ibetitsmike and others added 2 commits August 27, 2026 13:00
… sum (#27967)

## Problem

CODAGT-906: chats using OpenAI-compatible backends (e.g. poolside)
through the AI Bridge persist token usage inflated 105x-640x, which
falsely triggers automatic chat compaction on every turn.

The chat-completions streaming interceptor summed usage across every SSE
chunk of one upstream stream and rewrote each relayed usage-bearing
chunk with that running sum. Spec-compliant OpenAI emits usage once
(final chunk with `stream_options.include_usage`), so the sum equals the
final value. vLLM-style backends emit cumulative usage snapshots on
every chunk, so the relayed final usage becomes roughly `N_chunks x
prompt_tokens` (e.g. 417,012 persisted for a ~6,000-token context).
chatd persists that value per assistant message and its compaction
trigger reads it as context occupancy.

## Fix

Track the latest usage-bearing chunk's raw usage (last-wins) in the
stream processor, updating only when a chunk actually carries usage so a
trailing usage-less chunk cannot zero it. `marshalChunk` relays that
value and `recordTokenUsage` records the same value, unifying relayed
and recorded usage. Last-wins is correct for both shapes: a single final
usage chunk, and cumulative snapshots where each snapshot already
includes all prior tokens.

Per-iteration semantics are unchanged: each tool-loop iteration has its
own processor, and the final iteration's usage is what the client sees.

## Tests

- `TestStreamProcessorUsage` (internal): cumulative snapshots with a
trailing usage-less chunk, and the spec-compliant final-only shape;
asserts relayed and recorded usage equal the last snapshot.
- New txtar fixture `streaming_cumulative_usage_injected_tool.txtar`
with per-chunk cumulative usage plus an injected tool call; asserts
client-visible final usage through the full interceptor.
- Red-green verified: with the fix reverted, the internal test reports
zeroed usage (trailing chunk overwrite) and the fixture test reports
18000 summed prompt tokens instead of 6000.

The blocking (non-streaming) path deliberately keeps its cross-iteration
summation for external clients and is untouched. Remote dogfood UAT
validated chat streaming, tool calls, plausible usage numbers, and zero
spurious compactions.

> Mux acted on Mike's behalf to author this change.
Adds cache write token accounting to OpenAI interceptors.
@github-actions

Copy link
Copy Markdown
Contributor

@pawbana pawbana changed the title Pawel/token accounting fixes 2 36 fix: token accounting fixes 2 36 Aug 27, 2026
@pawbana pawbana changed the title fix: token accounting fixes 2 36 fix: token accounting fixes Aug 27, 2026
@pawbana
pawbana marked this pull request as draft August 27, 2026 13:34
@pawbana
pawbana marked this pull request as ready for review August 27, 2026 16:15
@pawbana
pawbana requested a review from mtojek August 27, 2026 16:16
@pawbana pawbana changed the title fix: token accounting fixes fix: add cache write accounting to OpenAI interceptors Aug 27, 2026
@mtojek
mtojek merged commit ca8ef6c into release/2.36 Aug 27, 2026
53 checks passed
@mtojek
mtojek deleted the pawel/token-accounting-fixes-2-36 branch August 27, 2026 16:19
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 27, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants