
Read API request cost in usd ticks
Read API request cost in usd ticks
Every xAI inference response puts the billed amount for that call in usage.cost_in_usd_ticks. One USD equals 10_000_000_000 ticks (10^10). Divide ticks by 1e10 for dollars. The value is the actual charge after prompt-cache discounts and includes token cost plus any server-side tool calls in that request.
Convert ticks
cost_usd = cost_in_usd_ticks / 10_000_000_000
Example: 37756000 ticks is $0.0037756. An image at 200000000 ticks is $0.02.
Responses / Chat Completions
curl https://api.x.ai/v1/responses \
-H "Authorization: Bearer $XAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "grok-4.6",
"input": "Say hello"
}' | jq '.usage.cost_in_usd_ticks'
import os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("XAI_API_KEY"),
base_url="https://api.x.ai/v1",
)
completion = client.chat.completions.create(
model="grok-4.6",
messages=[{"role": "user", "content": "Say hello"}],
)
ticks = completion.usage.cost_in_usd_ticks
print(f"Cost: ${ticks / 1e10:.6f}")
The xAI SDK exposes response.cost_usd (dollars) and response.usage.cost_in_usd_ticks (integer ticks).
Streaming
With the OpenAI SDK or raw REST, set stream_options: { "include_usage": true }. Cost appears only on the final chunk (empty choices). The xAI SDK carries a running total on each chunk; the assembled response holds the final cost.
Multi-turn and tools
cost_in_usd_ticks is per request. Sum turns yourself for a session total. When the model uses web search, X search, or code execution inside one call, the single returned cost covers all internal model steps and tool invocations for that request.
Imagine
Image and video responses use the same usage.cost_in_usd_ticks field. Batch jobs expose per-result ticks and a batch cost_breakdown — see Run a Batch API job.
Pitfalls
- Expecting Vercel AI SDK (
@ai-sdk/xai) to surface ticks — it does not yet; use OpenAI SDK or REST. - Reading intermediate stream chunks for usage without
include_usage: true. - Treating one turn’s cost as a multi-turn total.