Grok API (November 2026): Pricing, How to Get an API Key, Models, Imagine and Code Examples
On this page
Short answer: yes, Grok has an API. It's xAI's pay-as-you-go API at https://api.x.ai/v1, and 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. The cheapest text models are Grok 4.3 and the Grok 4.20 models at $1.25 / $2.50. Grok Imagine images cost $0.02 or $0.04 each. There's no free tier and no free credits: 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 November 23, 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.
Is there a free Grok API?
No. xAI's quickstart tells you to load your account with credits before you start, and its pricing page lists no free API tier or free credits.
The X Subscription plans (Free $0, Plus $8, Premium $30, Super $100, Ultra $200 a month) are a different product. They include usage inside the Grok apps and Cursor, and xAI's documentation doesn't describe them as including developer API credits. Grok Build, xAI's coding tool, also has a free tier, but that's for the tool, not for the API. Older Reddit threads about free Grok API credits describe programs that aren't in xAI's current documentation.
The cheapest way to try the API is a small credit top-up and grok-4.3, where 1,000 short chat requests cost a few dollars (see the math below).
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 |
Retired: grok-build-0.1 ($1 / $2) reached end of life on November 19, 2026, according to xAI's release notes, and is no longer served on the API, although it still appears in xAI's pricing table. Use grok-4.7 for coding instead.
Not on the API: Grok 4.7 Fast is the same model on faster hardware at $4 / $1 cached / $12. xAI offers it only inside Cursor and Grok Build, billed through those plans.
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-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.
grok-imagine-image-2.0 is billed at the quality it's served at. xAI's migration guide gives $0.04 for a 1K image and $0.06 for a 2K image at low quality, plus $0.01 per input image when editing. Check usage.cost_in_usd_ticks on each response to see what a request actually cost.
Retired: grok-imagine-image-quality (it was $0.05 per image) was retired on November 2, 2026. Requests to it, and to the older grok-imagine-image-pro, are now served by grok-imagine-image-2.0 at low quality and billed at that price. Your code keeps working, but switching the model name lets you pick the quality yourself.
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, file attachment search $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 every text model in the table above (
grok-4.7,grok-4.6,grok-4.5,grok-4.3and thegrok-4.20models), on all token types. Most batch jobs finish within 24 hours. Image and video jobs in a batch are billed at standard rates. - Priority processing costs 2x standard rates. It isn't available for image, video or batch requests.
- 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 ($2.20 / $0.55 / $6.60 on Grok 4.7). - Voice:
grok-voice-3.0speech-to-speech costs $0.08 a minute. Text to speech is $15 per million characters. - Policy violations: a request blocked for breaking xAI's usage guidelines before generation costs $0.05.
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 November 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 short prompts (under 200K tokens for Grok). Prices for the other providers are from OpenAI's, Anthropic's and Google's own pricing pages, checked November 23, 2026. Our OpenAI, Claude and DeepSeek pricing pages go into more detail.
| Model | Input | Output |
|---|---|---|
| Grok 4.7 | $2.00 | $6.00 |
| Grok 4.3 | $1.25 | $2.50 |
| OpenAI GPT-6.1 Sol / GPT-6.2 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 (prompts up to 100K) | $0.10 | $0.50 |
| Gemini 3.8 Flash (paid tier, through December 31, 2026) | $0.75 | $3.75 |
Google has announced that Gemini 3.8 Flash rises to $1.50 / $7.50 on January 1, 2027.
Grok 4.7 matches GPT-6.1 Sol, GPT-6.2 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 using the API.
- 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.
There's no free tier to skip step 2; see Is there a free Grok API? above.
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, the default; auto currently uses low for new images and medium for edits). n can be 1 to 10.
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 account and one bill for all of them. api.foo, the service that publishes these docs, serves Grok text models such as grok-4.7 and grok-4.3, plus Grok Imagine image models such as grok-imagine-image and grok-imagine-image-2.0, 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 November 23, 2026: Pricing, Models, Release notes, grok-imagine-image-quality retirement, 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 November 23, 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. The cheapest text models on the API, Grok 4.3 and the Grok 4.20 models, cost $1.25 and $2.50. Prompts of 200,000 tokens or more are billed at double rates for the whole request.
Is there a free Grok API or free tier?
No. xAI's quickstart says to load your account with credits before you use the API, and its pricing page lists no free tier or free credits. The X Subscription plans, including the $0 Free plan, 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 and GPT-6.2 Sol cost $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 and $0.04 with grok-imagine-image-2.0. 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. The older grok-imagine-image-quality model was retired on November 2, 2026, and its requests now run on grok-imagine-image-2.0 at low quality.
Why does the Grok API return 400 for a wrong API key?
That's how the API behaves today. In our test on November 23, 2026, 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 support@api.foo.