Grok API: Pricing, Keys & How to Call It
The Grok API is xAI's programmatic access to the Grok family — Grok 4 (the flagship), Grok Code Fast (a cheap, fast coding model), and Grok 3. Grok 4 is $3/$15 per million tokens, but the real story for developers is Grok Code Fast 1 at $0.20/$1.50 — one of the cheapest dedicated coding endpoints anywhere. This guide has the verified price table, how to get an xAI key, and how to call it (it's OpenAI-compatible), direct or via a gateway.
What the Grok API is
The Grok API is xAI's access to the Grok family — Grok 4 (the frontier flagship), Grok Code Fast (a cheap, fast coding model), and the Grok 3 tier. It's OpenAI-compatible, so it drops into existing OpenAI-SDK code. For developers, the standout isn't the flagship — it's how cheap the coding and mini tiers are.
Grok API pricing
| Model | Input | Output | Context |
|---|---|---|---|
| Grok 4 | $3 | $15 | 256K |
| Grok 3 | $3 | $15 | 131K |
| Grok Code Fast 1 | $0.20 | $1.50 | 256K |
| Grok 3 Mini | $0.30 | $0.50 | 131K |
Per 1M tokens, USD, June 2026. Grok Code Fast 1 is among the cheapest dedicated coding endpoints available.
Grok models by price
The flagship and the cheap coding tier are an order of magnitude apart on output price — which makes the cheap tiers the interesting ones for production:
How to get a Grok (xAI) API key
- Sign up at the xAI developer console (console.x.ai) and verify your account.
- Add billing/credit.
- Create an API key and copy it once; store it as
export XAI_API_KEY=...
How to call the Grok API
Grok is OpenAI-compatible — use the OpenAI SDK with xAI's base URL:
from openai import OpenAI
client = OpenAI(base_url="https://api.x.ai/v1", api_key="$XAI_API_KEY")
resp = client.chat.completions.create(
model="grok-code-fast-1", # or grok-4, grok-3
messages=[{"role": "user", "content": "Implement and test this function..."}],
)
print(resp.choices[0].message.content)
Which Grok model to use
Grok 4 Frontier tasks
- General frontier-level reasoning. $3/$15 — mid-priced versus the US frontier.
Grok Code Fast Cheap coding
- $0.20/$1.50, 256K context — high-volume coding-agent loops where speed and cost beat peak reasoning.
Grok 3 Mini Cheapest general
- $0.30/$0.50 — the cheapest general Grok calls for simple tasks.
Pattern First-pass + escalate
- Run Grok Code Fast as the cheap first pass, escalate to a stronger model on failure.
Calling Grok through a gateway
DataLLM Lab carries Grok 4, Grok Code Fast 1, and Grok 3 today — reachable with one OpenAI-compatible key alongside Claude, GPT, Gemini and DeepSeek, with automatic failover:
client = OpenAI(base_url="https://www.datallmlab.com/v1", api_key="$DATALLMLAB_API_KEY")
# reaches x-ai/grok-4, x-ai/grok-code-fast-1, anthropic/claude-opus-4.7, ...
Call Grok and 300+ models with one key
Grok 4, Grok Code Fast, Claude Opus 4.7, GPT-5.4, DeepSeek V3.2 and more — one OpenAI-compatible endpoint, live price comparison, automatic failover.
FAQ
How much does the Grok API cost?
Per 1M tokens: Grok 4 $3/$15; Grok 3 $3/$15; Grok 3 Mini $0.30/$0.50; Grok Code Fast 1 $0.20/$1.50 — a very cheap dedicated coding model.
How do I get a Grok (xAI) API key?
Sign up at console.x.ai, add billing, create a key. OpenAI-compatible at https://api.x.ai/v1. Or reach Grok via a gateway like DataLLM Lab.
Is the Grok API OpenAI-compatible?
Yes — use the OpenAI SDK with base_url https://api.x.ai/v1 and the model id (grok-4, grok-code-fast-1). Switching to/from a gateway is one line.
What is Grok Code Fast?
Grok Code Fast 1 is xAI's low-cost, low-latency coding model ($0.20/$1.50, 256K context), aimed at high-volume coding-agent loops.
Which Grok model should I use?
Grok 4 for general frontier tasks, Grok Code Fast 1 for cheap high-volume coding, Grok 3 Mini for the cheapest general calls. Use the cheap tier first-pass with escalation.
Can I use the Grok API through a gateway?
Yes — DataLLM Lab carries Grok 4, Grok Code Fast 1, and Grok 3, callable through one OpenAI-compatible key with failover.
DataLLM Lab