Skip to content

fix(tokenusage): handle split Responses API completion events - #56

Closed
buvidk1234 wants to merge 1 commit into
higress-group:mainfrom
buvidk1234:fix/responses-completed-token-usage
Closed

buvidk1234 wants to merge 1 commit into
higress-group:mainfrom
buvidk1234:fix/responses-completed-token-usage

Conversation

@buvidk1234

@buvidk1234 buvidk1234 commented Aug 25, 2026 •

Copy link
Copy Markdown

Summary

Fix token usage extraction for large OpenAI Responses API streaming responses
whose response.completed event is split across multiple callbacks.

Closes #55

Changes

  • Start buffering when the SSE marker event: response.completed is received.
  • Keep the existing JSON marker fallback for compatibility.
  • Add a regression test matching the observed callback order:
    • event line;
    • JSON prefix;
    • intermediate JSON fragments;
    • fragment containing usage and the completion type;
    • SSE event terminator.

Root cause

The previous implementation waited for the JSON value "response.completed".
That value is located near the end of a Responses API completion event.

When the event was split, the earlier {"response": ...} fragments were not
buffered. The final fragment contained usage but was not valid standalone JSON,
so response.usage.* resolved to zero.

Relation to #54

This PR is related to #54 but intentionally keeps the production change scoped
to the callback sequence reproduced from the reported request:

EOS-aware flushing and bounded buffering are useful hardening work, but require
separate behavioral contracts so they do not trade one token-accounting failure
for another.

Verification

The regression test returns:

input_tokens:  75907
output_tokens: 199
total_tokens:  76106

All tests pass:

go test ./...

Signed-off-by: buvidk <1532493290@qq.com>
@CLAassistant

CLAassistant commented Aug 25, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@almondfasccino-dev

Copy link
Copy Markdown

In this issue, we should consider the event fragment is really big, refer to this issue .
In this case,
if bytes.Contains(chunk, []byte("event: response.completed")) || (bytes.Contains(chunk, []byte("response.completed")) && !bytes.Contains(chunk, []byte("usage")))
the condition : "&& !bytes.Contains(chunk, []byte("usage")))" will have a problem as the event may return this 'usage' word in tools.description but literally it is not the one we want.
and also, should consider the fragment is allocated way more mem as one streaming response, it will have impact to the performance of the ai-statistic plugin itself

@buvidk1234 buvidk1234 closed this Sep 26, 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.

tokenusage: OpenAI Responses API usage is lost when response.completed is split across chunks

3 participants