A team I worked with spent a week arguing with their AI gateway vendor about a 22% gap between the usage panel and the invoice. Both numbers were computed correctly. The panel was summing a field that the gateway only populated for non-streaming requests. Everything they served through a chat UI — which was most of their traffic — reported nothing, and the panel treated nothing as zero.
Nobody lied. The field is optional.
The part of the spec people skip
If you call an OpenAI-compatible /v1/chat/completions endpoint without streaming, every response carries a usage object:
{
"id": "chatcmpl-8f2a...",
"choices": [{ "index": 0, "message": { "role": "assistant", "content": "..." }, "finish_reason": "stop" }],
"usage": { "prompt_tokens": 812, "completion_tokens": 190, "total_tokens": 1002 }
}
With stream: true, the default is that you get no usage at all. Not an empty object — no object. You have to ask for it:
{
"model": "your-model",
"messages": [{ "role": "user", "content": "hello" }],
"stream": true,
"stream_options": { "include_usage": true }
}
When you do, the server sends one extra chunk at the very end, after the final content delta:
data: {"id":"chatcmpl-8f2a...","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":null}
data: {"id":"chatcmpl-8f2a...","choices":[],"usage":{"prompt_tokens":812,"completion_tokens":190,"total_tokens":1002}}
data: [DONE]
Look at that second-to-last chunk carefully. choices is an empty array. There is no delta to render. It exists only to carry usage.
That is exactly why gateways drop it. A streaming proxy is usually written as a loop that parses each SSE frame, pulls choices[0].delta, appends it to the client's stream, and moves on. A frame with an empty choices array matches no branch, so it gets skipped. The token count is in the packet the code was written to ignore.
Three calls that tell you whether you have this problem
Run these against the exact endpoint you bill against, not against the provider's docs example.
1. Non-streaming, to get a reference number
curl -s https://your-gateway.example/v1/chat/completions \
-H "Authorization: Bearer $KEY" \
-H "Content-Type: application/json" \
-d '{"model":"your-model","messages":[{"role":"user","content":"Count from 1 to 40."}]}' \
| jq '.usage'
Write down prompt_tokens and completion_tokens.
2. Streaming, without asking for usage
curl -sN https://your-gateway.example/v1/chat/completions \
-H "Authorization: Bearer $KEY" \
-H "Content-Type: application/json" \
-d '{"model":"your-model","messages":[{"role":"user","content":"Count from 1 to 40."}],"stream":true}' \
| grep -c '"usage"'
If this prints 0, your gateway returns no usage on streaming responses. That is legal per the spec and it is also the source of your reconciliation gap. If it prints a number, check the next call to see whether the value is real or null.
3. Streaming, with usage requested
curl -sN https://your-gateway.example/v1/chat/completions \
-H "Authorization: Bearer $KEY" \
-H "Content-Type: application/json" \
-d '{"model":"your-model","messages":[{"role":"user","content":"Count from 1 to 40."}],"stream":true,"stream_options":{"include_usage":true}}' \
| tail -3
You want to see the empty-choices frame with a populated usage object before [DONE].
The three outcomes, and what each costs you
Gateway returns no usage on stream. Your own metering has to count tokens client-side, which means a tokenizer, which means your count will differ from the provider's by a few percent on every request and by a lot on any request with tools or images. You can still reconcile, but you are reconciling two estimates instead of comparing an estimate to a fact.
Gateway returns usage but only total_tokens. This is common and it is worse than it looks. You cannot split input from output, so you cannot check the request against a price sheet that bills them at different rates. A single blended number hides which side of the bill moved.
Gateway returns usage, but only on some routes. This is the case that produced the 22% gap: streaming routes silent, batch routes fine, panel sums everything. The number looks plausible, which is why nobody catches it for a month.
What to put in the acceptance test
Before you sign, send one streaming request and one non-streaming request with the same prompt and assert three things:
- Both responses contain a
usageobject withprompt_tokensandcompletion_tokensas separate integers. -
prompt_tokensmatches between the two calls. It should, for the same input — a difference means the two code paths are counting differently, and one of them is wrong. - The streaming response's
usagetotal is within a small tolerance of the non-streaming one. Sampling noise and temperature make exact equality unrealistic; a consistent directional bias does not.
Then do it again on a route with tool calls, because tool schemas are counted in prompt_tokens and are the most common place for a gateway to under-report.
Why this is worth an afternoon
The gap is not usually fraud. It is a default that nobody overrode, in a code path that was written to forward text and never told it was also responsible for numbers. But it has a direction: the field that goes missing is the one that would have let you check the bill. Silent, one-way, and in the vendor's favour is the worst combination for a number you are supposed to trust.
A usage field that is present, split, and consistent across every route is a cheap thing to demand. The alternative is a quarterly argument where both sides are right and neither can prove it.
Test the endpoint you actually pay through, not the one in the docs. The two are rarely the same implementation.
Top comments (0)