Grok API is offered by xAI and billed per token, with input and output priced separately. Many teams build a budget by watching the model's unit price, then discover after launch that the bill is far from their estimate. Resending message history on every turn, writing overly long outputs, retrying without backoff, and reloading long documents all multiply the final cost.
This article does not hard-code any unit prices. Instead, it breaks down the structure of Grok API billing and provides a reproducible estimation method plus a sensible order for controlling costs. For current unit prices, available models, and context limits, refer to xAI's official documentation and the model pricing page on this site.
Note: xAI and Grok are trademarks of their respective owners. This article is for technical reference only. Model names, interfaces, and prices may change; check the official pages and model pricing before procurement.
1. What makes up a Grok API bill
Grok API uses an OpenAI-compatible Chat Completions format, and bills are usually dominated by input tokens and output tokens. In real projects, you also need to account for caching, tool calls, long context, and failed retries.
| Billing item | When it occurs | Common cost multipliers | Controls |
|---|---|---|---|
| Input tokens | System prompts, message history, tool definitions, documents | Full history resent every turn | Summaries, trimming, retrieval |
| Cached input | Reusing the same prefix | Unstable prefixes miss the cache | Fix the system prompt and tool order |
| Output tokens | Content generated by the model | Verbose answers, uncontrolled format | Set max_tokens, use structured output |
| Tool calls | Function calling, search, and other add-ons | Multi-turn loops, repeated queries | Cap the number of rounds, cache results |
| Long context | Long documents, long conversations | Dumping entire corpora, repeated reads | Chunk processing, fetch only relevant passages |
| Retries | Resending after a 429 or timeout | No backoff, excessive concurrency | Exponential backoff, idempotency checks |
Different models and capabilities may follow separate billing rules. Whether cached input is charged, whether tool calls are billed separately, and similar details are governed by xAI's official billing documentation. Confirm which billing items apply to your workload before comparing unit prices.
2. Why the same call volume can cost several times more
Two applications can make the same number of calls and receive completely different bills. The difference usually comes from six places:
- Input-to-output ratio. A customer-support bot reads a lot and writes a moderate amount; a content generator reads little and writes a lot. You cannot estimate both with the same average.
- Context accumulation. In a multi-turn conversation, each turn resends the entire history, so input tokens climb quickly as the conversation grows.
- Cache hit rate. A stable prefix can be reused, but timestamps, random IDs, and dynamically generated tool lists break the cache.
- Model tier. Different Grok models differ in capability and unit price. Using a high-end model for a simple task inflates the budget.
- Retry count. Every retry is a new request. Resending immediately after a 429 easily turns into a retry storm.
- Output length. Output is usually priced higher than input, so long answers, repeated explanations, and uncontrolled formats all add cost.
| Scenario | Token structure | Main cost | Optimization direction |
|---|---|---|---|
| Customer-support bot | Much input, moderate output | Message history resent repeatedly | Summaries plus caching |
| Document extraction | Long input, short output | Documents read repeatedly | Retrieval plus a smaller model |
| Coding assistant | Large input and output | Tool results and reasoning traces | Trim tool output, route by model |
| Content generation | Short input, long output | Output tokens | Generate in sections, cap length |
3. Calculating the cost of one request
Breaking a single request into its parts gives the following formula:
Cost per request =
uncached input tokens × input unit price
+ cached input tokens × cache unit price
+ output tokens × output unit price
+ add-on fees for tools or search
The formula itself is simple. The hard part is obtaining real token counts. Do not guess from character counts; read the usage field in the response:
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["GROK_API_KEY"],
base_url=os.environ.get("GROK_BASE_URL", "https://ai.zhongshangqihang.com/v1"),
)
resp = client.chat.completions.create(
model="grok-4",
messages=[{"role": "user", "content": "Break this requirement into five development tasks."}],
max_tokens=512,
)
usage = resp.usage
print("Input tokens:", usage.prompt_tokens)
print("Output tokens:", usage.completion_tokens)
print("Total tokens:", usage.total_tokens)
Multiply by the actual unit price on your account, read from model pricing. Do not rely on a screenshot from several months ago. The field names for cache hits may differ between OpenAI-compatible implementations, so trust whatever the response actually returns.
4. The order in which to reduce cost
Cost reduction has a sequence, and doing it out of order wastes effort. Follow these seven steps:
- Quantify the baseline first. Export a week of real request logs and break input and output tokens down by project, model, and scenario. Know where the money goes before changing anything.
- Route by model. Send classification, extraction, and rewriting to smaller models; reserve high-end models for complex reasoning. Assign by task difficulty, not by team habit.
- Reduce input. Retrieve only relevant passages for RAG, summarize conversation history, and keep only the necessary fields from tool results.
- Limit output. Set a reasonable
max_tokens, request JSON or a fixed set of fields, and cut pleasantries and repeated explanations. - Use caching. Keep the system prompt, tool definitions, and knowledge-base prefix stable, then verify that the cache actually hits.
- Govern retries. Use exponential backoff with jitter for 429s and timeouts, set a maximum retry count, and avoid aggressive retries at fixed intervals.
- Set budgets and alerts. Give each project a spending cap, review the consumption ranking weekly, and investigate abnormal growth promptly.
Fix input usage and caching before switching to a cheaper model. Otherwise you only make unstable output cheaper while rework costs return.
5. Managing budgets and bills
- Use one key per project and set a budget cap per project so services do not compete for the same quota.
- Log at least the timestamp, model, input and output tokens, status code, and request ID. Never log full keys or sensitive content.
- Reconcile monthly across three dimensions: project, model, and scenario. Identify the fastest-growing calls.
- Record savings from caching, model routing, and output limits separately; otherwise you cannot prove that an optimization worked.
- Teams that need RMB payment and invoices can consolidate billing through a domestic AI gateway. For specific prices, exchange rates, and invoice rules, refer to model pricing.
FAQ
Does Grok API charge only for input and output?
Typical bills are dominated by input and output tokens, but cached input, tool calls, search, and other capabilities may have separate billing rules. Check the official billing documentation and your invoice details for the exact list.
Why is an average-token estimate inaccurate?
Averages hide long-tail requests. A small number of very long contexts, long outputs, or retries may account for most of the cost. Reading the distribution from real logs is more reliable than multiplying an average by call volume.
How much can caching save?
It depends on prefix reuse and hit rate. Cache reads are usually cheaper than ordinary input, but cache writes may carry a premium, and a prefix that is too short or changes every time will not hit. Confirm hits in the logs before estimating savings.
Are longer outputs always more expensive?
Usually, yes, because output tokens are billed separately and often at a higher rate than input. Keeping answers to the length the business needs and using structured output is often faster to implement than switching models.
How can teams in mainland China pay in RMB?
You can call Grok through an AI gateway that supports RMB top-ups and invoices, consolidating multi-model billing. Available models, prices, and invoice rules are subject to model pricing.
Next steps
Start by creating a test key at the ZSQH AI Gateway, record input and output tokens on a batch of real tasks, and then apply the formula above to build your budget. Model availability and prices are subject to model pricing.
Related reading:
