Grok API (October 2026): Pricing, How to Get an API Key, Models, Imagine and Code Examples
On this page
Short answer: the Grok API is xAI's pay-as-you-go API at https://api.x.ai/v1. It's OpenAI-compatible, so the official OpenAI SDKs work by changing the base URL. Grok 4.7, the model xAI recommends for code and chat, costs $2 input / $0.50 cached / $6 output per million tokens. Grok 4.3 costs $1.25 / $2.50. Grok Imagine images cost $0.02–$0.05 each. There's no free tier: you create a key at console.x.ai and prepay credits.
Prices and limits below are from xAI's documentation at docs.x.ai on October 17, 2026. xAI now operates as SpaceXSI; the API, console and docs domains haven't changed. We checked the request formats and error messages ourselves with test keys.
Grok API pricing
Text models
Prices per million tokens, in USD:
| Model | Context | Input | Cached input | Output |
|---|---|---|---|---|
grok-4.7 |
500K | $2.00 | $0.50 | $6.00 |
grok-4.6 |
500K | $2.00 | $0.50 | $6.00 |
grok-4.5 |
500K | $2.00 | $0.30 | $6.00 |
grok-4.3 |
1M | $1.25 | $0.20 | $2.50 |
grok-4.20-0309-reasoning / -non-reasoning |
1M | $1.25 | $0.20 | $2.50 |
grok-4.20-multi-agent-0309 |
1M | $1.25 | $0.20 | $2.50 |
grok-build-0.1 |
256K | $1.00 | $0.20 | $2.00 |
The 200K rule: when a prompt reaches 200,000 tokens, every token in that request (input, cached input and output) is billed at double the rates above. A 199K-token prompt with a 2K-token answer on Grok 4.7 costs about $0.41. At 200K tokens the same request costs about $0.82. If you work with long documents, split or trim them to stay under the line.
Reasoning models also produce reasoning tokens, which xAI lists as a separately billed token type alongside input, completion, image and cached tokens. Budget for them the way you budget for output.
Grok Imagine: images and video
| Model | Price |
|---|---|
grok-imagine-image |
$0.02 per image |
grok-imagine-image-2.0 |
$0.04 per image |
grok-imagine-image-quality |
$0.05 per image |
grok-imagine-video-1.5-lite |
$0.02 per second |
grok-imagine-video |
$0.05 per second |
grok-imagine-video-1.5 |
$0.08 per second |
xAI recommends grok-imagine-image-2.0 for images and grok-imagine-video-1.5 for video. A 10-second clip costs $0.20 on 1.5 Lite and $0.80 on 1.5.
Tools, batch and priority
- Server-side tools are billed per call on top of tokens: web search $5 per 1,000 calls, code execution $5 per 1,000, collections search (RAG) $2.50 per 1,000, and X search $5 per 1,000 posts or $10 per 1,000 profiles returned.
- Batch API: 20% off for
grok-4.3and thegrok-4.20models. Grok 4.7, 4.6, 4.5 and Build get no batch discount. Image and video jobs in a batch are billed at standard rates. - Priority processing costs 2x standard rates.
- US regional endpoint (
https://us.api.x.ai/v1) keeps processing in the United States. It serves onlygrok-4.7andgrok-4.6, no image, video or voice, and costs 10% more.
How much do 1,000 tokens cost?
On Grok 4.7, 1,000 input tokens cost $0.002 and 1,000 output tokens cost $0.006. A typical chat turn with a 2,000-token prompt and a 500-token answer costs about $0.007, so 1,000 such requests cost about $7. On Grok 4.3 the same 1,000 requests cost about $3.75.
Cost calculator
Estimate a monthly bill at xAI's October 2026 list prices. The calculator prices reasoning tokens like output, so add them to the output field for a conservative estimate.
Grok API vs ChatGPT, Claude and Gemini APIs
Standard input / output prices per million tokens for prompts under 200K tokens. Prices for the other providers are from our OpenAI, Claude and DeepSeek pricing pages and Google's pricing page, checked October 15–17, 2026.
| Model | Input | Output |
|---|---|---|
| Grok 4.7 | $2.00 | $6.00 |
| Grok 4.3 | $1.25 | $2.50 |
| OpenAI GPT-6.1 Sol | $2.00 | $10.00 |
| OpenAI GPT-6 Luna | $0.10 | $0.50 |
| Claude Sonnet 5.5 | $2.00 | $10.00 |
| Claude Haiku 5.5 | $0.10 | $0.50 |
| Gemini 3.8 Flash (paid tier) | $0.75 | $3.75 |
| DeepSeek V4 Pro (peak / off-peak) | $1.32 / $0.66 | $3.96 / $1.98 |
Grok 4.7 matches GPT-6.1 Sol and Claude Sonnet 5.5 on input and costs 40% less on output. For high-volume simple tasks, GPT-6 Luna and Claude Haiku 5.5 are much cheaper than any Grok model. Token counts differ between tokenizers, so compare on your real prompts.
How to get a Grok API key
- Sign up at console.x.ai.
- Add credits. xAI's quickstart tells you to load the account with credits before you start, and requests fail without them.
- Open API Keys in the console and create a key.
- Save it as an environment variable:
export XAI_API_KEY="your_api_key"
On Windows PowerShell, use setx XAI_API_KEY "your_api_key" and open a new terminal.
Is there a free tier?
Not today. xAI's pricing and quickstart pages list no free API tier, and you prepay credits before calling the API. The X Subscription plans (Free, Plus $8, Premium $30, Super $100, Ultra $200 a month) include usage inside the Grok apps and Cursor, and their documentation doesn't describe developer API credits. Older Reddit threads about free Grok API credits describe programs that aren't in xAI's current documentation.
Quickstart code
curl
curl https://api.x.ai/v1/chat/completions \
-H "Authorization: Bearer $XAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "grok-4.7", "messages": [{"role": "user", "content": "Explain what an API rate limit is in one sentence."}]}'
Python with the OpenAI SDK
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["XAI_API_KEY"], base_url="https://api.x.ai/v1")
resp = client.chat.completions.create(
model="grok-4.7",
messages=[{"role": "user", "content": "Explain what an API rate limit is in one sentence."}],
)
print(resp.choices[0].message.content)
print(resp.usage) # cached tokens appear in usage.prompt_tokens_details.cached_tokens
xAI also supports the Responses API (client.responses.create(model="grok-4.7", input="...")), which is what its own quickstart uses. Other SDKs: npm install ai @ai-sdk/xai for the Vercel AI SDK, and pip install xai-sdk for xAI's own Python SDK.
Generate an image
curl https://api.x.ai/v1/images/generations \
-H "Authorization: Bearer $XAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "grok-imagine-image-2.0", "prompt": "A red bicycle leaning on a brick wall, film photo", "n": 2, "aspect_ratio": "16:9"}'
The response contains image URLs. They're temporary, so download the files promptly or ask for base64 output. Optional parameters include aspect_ratio (default auto), resolution (1k by default, or 2k) and quality (low, medium or auto).
Get more cache hits
Prompt caching is automatic: when consecutive requests start with the same messages, the matching prefix is billed at the cached rate. xAI recommends sending an x-grok-conv-id header with a stable ID per conversation to raise the hit rate. Keep system prompts and tool definitions at the start and unchanged between turns.
Rate limits
Limits are per team and per model, in requests per second (RPS) and tokens per minute (TPM). Your tier depends on cumulative API spend since January 1, 2026, and never goes down:
| Tier | Total spend | grok-4.7 limits | grok-4.3 limits |
|---|---|---|---|
| Tier 0 | $0 | 150 RPS, 50M TPM | 37 RPS, 10M TPM |
| Tier 1 | $50 | 172 RPS, 53M TPM | 50 RPS, 15M TPM |
| Tier 2 | $250 | 208 RPS, 60M TPM | 75 RPS, 25M TPM |
| Tier 3 | $1,000 | 312 RPS, 74M TPM | 125 RPS, 45M TPM |
| Tier 4 | $5,000 | 500 RPS, 100M TPM | 208 RPS, 85M TPM |
Image models start at 6 requests per second and video models at 10 at Tier 0. Exceeding a limit returns 429 Too Many Requests. Your current tier and limits are on the Models page of the xAI console.
Errors you'll actually see
We sent requests with a wrong key and with no key to see the real responses:
| Situation | Status | Response |
|---|---|---|
| Wrong or revoked API key | 400 | {"code":"invalid-argument","error":"Incorrect API key provided. You can obtain an API key from https://console.x.ai."} |
| No API key sent | 401 | {"code":"unauthenticated:no-credentials","error":"No credentials presented."} |
| Too many requests | 429 | Rate limit reached; wait and retry with backoff |
| No permission | 403 | Ask your team admin in the console |
| Malformed field | 422 | A field in the body has the wrong format |
Note the first row: xAI's debugging page lists 401 for invalid tokens, but a wrong key actually returns 400. With the OpenAI Python SDK that's raised as openai.BadRequestError, not AuthenticationError, so catch both if your code handles bad keys. For outages, check status.x.ai.
Using Grok through a multi-model gateway
If Grok is one of several models you call, a gateway lets you use one key and one bill for all of them. api.foo, the service that publishes these docs, serves Grok text models such as grok-4.7, grok-4.3 and grok-build-0.1, plus grok-imagine-image and grok-imagine-image-quality, at the same OpenAI-compatible paths (https://api.foo/v1/chat/completions, /v1/responses and /v1/images/generations). We confirmed each path reaches the gateway with a test key. Model names and prices are on the api.foo pricing page.
If you only use Grok, xAI's own API is the direct route and gives you the batch discount, regional endpoint and server-side tools.
Related
- Claude Code vs Codex vs Cursor vs OpenCode
- OpenAI API pricing and calculator
- DeepSeek API pricing and calculator
- What is an OpenAI-compatible API?
- API errors: 401, 429 and how to fix them
Sources
xAI (SpaceXSI) documentation, checked October 17, 2026: Pricing, Models, Quickstart, Rate limits, Prompt caching, Regional endpoints, Image generation, Debugging errors and The X Subscription pricing.
FAQ
How much does the Grok API cost?
As of October 17, 2026, Grok 4.7, xAI's main model, costs $2 per million input tokens, $0.50 per million cached input tokens and $6 per million output tokens for prompts under 200,000 tokens. Grok 4.3 costs $1.25 and $2.50, and Grok Build 0.1 costs $1 and $2. Prompts of 200,000 tokens or more are billed at double rates for the whole request.
Is the Grok API free?
No. xAI's quickstart says to load your account with credits before you use the API, and its pricing pages list no free tier. The X Subscription plans cover usage inside the Grok apps and Cursor; xAI's documentation doesn't describe them as including developer API credits.
How do I get a Grok API key?
Sign up at console.x.ai, add credits to your team, then create a key on the API Keys page. Store it in the XAI_API_KEY environment variable and call https://api.x.ai/v1 with the OpenAI SDK, the Vercel AI SDK or xAI's own xai-sdk.
Is the Grok API cheaper than the ChatGPT API?
For output, yes. Grok 4.7 costs $2 input and $6 output per million tokens, while OpenAI's GPT-6.1 Sol costs $2 and $10. Input costs the same. Grok 4.3 is cheaper still at $1.25 and $2.50, and OpenAI's GPT-6 Luna at $0.10 and $0.50 is far cheaper than any Grok text model.
How much does Grok Imagine cost through the API?
Images cost $0.02 each with grok-imagine-image, $0.04 with grok-imagine-image-2.0 and $0.05 with grok-imagine-image-quality. Video is billed per second, from $0.02 a second with grok-imagine-video-1.5-lite to $0.08 a second with grok-imagine-video-1.5.
Why does the Grok API return 400 for a wrong API key?
That's how the API behaves today. In our test, an incorrect key returned HTTP 400 with "Incorrect API key provided", which the OpenAI Python SDK raises as BadRequestError, not AuthenticationError. A request with no key at all returns 401 "No credentials presented."
Found a mistake? Email [email protected].