Track request cost in usd ticks
Track request cost in usd ticks
Every inference response includes usage.cost_in_usd_ticks: the amount billed for that request after prompt-cache discounts, including token cost and server-side tool invocations. The field is on chat completions, Responses, image generation, and video generation. 1 USD = 10,000,000,000 ticks (10^10). Convert with cost_usd = cost_in_usd_ticks / 10_000_000_000. Example: 37756000 ticks is $0.0038.
The xAI SDK exposes response.cost_usd already converted, plus raw ticks on response.usage.cost_in_usd_ticks. Vercel AI SDK (@ai-sdk/xai) does not currently surface the field; use the xAI SDK, OpenAI SDK, or REST.
xAI SDK
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.6",
messages=[user("Say hello")],
)
response = chat.sample()
# Convenience property — ticks converted to dollars.
print(f"Cost: ${response.cost_usd:.6f}")
# Raw ticks for integer-precision accounting.
print(f"Cost (ticks): {response.usage.cost_in_usd_ticks}")
curl
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'
OpenAI SDK
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"}],
)
# cost_in_usd_ticks is available directly on the usage object.
cost_ticks = completion.usage.cost_in_usd_ticks
cost_usd = cost_ticks / 1e10
print(f"Cost: ${cost_usd:.6f}")
Streaming (xAI SDK)
Each stream chunk carries a running cost_in_usd_ticks total. The last chunk is the final cost. After chat.stream() finishes, read it off the assembled response:
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.6",
messages=[user("Tell me a joke")],
)
for response, chunk in chat.stream():
print(chunk.content, end="", flush=True)
print()
# After the stream completes, cost is on the final response.
print(f"Cost: ${response.cost_usd:.6f}")
On OpenAI SDK or REST streams, set stream_options: { include_usage: true }. Cost lands only on the final chunk (empty choices). Intermediate chunks have no usage.
cost_in_usd_ticks is per request. Sum it yourself across turns. With server-side tools, the one value already covers every decode and every tool call in that agentic loop; response.server_side_tool_usage lists which tools ran. Image (grok-imagine-image-2.0) and video (grok-imagine-video-1.5) responses use the same field. Batch results include per-request costs plus cost_breakdown on the batch object.
Pitfalls
@ai-sdk/xaidoes not currently surfacecost_in_usd_ticks. Read it from REST or the OpenAI / xAI SDKs.- Streaming over OpenAI/REST omits usage unless
stream_options.include_usageis true, and then only on the last chunk. - Ticks do not accumulate across conversation turns. Add them in your app.
- Console API credits are separate from SuperGrok's weekly pool on grok.com.