Skip to main content

Overview

GrokRealtimeLLMService provides real-time, multimodal conversation capabilities using xAI’s Grok Voice Agent API. It supports speech-to-speech interactions with integrated LLM processing, function calling, and advanced conversation management with low-latency response times.

Grok Realtime API Reference

Pipecat’s API methods for Grok Realtime integration

Example Implementation

Complete Grok Realtime conversation example

Async Tool Example

Grok with async function calling (cancel_on_interruption=False)

Grok Voice Documentation

Official xAI Grok Voice Agent API documentation

xAI Console

Access Grok models and manage API keys

Installation

To use Grok Realtime services, install the required dependencies:

Prerequisites

xAI Account Setup

Before using Grok Realtime services, you need:
  1. xAI Account: Sign up at xAI Console
  2. API Key: Generate a Grok API key from your account dashboard
  3. Model Access: Ensure access to Grok Voice Agent models
  4. Usage Limits: Configure appropriate usage limits and billing

Required Environment Variables

  • XAI_API_KEY: Your xAI API key for authentication

Key Features

  • Real-time Speech-to-Speech: Direct audio processing with low latency
  • Multilingual Support: Support for multiple languages
  • Voice Activity Detection: Server-side VAD for automatic speech detection
  • Function Calling: Seamless support for external functions and tool integration
  • Multiple Voice Options: Various voice personalities available
  • WebSocket Support: Real-time bidirectional audio streaming

Configuration

GrokRealtimeLLMService

str
required
xAI API key for authentication.
str
default:"wss://api.x.ai/v1/realtime"
WebSocket base URL for the Grok Realtime API. Override for custom deployments.
SessionProperties
default:"None"
deprecated
Deprecated since 0.0.105, removed in 2.0.0. Use settings=GrokRealtimeLLMService.Settings(session_properties=...) instead.
GrokRealtimeLLMService.Settings
default:"None"
Runtime-configurable settings. See Settings below.
bool
default:"False"
Whether to start with audio input paused.

Settings

Runtime-configurable settings passed via the settings constructor argument using GrokRealtimeLLMService.Settings(...). These can be updated mid-conversation with LLMUpdateSettingsFrame. See Service Settings for details.
NOT_GIVEN and None values are omitted, letting the service use its own defaults. Only parameters that are explicitly set are included.

SessionProperties

TurnDetection

The turn_detection field in SessionProperties configures voice activity detection and turn management:

AudioConfiguration

The audio field in SessionProperties accepts an AudioConfiguration with input and output sub-configurations: AudioInput (audio.input): AudioOutput (audio.output): Grok PCM audio supports sample rates: 8000, 11025, 16000, 22050, 24000, 32000, 44100, and 48000 Hz. InputAudioTranscription (audio.input.transcription):

Built-in Tools

Grok provides several built-in tools in addition to custom function tools:

Usage

Basic Setup

With Session Configuration

With Built-in Tools

Updating Settings at Runtime

A session_properties delta replaces the stored SessionProperties whole, so fields it leaves unset (such as tools, turn detection, and transcription) fall back to their defaults. Copy the full session config and change the fields you need:

Methods

delete_conversation_item

Delete a conversation item by ID. Sends a conversation.item.delete event to the server. Parameters:
  • item_id (str): ID of the conversation item to delete

force_message

Speak a hard-coded TTS line without involving the LLM. Sends a force_message conversation item. The server automatically injects the response lifecycle, so no response.create call is needed. Parameters:
  • text (str): Verbatim text to synthesize and play

set_audio_input_paused()

Stops (True) or resumes (False) sending input audio to the model.

Notes

  • Model versioning: The default model "grok-voice-latest" tracks xAI’s recommended Voice Agent. For stability, pin a specific version in settings (e.g., model="grok-voice-think-fast-1.0").
  • Audio format auto-configuration: If audio.input or audio.output is not set in session_properties, the service configures PCM for it using the pipeline’s sample rates, which must be one of the supported PCM rates. When you set audio.input or audio.output (for example, for transcription or speed), set its format too. Input audio is sent without resampling, so a configured input rate must match the pipeline’s audio_in_sample_rate.
  • Server-side VAD: Enabled by default. When VAD is enabled, the server handles speech detection and turn management automatically. Set turn_detection to None for manual turn detection.
  • Audio flow timing: User audio flows once the session is configured, without waiting for a first context, so the user can speak first without an LLMRunFrame.
  • Interruption handling: Interruptions send response.cancel to stop in-flight assistant audio on the wire and conversation.item.truncate to align server-side history with what the user heard. With server VAD enabled, the input buffer is preserved (not cleared) so interrupting user speech remains intact. Manual turn mode clears the input buffer on interruption.
  • Voice IDs: Accepts any built-in voice ID from xAI’s catalogue or a custom voice ID from the Custom Voices API. Voice IDs are case-insensitive. Default is "eve".
  • G.711 formats: PCMU and PCMA are fixed at 8000 Hz. The service passes audio through without converting it, and pipeline audio frames are 16-bit PCM, so use PCMAudioFormat; for telephony, the transport’s serializer converts to and from G.711.
  • System instruction precedence: The system_instruction from service settings takes precedence over an initial system message in the LLM context. A warning is logged when both are set.
  • Async tool support: Functions registered with cancel_on_interruption=False are supported. The final tool result is delivered via the formal tool-result channel once the async function completes. Intermediate results (is_final=False) reach the model as conversation messages.

Event Handlers