
Read cost_in_usd_ticks on Grok API responses
Pull the exact billed amount for each request from usage.cost_in_usd_ticks so you can meter a feature, alert on spikes, or sum a session without waiting on a Console invoice. Official Cost Tracking documents the field on Chat Completions, the Responses API, image generation, and video generation. The value is already after prompt-cache discounts and includes server-side tool invocations for that single call. Create a key and load credits at console.x.ai.
What you need
An XAI_API_KEY, a client that reads the usage object (curl, OpenAI-compatible SDK, or xAI SDK), and a place to store per-request dollars. Neighboring jobs include Get batch cost breakdown when the work is queued, Explore API usage in the xAI Console for team-level charts, and Get usage analytics via the Management API for programmatic rollups. More API jobs live on the API hub.
Convert ticks to dollars
- Export the key outside of source control, then run a Responses call and print the ticks field:
export XAI_API_KEY="your_api_key"
curl https://api.x.ai/v1/responses \
-H "Authorization: Bearer $XAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "grok-4.7",
"input": "Say hello"
}' | jq '.usage.cost_in_usd_ticks'
Divide by
10_000_000_000(10^10). Official docs use that scale so totals stay exact when you sum thousands of requests — for example37756000ticks is about$0.0038.Or use the xAI Python SDK, which exposes both raw ticks and a
cost_usdhelper:
import os
from xai_sdk import Client
from xai_sdk.chat import user
client = Client(api_key=os.getenv("XAI_API_KEY"))
chat = client.chat.create(model="grok-4.7", messages=[user("Say hello")])
response = chat.sample()
print(f"Cost: ${response.cost_usd:.6f}")
print(f"Cost (ticks): {response.usage.cost_in_usd_ticks}")
Streaming and agentic loops
On the OpenAI-compatible SDK, set stream_options: { "include_usage": true } so the final empty-choices chunk carries usage (and therefore cost_in_usd_ticks). Intermediate chunks omit usage. With the xAI SDK stream helper, read response.cost_usd after the loop finishes. When the request uses server-side tools such as web search or code execution, the returned ticks already cover every decode and tool call in that agentic loop — you do not add them up yourself for a single request.
Sum a multi-turn session
cost_in_usd_ticks is per request, not cumulative. Keep a running total in your app after each turn (add response.cost_usd or ticks / 1e10). Image and video responses expose the same field on their usage object; Batch jobs expose per-request costs plus a batch-level cost_breakdown covered in the sibling Batch cost post.
Pitfalls
Treating ticks as cents or as raw dollars understates or overstates spend by orders of magnitude — always divide by 1e10. Expecting a streaming OpenAI client to show cost without include_usage leaves you with no usage object until you enable it. Assuming the Vercel AI SDK surfaces cost_in_usd_ticks today is wrong — the Cost Tracking page says to use the OpenAI SDK or REST for that field. Mixing Console prepaid balance charts with per-request ticks without converting units will not reconcile cleanly.