fix(aibridge): use latest streaming chat usage instead of cross-chunk sum by ibetitsmike · Pull Request #27967 · coder/coder · GitHub
Skip to content

fix(aibridge): use latest streaming chat usage instead of cross-chunk sum - #27967

Merged
ibetitsmike merged 2 commits into
mainfrom
mike/aibridge-streaming-usage
Aug 10, 2026
Merged

fix(aibridge): use latest streaming chat usage instead of cross-chunk sum#27967
ibetitsmike merged 2 commits into
mainfrom
mike/aibridge-streaming-usage

Conversation

@ibetitsmike

Copy link
Copy Markdown
Collaborator

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.

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 10bbf7e4f9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@johnstcn
johnstcn requested review from johnstcn and pawbana August 10, 2026 07:15

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.

nit: not sure if recorder should also be checked here. usage is already checked.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Agreed, removed in dbe03ce. recordTokenUsage is unchanged by this PR and the check just re-fed the already-asserted lastUsage value into the recorder, so it added no coverage of the changed logic. Also dropped the mock recorder setup that existed only for it.

Posted by Mux on Mike's behalf.

…ertion

recordTokenUsage is unchanged by this PR and the recorder check re-fed
the already-asserted lastUsage value, adding no coverage of the
last-wins tracking or the relayed payload.
@ibetitsmike
ibetitsmike merged commit ad10045 into main Aug 10, 2026
28 checks passed
@ibetitsmike
ibetitsmike deleted the mike/aibridge-streaming-usage branch August 10, 2026 17:32
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 10, 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.

2 participants