OpenAI SDK compatibility
On this page
Using multi-agent through the OpenAI SDK
Many teams have already built their call layer around the OpenAI SDK, with its wrappers, tests, and logging conventions. Good news: accessing Grok’s multi-agent mode doesn’t force you to rewrite everything. xAI exposes an API compatible with OpenAI’s Responses format, so you can keep the OpenAI client and simply point it at the xAI API.
Client configuration
Two settings are all you need: xAI’s base URL and your xAI API key.
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("XAI_API_KEY"),
base_url="https://api.x.ai/v1",
)
From there, you use the Responses API as usual, specifying grok-4.20-multi-agent-0309 as the model. The rest of your call code stays unchanged.
Controlling agent count with reasoning.effort
This is where the translation between the two worlds plays out. Where the xAI SDK exposes an explicit agent_count, the OpenAI SDK offers only reasoning.effort—so you don’t request a number of agents; you request an effort level that xAI converts into a number of agents.
# 4 agents (low effort)
response = client.responses.create(
model="grok-4.20-multi-agent-0309",
reasoning={"effort": "low"},
input=[{"role": "user", "content": "What are the latest open source models from 2026?"}],
)
print(response.output_text)
# 16 agents (high effort)
response = client.responses.create(
model="grok-4.20-multi-agent-0309",
reasoning={"effort": "high"},
input=[{"role": "user", "content": "Compare the EU and US AI strategies in 2026"}],
)
print(response.output_text)
The effort → agents mapping
The field accepts four values, but they produce only two team sizes: "low" and "medium" use 4 agents, while "high" and "xhigh" use 16.
This detail deserves careful attention. Moving from low to medium, or from high to xhigh, does not change the number of agents: these intermediate levels may influence other aspects of model behavior, but within each pair the team stays the same. Imagine an interface where you offered the user four levels of research depth—they would perceive two, and your two extra buttons would only confuse their choice.
Chat Completions is not supported
Here is the constraint that trips up most developers on their first attempt: Chat Completions is not supported for multi-agent. You must use the Responses API, meaning client.responses.create, not client.chat.completions.create.
# DOES NOT WORK
response = client.chat.completions.create(
model="grok-4.20-multi-agent-0309",
messages=[{"role": "user", "content": "..."}],
)
This call returns an error. If your existing code relies entirely on Chat Completions—as many existing codebases do—the change is not just a model name swap: you have to migrate the call to a different input and output format. Size that effort before you promise the integration.
Streaming with the OpenAI SDK
Streaming is still available, again through the Responses API:
stream = client.responses.create(
model="grok-4.20-multi-agent-0309",
reasoning={"effort": "low"},
input=[{"role": "user", "content": "Summarize this week's tech announcements"}],
stream=True,
)
for event in stream:
if hasattr(event, 'delta') and event.delta:
print(event.delta, end="", flush=True)
The hasattr(event, 'delta') test is worth keeping as is: the Responses API event stream contains more than text, and blindly reading event.delta on every event would raise an exception.
Benefits and limits of this approach
The main advantage of this route is that it saves time: you don’t install another SDK, your developers get a familiar syntax, and switching between xAI and OpenAI models in the same codebase becomes trivial. For a team that wants to evaluate the multi-agent mode without committing to a full project, it is the shortest path.
The limits all stem from the same cause: you are going through a compatibility layer, so you lose what is specific to xAI. You don’t get agent_count (replaced by indirect control through reasoning.effort), verbose_streaming, or use_encrypted_content. On top of that, the effort → agents mapping comes from a beta API and could change: if your product depends specifically on the number of agents used, the native SDK remains the prudent choice.
Key takeaways
- The OpenAI SDK works with multi-agent through
base_url="https://api.x.ai/v1" - You control the number of agents through
reasoning.effort(low/medium = 4, high/xhigh = 16) - Chat Completions is NOT supported—use the Responses API only
- Some parameters exclusive to the xAI SDK (
agent_count,verbose_streaming) are not available