Grok Voice Agent API Example
This example demonstrates how to integrate xAI's Grok Voice Agents API with LiveKit Agents. It includes:
- Basic setup with xAI's voice-to-voice model
- Integration with built-in search tools so Grok can search x.com and the web
Quickstart
1. Set up environment variables
Note
All keys prefixed with
LIVEKIT_can be obtained from a project created on LiveKit Cloud or from a self-hosted LiveKit server instance.
export LIVEKIT_API_KEY=<your-livekit-api-key>
export LIVEKIT_API_SECRET=<your-livekit-api-secret>
export LIVEKIT_URL=<your-livekit-url>
export XAI_API_KEY=<your-xai-api-key>
2. Install dependencies
uv add "livekit-agents[xai,silero]" livekit-plugins-noise-cancellation
3. Run the agent
Option 1: Console mode
You can talk to Grok directly in your terminal:
uv run grok_voice_agent_api.py console
Option 2: Agents playground
LiveKit hosts a playground environment where you can connect to the agent running on your machine. First run the agent:
uv run grok_voice_agent_api.py dev
Then:
- Open the playground environment in your browser: https://agents-playground.livekit.io/
- Select the LiveKit Cloud project that's linked to your agent and click "Connect to "
Option 3: Run your own custom frontend
LiveKit offers a bunch of agent frontend starter templates across languages. Like Option 2, the first step is to run the agent on your machine:
uv run grok_voice_agent_api.py dev
Then:
Note
Whichever client platform you choose, to ensure your agent frontend and backend can connect to one another, use the same values for
LIVEKIT_environment variables with both your frontend and agent.
Clone and run agent-starter-react to interact with your agent in a browser
Or alternatively:
4. Try it out
Ask Grok something that requires real time information:
"What is Elon Musk's most recent X post?"