Intermediate
Grok API
The xAI API gives developers access to Grok models via a REST API at api.x.ai/v1. It is OpenAI-compatible — meaning existing OpenAI SDK code works with a base URL change.
Pricing (May 2026)
| Model | Input / 1M | Output / 1M | Context | Notes |
|---|---|---|---|---|
| grok-4.3 | $1.25 | $2.50 | 1M tokens | Current flagship — best quality |
| grok-3-mini | $0.30 | $0.50 | 131K tokens | Cheaper; chain-of-thought by default |
| grok-3 (legacy) | $0.30 | $0.50 | 131K tokens | Legacy — prefer grok-3-mini |
| grok-2-vision-1212 | $2.00 | $10.00 | 32K tokens | Vision (image input) — legacy pricing |
Free Developer Credits
$150/month in free API credits
xAI offers developers $150/month in free API credits through a data-sharing program. At Grok 4.3 prices ($1.25/$2.50 per 1M tokens), this covers around 50M input tokens or substantial experimentation. Sign up at console.x.ai to claim credits. This makes Grok one of the most cost-effective models to experiment with at real scale.
Quick Start
pip install openai
from openai import OpenAI
client = OpenAI(
api_key="your_xai_api_key",
base_url="https://api.x.ai/v1"
)
response = client.chat.completions.create(
model="grok-4.3",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What's trending on X right now?"}
]
)
print(response.choices[0].message.content)API Features
OpenAI-compatible
Same request/response format as OpenAI. Change base_url and api_key — existing code works. Compatible with LangChain, LlamaIndex, and any OpenAI SDK.
Streaming
All models support streaming. Set stream=True for token-by-token output — reduces perceived latency in user-facing applications.
Tool / Function Calling
Grok 4.3 and Grok 3 Mini support tool calling — define functions in JSON schema, model decides when to invoke them. Use for building Grok-powered agents.
Live Search (X data)
Optional live_search parameter connects the model to real-time X/Twitter data during inference. Responses include cited sources from X posts.
Vision (image input)
grok-2-vision-1212 accepts image + text input. Send base64-encoded images or image URLs. Useful for image analysis tasks.
JSON mode
Force structured JSON output — useful for data extraction. Set response_format: {type: 'json_object'} in the API call.
Live Search — Real-Time X Data
response = client.chat.completions.create(
model="grok-4.3",
messages=[{"role": "user", "content": "What are people saying about the latest AI releases?"}],
extra_body={"search_parameters": {"mode": "on"}} # enable live X search
)
# Response includes citations from real X postsWhen to Use the Grok API
- Current events and trending topics — the only commercial API with native X/Twitter data access
- Long document processing — Grok 4.3's 1M context window handles very large inputs
- Cost-sensitive experimentation — $150/month free credits make it cheap to prototype
- Alternative reasoning model — Grok 3 Mini uses chain-of-thought by default at competitive prices
Checklist: Do You Understand This?
- Can you make a Grok API call using the OpenAI Python SDK?
- Do you know the current flagship model's price and context window?
- Do you understand how to enable live X search in an API call?
- Can you claim the $150/month free developer credits?