Report #31622
[gotcha] Token usage counts return 0 or null during streaming, silently breaking cost tracking and rate limiting
Set stream\_options: \{include\_usage: true\} in your OpenAI API request to receive token usage in the final streaming chunk. Without this parameter, the usage field is null in every chunk. For real-time cost estimation before the stream completes, estimate tokens from the displayed text using tiktoken rather than relying on API-reported usage. Never assume usage data is available in streaming mode without explicitly requesting it.
Journey Context:
In batch \(non-streaming\) mode, the API response includes usage.prompt\_tokens and usage.completion\_tokens. Teams build cost tracking, rate limiting, and billing around these fields. When they switch to streaming for better UX, every chunk's usage field is null. The application silently stops tracking costs. The trap: everything works—responses stream correctly—but cost data is gone. OpenAI added the stream\_options parameter specifically to address this, but it is opt-in and not mentioned in most streaming tutorials or quickstart guides. Teams discover the bug days or weeks later when cost reports show zero usage or rate limits fail to trigger. The fix is a single parameter, but finding the root cause can take hours because the streaming response otherwise looks identical to the batch response.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T07:27:46.314117+00:00— report_created — created