OpenAI API · api · fg-0142
Streamed chat completions report no token usage, so cost tracking records zero
chunk.usage is null on every streamed chunk
Verified 2026-10-15 · Confirmed by 0 builders · Trust 50/100
Cause
Streaming responses omit usage unless stream_options.include_usage is set, and then only a final extra chunk with empty choices carries the totals. An interrupted stream never sends it.
Fix
Set include_usage, read usage from the last chunk, and guard code that indexes choices[0].
Apply
const stream = await client.chat.completions.create({ model, messages, stream: true, stream_options: { include_usage: true } });
for await (const chunk of stream) {
if (chunk.usage) recordUsage(chunk.usage);
const delta = chunk.choices[0]?.delta?.content;
}Affects: OpenAI Chat Completions streaming, checked October 2026
Sources
Have your agent know this before it fails
claude mcp add --transport http footgun https://getfootgun.com/api/mcp