Prompt
Why is my gateway not logging token usage correctly?
Latest observation
Nov 13, 2026
OpenAI APIWeb search: off
A gateway usually “misses” token usage logs for one of a few common reasons:
-
The model response doesn’t include usage
- Some providers only return token counts when you request it or when the model/API supports it.
- Example: you may need something like
include_usage=true,stream_options.include_usage, or a non-streamed response.
-
Streaming responses aren’t being handled correctly
- With streaming, usage often comes in a final chunk/event, not the first ones.
- If your gateway stops reading early or only logs the initial response, token usage will look missing or zero.
-
Your gateway isn’t parsing the right field
- Different providers use different shapes:
usage.total_tokensusage.prompt_tokens/usage.completion_tokens- nested metadata fields
- If the gateway expects one schema but the provider returns another, logging can fail silently.
- Different providers use different shapes:
-
Retries / fallbacks are confusing the accounting
- If the gateway retries a request or falls back to another model, the usage may be logged against the wrong request ID or not merged properly.
-
The response is cached
- Cached responses may not produce fresh usage data, or your gateway might intentionally skip billing/logging on cache hits.
-
Async logging is failing
- The gateway may receive usage, but the log write fails due to:
- queue backpressure
- DB errors
- timeouts
- dropped telemetry events
- The gateway may receive usage, but the log write fails due to:
-
Tool/function calls or partial outputs are not accounted for
- Some providers report token usage differently when tool calls, structured outputs, or multiple turns are involved.
-
Mismatch between provider and gateway versions
- Upgrading the model API without updating the gateway’s adapter can break usage parsing.
What to check first
- Look at the raw API response and confirm usage exists there.
- Verify your gateway is handling streaming final events.
- Check whether the gateway is parsing the correct usage field names.
- Inspect logs for errors in the telemetry/billing pipeline.
- Confirm whether the request was a cache hit, retry, or fallback.
If you want, paste:
- the gateway/provider you’re using,
- whether the request is streamed,
- and a sample raw response/log snippet,
and I can help pinpoint the exact cause.