The OneRouter API provides built-in Usage Accounting that allows you to track AI model usage without making additional API calls. This feature provides detailed information about token counts, costs, and caching status directly in your API responses.
Usage Information
When enabled, the API will return detailed usage information including:
Prompt and completion token counts using the model's native tokenizer
Cost in credits
Reasoning token counts (if applicable)
Cached token counts (if available)
This information is included in the last SSE message for streaming responses, or in the complete response for non-streaming requests.
Enabling Usage Accounting
You can enable usage accounting in your requests by including the usage parameter:
{"model":"your-model","usage": {"include":true }}
Response Format
When usage accounting is enabled, the response will include a usage object with detailed token information and a cost item and a cost_details object with detailed costs:
cost is the total amount charged to your account.
cost_details is the breakdown of the total cost.
Enabling usage accounting will add a few hundred milliseconds to the last response as the API calculates token counts and costs. This only affects the final message and does not impact overall streaming performance.
Benefits
Efficiency: Get usage information without making separate API calls
Accuracy: Token counts are calculated using the model's native tokenizer
Transparency: Track costs and cached token usage in real-time
Detailed Breakdown: Separate counts for prompt, completion, reasoning, and cached tokens
Best Practices
Enable usage tracking when you need to monitor token consumption or costs
Account for the slight delay in the final response when usage accounting is enabled
Consider implementing usage tracking in development to optimize token usage before production
Use the cached token information to optimize your application's performance
Examples
Basic Usage with Token Tracking
Streaming with Token Tracking
According to the OpenAI specification, to request token usage information in a streaming response, you would include the following parameters in your request:
This configuration tells the API to:
Use the Gemini 2.5 Flash model
Stream the response incrementally
Include token usage statistics in the stream response
The stream_options.include_usage parameter specifically requests that token usage information be returned as part of the streaming response.
{
"id": "c4942c8a-39d8-d39e-7eb0-395c4e4dbf68",
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": null,
"message": {
"content": "**Paris** is the capital of France. It's the largest city in the country, serving as the political, cultural, and economic center, with a population of about 2.1 million in the city proper and over 12 million in the greater metropolitan area. This has been the case since the 10th century, when Hugh Capet established it as the seat of the Capetian dynasty.",
"refusal": null,
"role": "assistant",
"annotations": null,
"audio": null,
"function_call": null,
"tool_calls": null
}
}
],
"created": 1763949831,
"model": "grok-4-1-fast-non-reasoning",
"object": "chat.completion",
"service_tier": null,
"system_fingerprint": "fp_80e0751284",
"usage": {
"completion_tokens": 80,
"prompt_tokens": 175,
"total_tokens": 255,
"completion_tokens_details": {
"accepted_prediction_tokens": 0,
"audio_tokens": 0,
"reasoning_tokens": 0,
"rejected_prediction_tokens": 0
},
"prompt_tokens_details": {
"audio_tokens": 0,
"cached_tokens": 161,
"image_tokens": 0,
"text_tokens": 175
},
"num_sources_used": 0
},
"cost": 0.000051,
"cost_details": {
"audio_cost": 0,
"cache_prompt_cost": 8.05e-6,
"cache_write_cost": 0,
"generation_cost": 0,
"image_cost": 0,
"input_prompt_cost": 2.8e-6,
"output_prompt_cost": 0.00004,
"tools_cost": 0,
"video_cost": 0
},
"request_id": "e7d2ff652d84410f903aef33d7f6471e"
}
from openai import OpenAI
client = OpenAI(
base_url="https://llm.onerouter.pro/v1",
api_key="{{API_KEY_REF}}",
)
response = client.chat.completions.create(
model="{{MODEL}}",
messages=[
{"role": "user", "content": "What is the capital of France?"}
],
extra_body={
"usage": {
"include": True
}
}
)
print("Response:", response.choices[0].message.content)
print("Usage Stats:", getattr(response, "usage", None))
curl https://llm.onerouter.pro/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <<API Keys>>" \
-d '{
"model": "gemini-2.5-flash",
"messages": [
{
"role": "user",
"content": "What is the meaning of life?"
}
],
"stream": true,
"stream_options": {
"include_usage": true
}
}'
data: {"id":"gen-1765672972-kDHtq4adiMmXj2DCk9mc","object":"chat.completion.chunk","created":1765672972,"model":"gemini-2.5-flash","system_fingerprint":null,"request_id":"69379f7f07e5406d8406ca56e26b0355","choices":[{"delta":{"content":"The","role":"assistant"},"logprobs":null,"finish_reason":null,"index":0}],"usage":null}
data: {"id":"gen-1765672972-kDHtq4adiMmXj2DCk9mc","object":"chat.completion.chunk","created":1765672972,"model":"gemini-2.5-flash","system_fingerprint":null,"request_id":"69379f7f07e5406d8406ca56e26b0355","choices":[{"delta":{"content":" meaning of life is a","role":"assistant"},"logprobs":null,"finish_reason":null,"index":0}],"usage":null}
data: {"id":"gen-1765672972-kDHtq4adiMmXj2DCk9mc","object":"chat.completion.chunk","created":1765672972,"model":"gemini-2.5-flash","system_fingerprint":null,"request_id":"69379f7f07e5406d8406ca56e26b0355","choices":[{"delta":{"content":" deeply personal and philosophical question that has been pondered by thinkers across cultures and throughout history. There's","role":"assistant"},"logprobs":null,"finish_reason":null,"index":0}],"usage":null}
data: {"id":"gen-1765672972-kDHtq4adiMmXj2DCk9mc","object":"chat.completion.chunk","created":1765672972,"model":"gemini-2.5-flash","system_fingerprint":null,"request_id":"69379f7f07e5406d8406ca56e26b0355","choices":[{"delta":{"content":" no single, universally accepted answer, and that's part of what makes it so fascinating.\n\n","role":"assistant"},"logprobs":null,"finish_reason":null,"index":0}],"usage":null}
data: {"id":"gen-1765672972-kDHtq4adiMmXj2DCk9mc","object":"chat.completion.chunk","created":1765672972,"model":"gemini-2.5-flash","system_fingerprint":null,"request_id":"69379f7f07e5406d8406ca56e26b0355","choices":[{"delta":{"content":"Here are some common perspectives and ways people approach this question:\n\n**1. There is no inherent, pre-ordained meaning.**\n* **Exist","role":"assistant"},"logprobs":null,"finish_reason":null,"index":0}],"usage":null}
data: {"id":"gen-1765672972-kDHtq4adiMmXj2DCk9mc","object":"chat.completion.chunk","created":1765672972,"model":"gemini-2.5-flash","system_fingerprint":null,"request_id":"69379f7f07e5406d8406ca56e26b0355","choices":[{"delta":{"content":"entialism:** This philosophy suggests that we are born into a world without inherent meaning, and it is *our responsibility* to create our own meaning through our choices,","role":"assistant"},"logprobs":null,"finish_reason":null,"index":0}],"usage":null}
data: {"id":"gen-1765672972-kDHtq4adiMmXj2DCk9mc","object":"chat.completion.chunk","created":1765672972,"model":"gemini-2.5-flash","system_fingerprint":null,"request_id":"69379f7f07e5406d8406ca56e26b0355","choices":[{"delta":{"content":" actions, and values. Life is what you make of it.\n* **Absurdism:** Similar to existentialism, absurdism acknowledges the lack of inherent meaning in the universe and the human desire for it. However, it emphasizes","role":"assistant"},"logprobs":null,"finish_reason":null,"index":0}],"usage":null}
data: {"id":"gen-1765672972-kDHtq4adiMmXj2DCk9mc","object":"chat.completion.chunk","created":1765672972,"model":"gemini-2.5-flash","system_fingerprint":null,"request_id":"69379f7f07e5406d8406ca56e26b0355","choices":[{"delta":{"content":" the \"absurd\" conflict between this desire and the silent, meaningless universe. The meaning, then, comes from embracing this absurdity and rebelling against it by living fully.\n\n**2. Meaning is found in external sources.**\n* **Religion","role":"assistant"},"logprobs":null,"finish_reason":null,"index":0}],"usage":null}
data: {"id":"gen-1765672972-kDHtq4adiMmXj2DCk9mc","object":"chat.completion.chunk","created":1765672972,"model":"gemini-2.5-flash","system_fingerprint":null,"request_id":"69379f7f07e5406d8406ca56e26b0355","choices":[{"delta":{"content":"/Spirituality:** Many religions offer a framework for meaning, often involving a divine purpose, a path to salvation, a connection to something greater than oneself, or a set of moral guidelines that give life structure and significance.\n* **Nature/Universe:** Some find meaning in the interconnectedness of all living things, the","role":"assistant"},"logprobs":null,"finish_reason":null,"index":0}],"usage":null}
data: {"id":"gen-1765672972-kDHtq4adiMmXj2DCk9mc","object":"chat.completion.chunk","created":1765672972,"model":"gemini-2.5-flash","system_fingerprint":null,"request_id":"69379f7f07e5406d8406ca56e26b0355","choices":[{"delta":{"content":" vastness and beauty of the cosmos, or the scientific understanding of our existence.\n* **Humanity/Societal Contribution:** Meaning can be derived from contributing to the well-being of others, working for social good, making a positive impact on the world, or leaving a legacy.\n\n**3. Meaning is","role":"assistant"},"logprobs":null,"finish_reason":null,"index":0}],"usage":null}
data: {"id":"gen-1765672972-kDHtq4adiMmXj2DCk9mc","object":"chat.completion.chunk","created":1765672972,"model":"gemini-2.5-flash","system_fingerprint":null,"request_id":"69379f7f07e5406d8406ca56e26b0355","choices":[{"delta":{"content":" found in internal experiences and pursuits.**\n* **Love and Relationships:** For many, deep connections with family, friends, and romantic partners provide immense meaning, joy, and purpose.\n* **Happiness and Pleasure:** While often seen as hedonistic, some find meaning in the pursuit of happiness, joy, and pleasurable","role":"assistant"},"logprobs":null,"finish_reason":null,"index":0}],"usage":null}
data: {"id":"gen-1765672972-kDHtq4adiMmXj2DCk9mc","object":"chat.completion.chunk","created":1765672972,"model":"gemini-2.5-flash","system_fingerprint":null,"request_id":"69379f7f07e5406d8406ca56e26b0355","choices":[{"delta":{"content":" experiences, as long as it doesn't harm others.\n* **Creativity and Self-Expression:** The act of creating art, music, writing, or any form of self-expression can imbue life with meaning and a sense of purpose.\n* **Learning and Growth:** Continuously learning, growing","role":"assistant"},"logprobs":null,"finish_reason":null,"index":0}],"usage":null}
data: {"id":"gen-1765672972-kDHtq4adiMmXj2DCk9mc","object":"chat.completion.chunk","created":1765672972,"model":"gemini-2.5-flash","system_fingerprint":null,"request_id":"69379f7f07e5406d8406ca56e26b0355","choices":[{"delta":{"content":", and seeking knowledge can be a source of profound meaning.\n* **Personal Fulfillment:** This is broad but often involves discovering and pursuing one's passions, talents, and what makes one feel alive and engaged.\n\n**4. The meaning of life is the act of living itself.**\n* This perspective suggests that the","role":"assistant"},"logprobs":null,"finish_reason":null,"index":0}],"usage":null}
data: {"id":"gen-1765672972-kDHtq4adiMmXj2DCk9mc","object":"chat.completion.chunk","created":1765672972,"model":"gemini-2.5-flash","system_fingerprint":null,"request_id":"69379f7f07e5406d8406ca56e26b0355","choices":[{"delta":{"content":" meaning isn't a destination or a hidden secret, but rather the sum of all our experiences, struggles, joys, and simply the fact of being alive. It's about being present and experiencing life as it unfolds.\n\n**In summary:**\n\nThe \"meaning of life\" isn't a single answer waiting","role":"assistant"},"logprobs":null,"finish_reason":null,"index":0}],"usage":null}
data: {"id":"gen-1765672972-kDHtq4adiMmXj2DCk9mc","object":"chat.completion.chunk","created":1765672972,"model":"gemini-2.5-flash","system_fingerprint":null,"request_id":"69379f7f07e5406d8406ca56e26b0355","choices":[{"delta":{"content":" to be discovered. It's more of a personal quest to define what gives *your* life purpose, value, and significance. It can evolve over time, change with experiences, and be found in a multitude of places.\n\nPerhaps the true meaning lies in the search itself, in the questions we ask, and in the way","role":"assistant"},"logprobs":null,"finish_reason":null,"index":0}],"usage":null}
data: {"id":"gen-1765672972-kDHtq4adiMmXj2DCk9mc","object":"chat.completion.chunk","created":1765672972,"model":"gemini-2.5-flash","system_fingerprint":null,"request_id":"69379f7f07e5406d8406ca56e26b0355","choices":[{"delta":{"content":" we choose to live our lives in response.","role":"assistant"},"logprobs":null,"finish_reason":"stop","native_finish_reason":"STOP","index":0}],"usage":null}
data: {"id":"gen-1765672972-kDHtq4adiMmXj2DCk9mc","object":"chat.completion.chunk","created":1765672972,"model":"gemini-2.5-flash","system_fingerprint":null,"request_id":"69379f7f07e5406d8406ca56e26b0355","choices":[{"delta":{"content":"","role":"assistant"},"logprobs":null,"finish_reason":null,"index":0}],"usage":{"prompt_tokens":7,"completion_tokens":677,"total_tokens":684,"prompt_tokens_details":{},"completion_tokens_details":{},"input_tokens":0,"output_tokens":0,"ttft":0,"server_tool_use":{"web_search_requests":""}}}
data: [DONE]