Implementation with the xAI SDK
On this page
The native xAI SDK
The xAI SDK is the most direct way to use multi-agent mode, and the only one that exposes parameters like agent_count that third-party SDKs don’t. If you’re starting without ecosystem constraints, start here: you’ll have access to the full set of settings.
Basic setup
Initializing the client comes down to three imports and reading your API key from the environment:
import os
from xai_sdk import Client
from xai_sdk.chat import user
client = Client(api_key=os.getenv("XAI_API_KEY"))
The XAI_API_KEY environment variable must contain your xAI API key. Never hard-code the key: it would end up in the Git repository, then in its history, where it’s hard to remove.
4-agent request (fast mode)
For a quick, focused search, you pass agent_count=4 when you create the chat:
chat = client.chat.create(
model="grok-4.20-multi-agent-0309",
agent_count=4
)
chat.append(user("Compare TCP and UDP in detail"))
for response, chunk in chat.stream():
if chunk.content:
print(chunk.content, end="", flush=True)
Note that you consume the response through chat.stream(). That’s the recommended method: without it, your program hangs with no sign of life for the entire search, and nothing tells you whether the request is still running or has crashed.
16-agent request (in-depth mode)
Switching to in-depth mode only requires changing one value, but the request you pair with it has to match the scale of the team you’re deploying:
chat = client.chat.create(
model="grok-4.20-multi-agent-0309",
agent_count=16
)
chat.append(user(
"Give me a complete state of the art on RAG architectures in 2026, "
"comparing hybrid approaches, performance, and costs"
))
for response, chunk in chat.stream():
if chunk.content:
print(chunk.content, end="", flush=True)
Adding built-in tools
You add tools with a simple list, and the SDK provides the matching constructors:
from xai_sdk.tools import web_search, x_search
chat = client.chat.create(
model="grok-4.20-multi-agent-0309",
agent_count=4,
tools=[web_search(), x_search()],
include=["verbose_streaming"],
)
chat.append(user("What are xAI's latest announcements this week?"))
for response, chunk in chat.stream():
if chunk.content:
print(chunk.content, end="", flush=True)
The include=["verbose_streaming"] parameter changes what you receive: tool calls appear in real time while the agents work, and you know what the current search is focused on.
Encrypted content for multi-turn conversations
If you plan to ask several questions in a row on the same topic, turn on encrypted content when you create the chat:
chat = client.chat.create(
model="grok-4.20-multi-agent-0309",
agent_count=4,
use_encrypted_content=True,
)
The model then keeps the sub-agents’ state between turns, which makes extended exchanges much more coherent.
Error handling
Because the API is in beta, a request can fail through no fault of your own. Wrap the call and plan an escape route:
try:
chat = client.chat.create(
model="grok-4.20-multi-agent-0309",
agent_count=4
)
chat.append(user("My question"))
for response, chunk in chat.stream():
if chunk.content:
print(chunk.content, end="", flush=True)
except Exception as e:
print(f"Multi-agent error: {e}")
# Fall back to a standard model if needed
The comment on the last line is waiting to be replaced with real code. In production, this block has to actually fall back to a standard model, otherwise your feature stops working the first time multi-agent is unavailable.
Key takeaways
- The xAI SDK exposes
agent_countto control the number of agents directly - Always use streaming for multi-agent (
.stream()) verbose_streamingreveals tool calls in real timeuse_encrypted_content=Truefor multi-turn conversations- Always plan for error handling (beta API)