Grok API Setup Guide for Python: Registration, SDK Installation & Streaming Chat
Getting Started with the Grok API for Python Development
xAI’s Grok API gives Python developers access to one of the most capable large language models available today. This guide walks you through every step — from creating your xAI account to building streaming chat completions and configuring function calling — so you can integrate Grok into your applications quickly and confidently.
Step 1: Register on the xAI Console
- Navigate to console.x.ai and click Sign Up.- Authenticate with your X (Twitter) account or create a standalone xAI account using your email address.- Complete email verification and accept the Terms of Service.- Once logged in, you land on the xAI Console dashboard where you can manage API keys, monitor usage, and review billing.
Step 2: Generate Your API Key
- In the console sidebar, click API Keys.- Click Create API Key.- Give the key a descriptive name (e.g.,
my-python-project).- Copy the key immediately — it will not be shown again.- Store it securely using environment variables or a secrets manager.Set the key as an environment variable on your system:# Linux / macOS export XAI_API_KEY=“YOUR_API_KEY”
Windows PowerShell
$env:XAI_API_KEY=“YOUR_API_KEY”
Step 3: Install the Python SDK
The Grok API is compatible with the OpenAI SDK, which means you can use the familiar openai Python package with a custom base URL. Install the required packages:
pip install openai python-dotenv
Optionally, create a .env file for local development:
XAI_API_KEY=YOUR_API_KEY
## Step 4: Configure the Client
Initialize the OpenAI client pointed at the xAI endpoint:
import os
from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
client = OpenAI(
api_key=os.getenv(“XAI_API_KEY”),
base_url=“https://api.x.ai/v1”,
)
Step 5: Send Your First Chat Completion
Make a basic non-streaming request to the Grok model:
response = client.chat.completions.create(
model="grok-3-latest",
messages=[
{"role": "system", "content": "You are a helpful coding assistant."},
{"role": "user", "content": "Explain Python list comprehensions in three sentences."},
],
temperature=0.7,
max_tokens=512,
)
print(response.choices[0].message.content)
Step 6: Enable Streaming Chat Completions
Streaming returns tokens incrementally, improving perceived latency in user-facing applications:
stream = client.chat.completions.create(
model="grok-3-latest",
messages=[
{"role": "system", "content": "You are a concise technical writer."},
{"role": "user", "content": "Write a quick guide to Python decorators."},
],
stream=True,
)
for chunk in stream:
delta = chunk.choices[0].delta.content
if delta:
print(delta, end="", flush=True)
print() # newline after stream ends
Step 7: Configure Function Calling
Function calling lets Grok invoke tools you define, enabling agentic workflows. Define your tools as JSON schemas and pass them in the request:
import json
tools = [
{
“type”: “function”,
“function”: {
“name”: “get_weather”,
“description”: “Get the current weather for a given city.”,
“parameters”: {
“type”: “object”,
“properties”: {
“city”: {
“type”: “string”,
“description”: “City name, e.g. San Francisco”,
},
“unit”: {
“type”: “string”,
“enum”: [“celsius”, “fahrenheit”],
“description”: “Temperature unit”,
},
},
“required”: [“city”],
},
},
}
]
response = client.chat.completions.create(
model=“grok-3-latest”,
messages=[
{“role”: “user”, “content”: “What is the weather in Tokyo?”}
],
tools=tools,
tool_choice=“auto”,
)
tool_call = response.choices[0].message.tool_calls[0]
print(f”Function: {tool_call.function.name}”)
print(f”Arguments: {tool_call.function.arguments}“)
After receiving the tool call, execute your function locally and return the result back to the model:
# Simulate function execution
weather_result = json.dumps({“city”: “Tokyo”, “temp”: 22, “unit”: “celsius”, “condition”: “Partly cloudy”})
Send the tool result back
follow_up = client.chat.completions.create(
model=“grok-3-latest”,
messages=[
{“role”: “user”, “content”: “What is the weather in Tokyo?”},
response.choices[0].message,
{
“role”: “tool”,
“tool_call_id”: tool_call.id,
“content”: weather_result,
},
],
)
print(follow_up.choices[0].message.content)
Available Grok Models
| Model ID | Context Window | Best For |
|---|---|---|
grok-3-latest | 131,072 tokens | Complex reasoning, coding, analysis |
grok-3-mini-latest | 131,072 tokens | Fast responses, lightweight tasks |
grok-2-latest | 131,072 tokens | General-purpose, balanced performance |
AsyncOpenAI from the openai package to run multiple requests concurrently — ideal for processing large datasets.- **Monitor usage in the console:** The xAI Console provides real-time token usage and cost dashboards. Set billing alerts to avoid unexpected charges.- **Structured outputs with JSON mode:** Add response_format={"type": "json_object"} to your request and instruct the model to return JSON in the system prompt for reliable structured data extraction.- **Combine multiple tools:** You can define up to 128 tools in a single request. Grok can call multiple tools in parallel when the query requires it.
## Troubleshooting Common Errors
| Error | Cause | Fix |
|---|---|---|
401 Unauthorized | Invalid or missing API key | Verify XAI_API_KEY is set correctly and the key is active in the console. |
429 Too Many Requests | Rate limit exceeded | Implement exponential backoff. Check your plan's rate limits in the console. |
404 Not Found | Wrong base URL or model ID | Ensure base_url is https://api.x.ai/v1 and the model name is valid. |
openai.APIConnectionError | Network issue or firewall block | Check internet connectivity. Ensure your firewall allows outbound HTTPS to api.x.ai. |
Empty tool_calls | Model did not trigger function call | Make your function descriptions more specific, or use tool_choice="required" to force a call. |
Is the Grok API free to use?
xAI offers free API credits for new accounts so you can experiment without charge. After the free tier is exhausted, usage is billed per million tokens. Check the xAI Console pricing page for current rates on each model.
Can I use the Grok API with frameworks like LangChain or LlamaIndex?
Yes. Because the Grok API follows the OpenAI-compatible specification, any framework that supports custom OpenAI base URLs works out of the box. In LangChain, use ChatOpenAI with openai_api_base=“https://api.x.ai/v1” and pass your xAI API key.
What is the difference between grok-3-latest and grok-3-mini-latest?
grok-3-latest is the full-size flagship model optimized for complex reasoning, advanced coding, and multi-step analysis. grok-3-mini-latest is a smaller, faster variant suited for simpler tasks where low latency and cost efficiency matter more than peak capability. Both share the same 131K context window.