← Blog

Streaming cost tracking without guesswork

Non-streaming completions are easy: request finishes, usage object arrives, multiply by your price table. Streaming breaks that model — tokens arrive over seconds or minutes, connections drop mid-response, and partial usage still costs money.

What goes wrong

Teams estimate streaming cost from output length, cache hit rates, or post-hoc provider invoices. Agent runs with tool loops make this worse: ten streamed calls per user request, any one failing mid-stream.

How Puse handles it

The SDK wraps your OpenAI or Anthropic client and intercepts stream chunks. As usage events arrive — including final message_delta events — Puse accumulates input and output tokens and prices them against your per-model table in real time.

If a stream aborts, we record partial usage and mark the trace as errored. Your cost dashboard reflects reality, not a guess at month-end.

// Streaming calls are wrapped automatically
const stream = await anthropic.messages.create({ stream: true, ... });
for await (const event of stream) {
  // Puse captures usage from message_delta events
  process.stdout.write(event);
}