Build a Web Search Agent using xAI

Build a web search agent on Grok and run it sync, streaming, and async.

tool_use.py
"""Build a Web Search Agent using xAI."""

import asyncio

from agno.agent import Agent
from agno.models.xai import xAI
from agno.tools.websearch import WebSearchTools

# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------

agent = Agent(
    model=xAI(id="grok-2"),
    tools=[WebSearchTools()],
    markdown=True,
)

# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    # --- Sync ---
    agent.print_response("Whats happening in France?")

    # --- Sync + Streaming ---
    agent.print_response("Whats happening in France?", stream=True)

    # --- Async + Streaming ---
    asyncio.run(agent.aprint_response("Whats happening in France?", stream=True))

Run the Example

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate

Install dependencies

uv pip install -U agno ddgs openai

Export your xAI API key

export XAI_API_KEY="your_xai_api_key_here"

Select the current Grok model

Replace id="grok-2" with id="grok-4.3" in the saved constructor. Grok 4.3 supports function calling and structured output; use an API key with access to this model.

Run the example

Save the code above as tool_use.py, then run:

python tool_use.py

Full source: cookbook/90_models/xai/tool_use.py