
Grok 2 API: Access, Rate Limits & SDKs 2026
The xAI API is OpenAI-compatible and built around one differentiator: server-side Web Search and X Search give Grok live web and X data most rivals lack.
Grok 2 API verdict
The xAI API authenticates with a single bearer key and is OpenAI-compatible.
The native xai-sdk, the OpenAI SDK pointed at https://api.x.ai/v1, or the Vercel AI SDK all call grok-4.3. A grok-build-0.1 model covers coding, and separate Voice and Imagine APIs handle speech and image or video.
xAI's API is a low-friction OpenAI-compatible target with one differentiator worth building around. Server-side Web Search and X Search give grok-4.3 live web and X data that most rivals lack, so it is a strong backend for realtime, current-events and social-listening apps. Keep your OpenAI SDK and swap the base URL to https://api.x.ai/v1, or use the native xai-sdk or Vercel AI SDK for an automatic tool loop. Lean on function calling with Pydantic or Zod schemas, up to 200 tools and parallel by default, and pin a model alias for reproducibility or use -latest for new features. Budget on competitive token pricing, at $1.25 and $2.50 with an 84% cache discount. Remember the base model needs the search tools for recency, and that logprobs are unsupported on current models.
- The base model has no realtime knowledge beyond training data unless server-side Web Search or X Search tools are enabled.
- logprobs and top_logprobs are unsupported on grok-4.20 and newer, and are silently ignored if set.
- A tool whose parameters root is not an object, or a union of objects, cannot be compiled and is rejected with a 400 that names the tool. Max 200 tools per request.
- With streaming, a function call is returned whole in a single chunk rather than streamed across chunks, so parse it as one unit.
- Auth
- Bearer XAI_API_KEY
- Base URL
- api.x.ai/v1 (OpenAI-compat)
- Built-in tools
- Web Search + X Search
- Tools/request
- Max 200
- Context
- 1M tokens
Build with the Grok 2 API
from openai import OpenAI
client = OpenAI(
api_key="<XAI_API_KEY>",
base_url="https://api.x.ai/v1",
)
response = client.chat.completions.create(
model="grok-4.3",
messages=[{"role": "user", "content": "Hello!"}],
)
print(response.choices[0].message.content){
"choices": [{
"message": { "role": "assistant", "content": "Hello! How can I help?" },
"finish_reason": "stop"
}],
"usage": { "prompt_tokens": 9, "completion_tokens": 8 }
}- Cost levers: prompt caching (cache hit $0.20/1M, 84% off); grok-build-0.1 for cheaper coding calls; Batch API for async high-volume jobs.
- Handle: 400 (Invalid tool schema / out-of-spec request).
Real Grok 2 code from the official docs. The raw cURL hello-world is the code block above; full limits and endpoints are in the tables on this page.
Your first Grok 2 API request
curl https://api.x.ai/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${XAI_API_KEY}" \
-d '{
"model": "grok-4.3",
"messages": [{"role": "user", "content": "Hello!"}]
}'How to authenticate with the Grok 2 API
| Aspect | Detail | Notes |
|---|---|---|
| Auth | Bearer XAI_API_KEY | Manage keys in the xAI API console |
| Base URL | https://api.x.ai/v1 (OpenAI-compatible) | Drop-in for the OpenAI SDK |
| SDKs | xai-sdk (Python, JS), OpenAI SDK, Vercel AI SDK | @ai-sdk/xai for the Vercel AI SDK |
| Primary model | grok-4.3 | Most intelligent and fastest; 1M context |
| Coding model | grok-build-0.1 | 256k context, agentic coding workflows |
| Model aliases | <name>, <name>-latest, <name>-<date> | Auto-migrate or pin a release |
| Other APIs | Voice API, Imagine API (image + video) | Voice $3/hr agent; image $0.02/image |
Core endpoints and tools
| Capability | How | Purpose |
|---|---|---|
| Responses API | Generate, multi-turn, function calling | Text generation and conversations |
| Function calling | tools[] with name, description, JSON schema | Custom tools the model invokes; you execute and return |
| Built-in tools | web_search(), x_search() | Realtime web and X data, executed on xAI servers |
| Tool choice | auto / required / none / forced | Control whether and which tool the model calls |
| Parallel tool calls | Enabled by default | Multiple tool calls in one response; disable with parallel_tool_calls:false |
| Structured outputs | Structured outputs feature | JSON conforming to a schema |
| Batch API | Batch API | Asynchronous high-volume jobs |
| Image input | jpg/png, up to 20MiB, no count limit | Multimodal prompts; any text/image order |
Limits, aliases and model rules
| Limit | Detail | Notes |
|---|---|---|
| Context window | 1M tokens (grok-4.3) | 256k for grok-build-0.1 |
| Tools per request | Max 200 tools | Invalid tool schema rejected with 400 naming the tool |
| Image input | jpg/png, up to 20MiB, no count limit | Any text/image order accepted |
| Realtime data | None without search tools enabled | Enable Web Search / X Search for live data |
| logprobs | Not supported on grok-4.20 and newer | Silently ignored if set |
| Role order | No role-order limitation | Mix system/user/assistant in any sequence |
| Tool schema root | Must be an object or union of objects | Scalar/array root rejected with 400 |
SDKs, tools and developer fit
| Aspect | Detail | Notes |
|---|---|---|
| SDKs | xai-sdk (Python/JS), OpenAI SDK, Vercel AI SDK | OpenAI-compatible via base_url swap |
| Tool schemas | JSON Schema or Pydantic / Zod | model_json_schema() generates the schema |
| Built-in vs custom | Built-in run on xAI servers; custom pause for you | web_search/x_search auto, custom returns tool_call |
| Streaming tool calls | Function call returned whole in one chunk | Not streamed across chunks |
| Vercel AI SDK | xai.responses('grok-4.3') with auto tool loop | streamText + tool() + stepCountIs |
| Other surfaces | Voice API (STT/TTS), Imagine API (image/video) | Dedicated models per modality |
| API pricing (grok-4.3) | $1.25/1M in, $2.50/1M out, cache hit $0.20/1M | Independent cross-reference |
Grok 2 API FAQ
How do I authenticate with the xAI API?
Create a key in the xAI API console and send it as a bearer token, as Authorization: Bearer ${XAI_API_KEY}. The native xai-sdk reads the XAI_API_KEY environment variable automatically. The API is OpenAI-compatible, so you can keep the OpenAI SDK and point its base URL at https://api.x.ai/v1, then call model grok-4.3.
Can I use the OpenAI SDK or Vercel AI SDK with Grok?
Yes. xAI ships a native xai-sdk for Python and JavaScript, and is OpenAI-compatible through the OpenAI SDK with base URL https://api.x.ai/v1. The Vercel AI SDK also supports it through @ai-sdk/xai, with an automatic tool loop. Tool parameter schemas can be plain JSON Schema, Pydantic models, or Zod.
How does Grok get realtime data?
The base model has no knowledge of current events beyond its training data. To use live information, enable the built-in server-side tools web_search and x_search, which run on xAI servers and fetch live web and X content during the request. Built-in tools execute automatically, while your custom function tools pause execution and return a tool_call for you to handle.
What are the xAI API limits?
The hard ceilings are a 1M-token context, 256k for grok-build-0.1, and a maximum of 200 tools per request. Images must be jpg or png up to 20MiB, with no count limit and any text or image order. A tool whose parameters root is not an object or union of objects is rejected with a 400 that names the tool, and logprobs and top_logprobs are unsupported on grok-4.20 and newer.
What can the xAI API do beyond text, and how is function calling structured?
Beyond the Responses API, which handles text, multi-turn and function calling, xAI offers a Voice API for STT, TTS and real-time conversations, and an Imagine API for image and video generation. Function calling defines tools by name, description and JSON-schema parameters. Tool choice can be auto, required, none or a forced function. Parallel tool calls are on by default and disabled with parallel_tool_calls:false, and with streaming a function call is returned whole in a single chunk.
Sources & verification
| Source | What was checked | Last checked |
|---|---|---|
| X Grok | Official product page | July 10, 2026 |
| Artificial Analysis | Independent reference | July 10, 2026 |
| X Developer docs | Overview | July 10, 2026 |
| X Developer docs | Developers Models | July 10, 2026 |
| X Developer docs | Tools Function Calling | July 10, 2026 |
Every fact on this Grok 2 page is tied to a named source and a verification date. Freshness-sensitive figures trace to the sources above; verify against the vendor before relying on them.
Explore Grok 2
Every page on Grok 2 in one place, you are on api.
