Get started with xAI
Build with Grok, the AI model designed to deliver truthful, insightful answers.
Models
We offer a range of models supporting multiple use cases and modalities.
Grok 4.20
The most truth-seeking large language model in the world.
Context- tokens
Input- / 1M tokens
Output- / 1M tokens
Voice API
Capable of real-time conversation, generation and transcription.
ModesAgent, TTS & STT
Realtime$3.00 / hour
TTS$4.20 / 1M characters
Imagine API
Turn ideas into reality with our image and video generation models.
ModesGeneration & editing
Image- / image
Video- / second
Jump straight in
We've prepared some simple examples for you to try.
import os
from xai_sdk import Client
from xai_sdk.chat import user, system
client = Client(
api_key=os.getenv("XAI_API_KEY"),
)
chat = client.chat.create(model="grok-4.20-reasoning")
chat.append(system("You are Grok, a chatbot inspired by the Hitchhiker's Guide to the Galaxy."))
chat.append(user("What is the meaning of life, the universe, and everything?"))
response = chat.sample()
print(response)from openai import OpenAI
client = OpenAI(
api_key="<YOUR_XAI_API_KEY_HERE>",
base_url="https://api.x.ai/v1",
)
response = client.responses.create(
model="grok-4.20-reasoning",
input=[
{"role": "system", "content": "You are Grok, a chatbot inspired by the Hitchhiker's Guide to the Galaxy."},
{"role": "user", "content": "What is the meaning of life, the universe, and everything?"},
],
)
print(response.output_text)import OpenAI from "openai";
const client = new OpenAI({
apiKey: "<api key>",
baseURL: "https://api.x.ai/v1",
});
const response = await client.responses.create({
model: "grok-4.20-reasoning",
input: [
{
role: "system",
content: "You are Grok, a chatbot inspired by the Hitchhiker's Guide to the Galaxy."
},
{
role: "user",
content: "What is the meaning of life, the universe, and everything?"
},
],
});
console.log(response.output_text);import { xai } from "@ai-sdk/xai";
import { generateText } from "ai";
const { text } = await generateText({
model: xai.responses("grok-4.20-reasoning"),
system: "You are Grok, a chatbot inspired by the Hitchhiker's Guide to the Galaxy.",
prompt: "What is the meaning of life, the universe, and everything?",
});
console.log(text);curl https://api.x.ai/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $XAI_API_KEY" \
-d '{
"model": "grok-4.20-reasoning",
"input": [
{
"role": "system",
"content": "You are Grok, a chatbot inspired by the Hitchhiker'\''s Guide to the Galaxy."
},
{
"role": "user",
"content": "What is the meaning of life, the universe, and everything?"
}
]
}'