In short.
xAI's Cost Tracking guide helps developers read the charge for each xAI API request, but they must add request costs together to track a conversation. The documentation describes a cost_in_usd_ticks response field that reports the amount billed after applicable discounts, including token charges and server-side tool charges. It covers SDK and REST access, streaming, image and video generation, and batch accounting. Its most useful implementation details concern integer precision and avoiding duplicate accumulation of streaming totals. Examples were not executed; SDK compatibility and actual billing accuracy were not assessed.
Researched and written by an AI model and checked by a second AI model. How it works
Covers the complete selected page at [https://docs.x.ai/developers/cost-tracking.md], including its Vercel SDK notice, streaming distinctions, conversation examples, server-side tools, image and video examples, and batch note. Linked prompt-caching and Batch API pages are excluded. API calls, SDK installation, model availability, billing accuracy and failure behavior were not tested.
The overview
Identity
This is xAI's developer guide to reading request costs from its API responses, available at [https://docs.x.ai/developers/cost-tracking.md]. It covers Chat Completions, the Responses API, image generation, video generation and batch results.
Assessment
The guide is useful for adding request charges to application logs, conversation totals or usage displays. It supplies concrete fields and examples, including important differences between streaming clients. It does not establish a complete spending-control system or demonstrate reconciliation against invoices.
Users
The intended readers are developers integrating xAI into applications. Beginners get a direct conversion formula and short examples; experienced developers get details about precision, streaming totals, tool charges and batch aggregation. Reading the examples assumes familiarity with API requests, response objects and environment variables.
Capabilities
The page documents per-request charges through usage.cost_in_usd_ticks and the xAI SDK's cost_usd convenience property. According to the documentation, the returned charge is the amount billed after applicable discounts, including prompt caching reductions, and combines token and server-side tool charges.
It also explains conversation accumulation, streaming access, image and video response costs, and batch totals through individual results or the batch object's cost_breakdown.
Using it
Requirements
The examples require an xAI API key supplied through XAI_API_KEY and access to the relevant API model. They use the xAI Python SDK, the OpenAI Python or JavaScript SDK configured for xAI, or direct REST requests. The command-line examples also use curl and jq. Minimum SDK versions and account eligibility requirements are not specified.
Mechanism
The API represents charges as integer ticks, with 10,000,000,000 ticks per US dollar. Divide cost_in_usd_ticks by 10,000,000,000 to obtain dollars. Each request has its own charge; an internal sequence of model and server-side tool calls is already included in that request's total.
For xAI SDK streaming, successive chunks carry running totals and the assembled response carries the final cost. The OpenAI SDK Chat Completions streaming example requests stream_options with include_usage set to true, then reads usage from the final chunk, whose choices are empty. Running totals should not be added together.
Setup
Choose the client used by your application and supply XAI_API_KEY through its environment. With the OpenAI SDK, configure the base URL as [https://api.x.ai/v1]. Read usage.cost_in_usd_ticks from completed responses, or use response.cost_usd with the xAI SDK.
For streamed Chat Completions through the OpenAI SDK, enable include_usage and handle the final usage chunk separately from text chunks. The page provides Python, JavaScript and shell examples, but no installation commands or pinned dependency versions.
Example
The page illustrates a response containing 37,756,000 ticks. Applying its formula gives $0.0037756, displayed in the guide as approximately $0.0038. This is a conversion example, not a measured result from executing the resource.
For a conversation, retain each request's tick value and add it once to the session total. As an implementation recommendation, sum integer ticks and convert the total for display to preserve the precision that the field provides.
Assessment & evidence
Limitations
The page states that the Vercel AI SDK package @ai-sdk/xai does not expose cost_in_usd_ticks in response metadata. It recommends the OpenAI SDK or direct REST access for that field.
Conversation totals require application-side accumulation. The page does not explain cost recovery after an interrupted stream, charging for failed requests, retry accounting or account-wide invoice reconciliation. Its brief batch section refers readers to another page for details.
Reasoning
The documented request total reduces the accounting work needed when one request invokes several server-side tools. Its usefulness depends on collecting the final value reliably and assigning it to the correct conversation or workload.
The conversation examples accumulate dollar values in floating-point variables. The xAI SDK example also substitutes zero when a cost value is absent. For accounting code, integer accumulation and an explicit missing-cost state would better preserve precision and reveal incomplete records; these are recommendations inferred from the examples.
Alternatives
The page offers several access paths to the same cost information. The xAI SDK provides automatic dollar conversion; the OpenAI SDK configured for xAI exposes the raw usage field; direct REST access allows reading that field without either SDK.
The stated Vercel metadata limitation matters when choosing an integration. Comparisons with external cost dashboards, other providers or invoice-based accounting are not supported by the selected page.
Evidence
The evidence is the complete xAI Cost Tracking documentation page at [https://docs.x.ai/developers/cost-tracking.md].
The page supplies vendor descriptions, code examples and illustrative values. No examples were executed, and no independent response captures, invoice comparisons or runtime measurements establish the documented behavior.
Access & freshness
Costs
The documented field reports the amount charged for a completed request after applicable discounts, including its token and server-side tool charges. It provides retrospective accounting; the page does not describe predicting or limiting a request's charge.
An image example shows 400,000,000 ticks, equivalent to $0.04. That illustrative value does not establish a general image price. Current model rates, free allowances, additional fees and the total cost of a particular workload are unknown from this page.
Rights
The selected page supplies no explicit licence for its documentation or code examples. Permission to redistribute them, the SDK licences and applicable API service terms are not established by this evidence.
Freshness
This page was captured on 2026-11-15 at 05:33:38.520 UTC. No publication date, last-updated date or SDK compatibility range appears in the evidence.
Examples name grok-4.7, grok-imagine-image-2.0 and grok-imagine-video-1.5. Their current availability and the continued Vercel SDK limitation were not independently verified.
Actions
Use the guide to identify where your chosen client exposes the final request cost. Retain raw integer ticks, add each request once, and convert totals to dollars for display. For streaming, distinguish running totals from the final charge.
Before relying on the integration for accounting, verify field availability in your installed SDK and compare representative completed requests with billing records. Establish how your application handles missing usage, interrupted streams and retries; the guide leaves those behaviors unspecified.
