The OpenAI SDK for Grok
On this page
Updated November 3, 2026, based on the xAI Models page: grok-4.7 (September 2026) succeeded grok-4.5 as the recommended model; the example, written with grok-4.5, also works with grok-4.7. Sources: Models · Release notes.
Clarified November 3, 2026, based on the xAI Chat Completions reference: the call isn’t strictly identical to OpenAI’s;
logprobsandtop_logprobs(ignored fromgrok-4.20on) andlogit_bias(not supported) add to the differences listed below. Source: xAI, Chat Completions.
Reuse what you know about OpenAI
If you already work with the OpenAI SDK in Python, you can use it as is to call the Grok API. Just redirect requests to xAI’s servers by changing the base URL. It’s the fastest approach if you have existing code.
Installation
If the OpenAI SDK isn’t installed in your project yet:
pip install openai
Configure the client for xAI
The only difference from regular OpenAI usage is the base_url parameter:
from openai import OpenAI
import os
client = OpenAI(
api_key=os.getenv("XAI_API_KEY"),
base_url="https://api.x.ai/v1"
)
That’s it. From here on, every method in the OpenAI SDK works with the Grok models.
Send a message
The call has the same shape as for GPT, except for unsupported parameters (see below):
response = client.chat.completions.create(
model="grok-4.5",
messages=[
{"role": "system", "content": "You are a technical assistant."},
{"role": "user", "content": "What's the difference between REST and GraphQL?"}
],
temperature=0.7,
max_completion_tokens=500
)
print(response.choices[0].message.content)
Available models
Just replace the OpenAI model name with a Grok model:
grok-4.7: the model xAI recommends, versatile, with image input (grok-4.6andgrok-4.5remain available)grok-4.20-0309-reasoning: advanced reasoninggrok-4.3: fast, inexpensive answers (1M context)
Migrate an existing OpenAI project
To adapt an existing project, you have two options.
Option 1: Environment variables
Change your variables without touching the code:
export OPENAI_API_KEY="xai-your-key"
export OPENAI_BASE_URL="https://api.x.ai/v1"
The OpenAI SDK reads these variables automatically. Your existing code will work if you also change the model name and remove the parameters xAI doesn’t support.
Option 2: Explicit configuration
Write an initialization function that switches between providers:
import os
from openai import OpenAI
def create_client(provider="xai"):
if provider == "xai":
return OpenAI(
api_key=os.getenv("XAI_API_KEY"),
base_url="https://api.x.ai/v1"
)
return OpenAI() # OpenAI by default
client = create_client("xai")
This approach lets you switch easily between providers during the migration.
Differences to know
Although the API is compatible, some behavior differs:
- The
frequency_penaltyandpresence_penaltyparameters are not supported on reasoning models - The
stopparameter isn’t available on reasoning models logprobsandtop_logprobsare silently ignored bygrok-4.20and newer models, andlogit_biasisn’t supported- Model names are obviously different
- Some xAI features (X search, web search) aren’t available through the standard OpenAI format
When to choose this approach
Compatibility mode makes the most sense when your existing code matters more than exclusive features. The most common case is migration: code already written for OpenAI switches to Grok by changing the base URL and the key: two lines, no rewrite. Reversibility matters too: as long as you stay on the OpenAI format, switching providers remains a matter of configuration, which is valuable for comparing models or negotiating. Then there’s the ecosystem: third-party tools built around the OpenAI SDK, such as LangChain and LlamaIndex, work immediately, and a team that already knows the SDK has nothing new to learn. The price to pay is the one you saw above: xAI-specific features take a back seat.
Key takeaways
- Add
base_url="https://api.x.ai/v1"to theOpenAI()constructor - Replace the API key and the model name
- The
OPENAI_BASE_URLandOPENAI_API_KEYvariables allow a migration without touching the code - Some parameters (penalties, stop) aren’t supported on reasoning models