⚡ New — Kimi K3 is live: bring your own Moonshot key →
Documentation

Stream tokens and know the exact ₹ cost

← Cookbook

Read server-sent events and capture the final usage chunk so every request is costed.

SSE + usage chunkAdvancedIntermediate8 min

Streaming gives users tokens as they're generated. The catch with most gateways is you lose the token count. BharatRouter injects stream_options.include_usage on providers that support it, so the final chunk before [DONE] always carries a usage block — you stream and cost the request.

You'll use: stream: true + the final usage chunk. Reference: chat completions.

Do it

from openai import OpenAI

client = OpenAI(base_url="https://api.bharatrouter.com/v1", api_key="br-...")

stream = client.chat.completions.create(
    model="gemma-4-e4b-it",
    messages=[{"role": "user", "content": "Explain UPI in two lines"}],
    stream=True,
    stream_options={"include_usage": True},
)

usage = None
for chunk in stream:
    if chunk.usage:                       # final chunk carries usage
        usage = chunk.usage
    elif chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

print(f"\n\n[tokens: {usage.total_tokens}]")

How it works

The response is text/event-stream. Content deltas arrive first; the very last data chunk before [DONE] has empty choices and a populated usage object. The route that served you is still in the x-br-provider response header.

Pair with the cost playbook to both minimise and measure spend in one call.

More recipes in the Cookbook, or see the fullAPI reference.