LiveKit Agents
xAI with LiveKit
xAI in a LiveKit agent: through our plugin, or through LiveKit's xAI plugin pointed at the router.
With our plugin#
xAI’s text to speech with the settings of LiveKit’s xAI plugin. Each sentence of your agent’s reply is synthesized on its own and cached.
pip install livekit-plugins-tts-cache-routerfrom livekit.plugins import tts_cache_router as tcr# The API key comes from TTS_CACHE_ROUTER_API_KEY.tts = tcr.TTS( provider=tcr.XAI( voice="ara", language="auto", ),)Pass tts to AgentSession(tts=tts). Set TTS_CACHE_ROUTER_API_KEY in the agent’s environment, and store your xAI key under Provider keys. The constructor of TTS itself is on the plugin’s page.
tcr.XAI(...)#
voicestrDefault"ara"- An xAI voice, e.g.
araoreve, or a custom voice id. languagestrDefault"auto"- A language code such as
en,hiorpt-BR, orautoto let xAI detect it. speedfloat | NoneDefaultNone- Speaking rate, 0.7–1.5.
Noneleaves it to xAI (1.0). optimize_streaming_latencyint | NoneDefaultNone0for the best quality,1or2for a smaller first chunk and lower latency, with a quality tradeoff at chunk boundaries.Noneleaves it to xAI (0).text_normalizationbool | NoneDefaultNone- Speak numbers, abbreviations and symbols in their spoken forms.
Noneleaves it to xAI (off).
Every value the router accepts, and the languages per model, are in Models and options.
Changing settings#
Replies that start afterwards use the new settings:
tts.update_options(voice="eve", speed=1.1)With LiveKit's xAI plugin#
Keep livekit-plugins-xai and point it at the router: your router API key in place of your xAI key, and the router’s address. Nothing else changes.
pip install livekit-plugins-xaiimport osfrom livekit.agents import tokenizefrom livekit.plugins import xaifrom livekit.plugins.xai import tts as xai_ttsxai_tts.XAI_WEBSOCKET_URL = "wss://api.ttscacherouter.dev/xai/v1/tts"tts = xai.TTS( voice="ara", language="auto", tokenizer=tokenize.basic.WordTokenizer(ignore_punctuation=False, retain_format=True), api_key=os.environ["TTS_CACHE_ROUTER_API_KEY"],)LiveKit’s xAI plugin has no base_url: set its module’s XAI_WEBSOCKET_URL to the router before creating the TTS, as above.
Pass the tokenizer shown, with or without the router. The plugin’s default word tokenizer sends your agent’s words without the spaces between them, and xAI joins them as sent (Hi then there. is spoken as Hithere.). With the default tokenizer the router also can’t tell where sentences end, so each reply is synthesized and cached whole.
The router splits the reply into sentences and caches each one, with a few differences from xAI’s own messages.
Requests, responses and errors keep xAI’s own shape. The endpoints behind it are described in xAI API drop-in.
Which to choose#
Both use your org's cache and your stored provider key, and sound the same.
- Our plugin reports only the characters xAI billed in LiveKit’s
TTSMetrics(cache hits count zero), and lets you switch provider withupdate_options. - xAI’s plugin is the smallest change to an agent that already uses it. Its metrics count every character, cached or not; the dashboard’s Usage page has the real split.