API Usage Accounting

Track token costs and spending with usage in every response. OpenRouter-compatible.

TheRouter.ai is an OpenAI-compatible API router. Every response — whether non-streaming or the final chunk of a stream — includes a usage object with prompt_tokens, completion_tokens, total_tokens, and, when available, cost in your account currency. This page shows how to read that object from TypeScript, Python, and raw HTTP so you can meter applications in real time without a separate billing API call.

What the Usage Object Contains

Every non-streaming response and the final streaming chunk include a usageobject with prompt_tokens, completion_tokens, and (when billing is enabled) cost. This is the primary way to meter API spend in real time without polling a separate billing endpoint.

Overview

This page mirrors the OpenRouter workflow and adapts it for TheRouter.ai. Use TheRouter.ai as your OpenAI-compatible endpoint and keep model IDs in provider/model format.

Quick Verification

Send a single request to confirm usage is being returned. Keep your TheRouter.ai key in THEROUTER_API_KEY.

install.sh
curl https://api.therouter.ai/v1/chat/completions   -H "Authorization: Bearer $THEROUTER_API_KEY"   -H "Content-Type: application/json"   -d '{"model":"openai/gpt-4o-mini","messages":[{"role":"user","content":"hi"}]}'

Reading Usage in Code

Both TypeScript and Python SDKs expose the usage object directly on the response. The fields are identical to what OpenAI returns, so existing parsing code works without changes.

TypeScript
const usage = response.usage;
console.log(usage.prompt_tokens, usage.completion_tokens, usage.cost);

Streaming Caveats

Wait for the terminal chunk
In streaming mode, intermediate chunks carry incremental content but not the final usage breakdown. Only the terminal chunk — the one with finish_reason set — includes the complete usage object. Commit your cost record only after that chunk arrives.

For production rollouts, pin SDK versions, validate model compatibility in staging, and confirm that your billing integration reads from the terminal chunk rather than from a mid-stream delta.

Help & contact