Installation
Environment Setup
Quick Start
xAI models use thexai/ prefix:
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Build Swarms agents on xAI Grok models — Grok 4 and earlier.
pip install -U swarms
export XAI_API_KEY="xai-..."
xai/ prefix:
from swarms import Agent
agent = Agent(
agent_name="Grok-Agent",
model_name="xai/grok-4-0709",
max_loops=1,
)
print(agent.run("Summarize the case for first-principles thinking in three paragraphs."))
| Model | model_name | Best for |
|---|---|---|
| Grok 4 | "xai/grok-4-0709" or "grok-4" | Frontier reasoning, default |
| Grok 2 | "xai/grok-2-1212" | Earlier generation, cheaper |
| Grok Beta | "xai/grok-beta" | Legacy |
from swarms import Agent
agent = Agent(
agent_name="Grok-Strategist",
model_name="xai/grok-4-0709",
system_prompt="You are a senior strategy consultant. Reason first-principles and cite trade-offs.",
max_loops=1,
)
print(agent.run(
"Should a B2B SaaS startup with $5M ARR build an in-house data warehouse or use Snowflake?"
))
from swarms import Agent
def get_weather(city: str) -> str:
"""Return the current weather for a city."""
return f"{city}: 21°C, partly cloudy"
agent = Agent(
agent_name="Grok-Assistant",
model_name="xai/grok-4-0709",
tools=[get_weather],
max_loops=3,
)
print(agent.run("What's the weather in Tokyo right now?"))
from swarms import Agent
agent = Agent(
agent_name="Streaming-Grok",
model_name="xai/grok-4-0709",
streaming_on=True,
max_loops=1,
)
agent.run("Walk me through how proof-of-stake differs from proof-of-work.")
from swarms import Agent, SequentialWorkflow
researcher = Agent(
agent_name="Grok-Researcher",
model_name="xai/grok-4-0709",
system_prompt="Gather facts and quotes with citations.",
max_loops=2,
)
writer = Agent(
agent_name="Claude-Writer",
model_name="claude-sonnet-4-6",
system_prompt="Write a clear executive summary from the research.",
max_loops=1,
)
pipeline = SequentialWorkflow(agents=[researcher, writer], max_loops=1)
print(pipeline.run("Impact of AI on the global semiconductor supply chain in 2026."))
from swarms import Agent
agent = Agent(
agent_name="Production-Grok",
model_name="xai/grok-4-0709",
max_loops=1,
persistent_memory=True,
context_compression=True,
context_length=128_000,
autosave=True,
retry_attempts=3,
print_on=False,
)
Was this page helpful?