streaming-usage-accounting · EN · 2026-10-14

Does Usage Arrive in the Final Chunk? Tracking Tokens and USDC Cost While Streaming

Learn how usage totals appear in streamed LLM responses, how to capture them for real-time cost tracking, and how to reconcile them with the 1.3x USDC deduction on the aggregator platform. Includes practical steps and code examples.

Why Usage in Streaming Matters

When you stream responses from an LLM API, you typically receive chunks of text incrementally. If you need to track token usage and cost in real time, you must know whether the final chunk includes usage totals. This guide explains where to find usage in streams and how to reconcile it with your USDC deductions.

Where Usage Totals Appear

Most LLM providers follow one of these patterns:

  • Final chunk includes usage: The last message in the stream contains a usage field with prompttokens, completiontokens, and total_tokens.
  • Separate usage event: Some providers send a distinct event (e.g., event: usage) just before the stream ends.
  • No usage in stream: Usage is only available via a separate API call after the stream completes.

On our aggregator, we normalize responses so that usage is always included in the final chunk when the underlying provider supports it. This lets you compute costs without an extra request.

How to Capture Usage in Your Client

When parsing the stream, look for the last chunk and extract the usage object. Here is a Python example using SSE:

import requests
import json

response = requests.post(
    "https://api.example.com/v1/chat/completions",
    headers={"Authorization": "Bearer YOUR_KEY"},
    json={"model": "claude-3", "messages": [...], "stream": True},
    stream=True
)

usage = None
for line in response.iter_lines():
    if line:
        line = line.decode("utf-8").removeprefix("data: ")
        if line == "[DONE]":
            break
        chunk = json.loads(line)
        if "usage" in chunk:
            usage = chunk["usage"]

print(usage)  # {'prompt_tokens': 10, 'completion_tokens': 20, 'total_tokens': 30}

If your provider sends usage in a separate event, adjust your parsing logic accordingly.

Calculating Cost from Usage

Once you have token counts, compute the official cost using the model's pricing. For example, if a model charges $X per 1K tokens for input and $Y per 1K tokens for output:

cost_official = (prompt_tokens / 1000) * X + (completion_tokens / 1000) * Y

Your actual USDC deduction is cost_official * 1.3 (the platform multiplier). This amount is deducted from your USDC balance on Base.

Reconciling with Your USDC Deduction

To reconcile without waiting for a billing call:

  1. After the stream ends, extract the usage totals.
  2. Compute the official cost.
  3. Multiply by 1.3 to get the expected USDC deduction.
  4. Compare this with the change in your USDC balance (if you track it) or with the deduction shown in your dashboard.

Note that the deduction may be batched or delayed, so immediate balance checks might not reflect the exact amount. However, the computed cost should match the eventual deduction.

Handling Missing Usage

If the stream does not include usage, you have two options:

  • Fall back to a separate usage endpoint (if provided).
  • Estimate tokens using a tokenizer library (e.g., tiktoken for OpenAI models). Be aware that estimates may not match exact billed amounts.

Our aggregator aims to include usage in the stream whenever possible, but always check the final chunk or look for a usage event.

Best Practices

  • Always parse the final chunk for usage.
  • Log token counts and computed costs for auditing.
  • Use the 1.3 multiplier to predict USDC deductions.
  • If you are a key contributor, remember you are credited at official price × 1.1 (or × 1.2 for premium models) in USDC, not charged.
  • Avoid relying on estimates for billing; use actual usage when available.

By following these steps, you can track token usage and USDC costs in real time without waiting for a separate billing call.