Build Swarms agents on xAI Grok models — Grok 4 and earlier.
xAI’s Grok models are frontier reasoning models with strong real-time knowledge, long context windows, and good tool-use performance. Grok 4 is the right pick when you need an alternative to GPT-5.4 or Claude Opus with comparable capability.
from swarms import Agentagent = 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."))
from swarms import Agentagent = 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 Agentdef 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 Agentagent = 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.")
Mix Grok with other models — for example, Grok for research and Claude for synthesis:
from swarms import Agent, SequentialWorkflowresearcher = 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."))