LiveKit Agents

The LiveKit plugin

livekit-plugins-tts-cache-router: install it, construct TTS, change settings mid-session, and what it reports.

View as Markdown

Install#

livekit-plugins-tts-cache-router is a LiveKit Agents TTS that speaks through the router, for every provider. It needs Python 3.10 or later and livekit-agents 1.8.5 or later. This page describes version 0.13.0.

pip install livekit-plugins-tts-cache-router
agent.py
from livekit.plugins import tts_cache_router as tcr# The API key comes from TTS_CACHE_ROUTER_API_KEY.tts = tcr.TTS(    model="sarvam/bulbul:v3",    voice="shubh",    language="hi-IN",)

Pass tts to AgentSession(tts=tts). Set TTS_CACHE_ROUTER_API_KEY to your router API key, and store your provider key under Provider keys: the plugin never sends one. It streams, keeps transcripts in step with speech, connects to the provider when the session starts (prewarm()), and keeps its connections warm across turns.

TTS(...)#

The plugin’s one class. Its settings are in the router’s own names, those of /v1/audio/speech and GET /v1/models, the same for every provider.

from livekit.plugins import tts_cache_router as tcrtts = tcr.TTS(    model="elevenlabs/eleven_flash_v2_5",    voice="JBFqnCBsd6RMkjVDRZzb",    language="en",    options={"voice_settings": {"stability": 0.5, "similarity_boost": 0.75}},)
modelstrRequired
Any model GET /v1/models lists, with its provider: maya/maya-2-native, vaakini/vaakini-tts-1, sarvam/bulbul:v3, elevenlabs/eleven_flash_v2_5, openai/tts-1, deepgram/aura-2, xai/xai-tts, … (Models and options).
voicestr | NoneRequired
The provider’s voice id, as GET /v1/voices lists it. None only for Gemini’s two speakers, which options["speakers"] names.
languagestr | NoneDefault None
The provider’s language code, sent as written: hi-IN for Sarvam, hi for Raya, Maya Open-Source and Vaakini, en for the others. Required for Sarvam, Raya and Soniox. Optional for Maya Open-Source, Vaakini, ElevenLabs and xAI: left out, the provider detects it. Optional for Smallest AI and Cartesia: left out, en. Not taken by OpenAI, Deepgram, Gemini or 60db.
speedfloat | NoneDefault None
The speaking rate, where the provider takes one. None: the router's default.
optionsdict | NoneDefault None
The provider’s other settings, by their names in Models and options: {"temperature": 0.6} for Sarvam, {"voice_settings": {...}} for ElevenLabs, {"instructions": "..."} for OpenAI and Gemini, {"wpm": 150} for 60db, {"temperature": 0.8} for Maya Open-Source. Each provider’s page under Providers lists its own.
api_keystr | NoneDefault $TTS_CACHE_ROUTER_API_KEY
Your router API key. Without one, the constructor raises ValueError.
base_urlstrwss://api.media.ttscacherouter.dev
The router: the speech host for every provider unless you set another, such as a router you run yourself.
sample_rateintDefault 24000
Of the PCM audio your agent receives, one the provider streams: 24000 works with every provider; each provider’s list is in Models and options.
http_sessionaiohttp.ClientSession | NoneDefault None
The aiohttp session to connect with. Inside an agent job, leave it out: the job's is used. Outside one, pass your own.

The router is the one that checks the settings. The constructor raises ValueError only for a model that isn’t <provider>/<model> or a missing voice. When the session starts, the plugin connects and sends the settings, and a setting the router refuses is logged as an error then; a reply with it fails with APIStatusError (bad_request, not retryable). tts.provider and tts.model name the underlying provider and model (Sarvam, bulbul:v3) for cost dashboards.

Changing settings#

update_options changes the voice or settings mid-session. Replies that start afterwards use them; the reply being spoken finishes as it began.

Python
# Another voice and speed, for the same provider:tts.update_options(voice="priya", speed=1.1)# Or another provider altogether; its language, speed and options start from none:tts.update_options(model="elevenlabs/eleven_flash_v2_5", voice="JBFqnCBsd6RMkjVDRZzb")

options replaces the options as a whole. A sentence in a new voice or with new settings is a new cache entry: synthesized once, then cached.

Interruptions#

When the user barges in, LiveKit cancels the reply; the plugin tells the router to stop and keeps the connection.

The router stops sending the reply’s audio at once, and the provider stops the sentence it was synthesizing, so nothing cut off is cached (ElevenLabs and Cartesia finish their sentence, and cache it). The connection returns to the plugin’s pool, ready for the next reply.

Transcripts#

The plugin supports LiveKit's aligned transcripts, so captions follow the speech.

Each sentence’s text is timed to the moment its audio starts. With ElevenLabs, transcripts are word by word, from ElevenLabs’ character timings, and with Cartesia from its own (turn them off with options={"sync_alignment": False} or options={"word_timestamps": False}). Word timings are cached with the audio, so a reply from the cache is aligned too.

Metrics#

TTSMetrics, once per reply, count only the characters the provider billed.

Sentences from the cache count zero, so a fully cached reply reports characters_count 0. ttfb, audio_duration and the connection fields are as usual, and the metadata names the underlying provider and model.

Python
from livekit.agents.metrics import TTSMetricsdef on_tts_metrics(metrics: TTSMetrics) -> None:    # What the provider billed for this reply: 0 when it all came from the cache.    print(metrics.characters_count, metrics.ttfb)tts.on("metrics_collected", on_tts_metrics)

Cache stats#

Once a reply is all sent, the plugin emits cache_stats: how much of it came from your org’s cache. This script speaks a sentence twice, outside an agent, and prints both:

speak_twice.py
import asyncioimport aiohttpfrom livekit import rtcfrom livekit.plugins import tts_cache_router as tcrdef on_cache_stats(stats: tcr.CacheStats) -> None:    print(f"{stats.cached_sentences}/{stats.sentences} sentences from the cache, "          f"{stats.billed_characters}/{stats.characters} characters billed by the provider")async def main() -> None:    async with aiohttp.ClientSession() as http:        tts = tcr.TTS(model="openai/tts-1", voice="alloy", http_session=http)        tts.on("cache_stats", on_cache_stats)        for _ in range(2):  # the second time, from the cache            frames = [event.frame async for event in tts.synthesize("Your order has shipped.")]        with open("speech.wav", "wb") as f:            f.write(rtc.combine_audio_frames(frames).to_wav_bytes())        await tts.aclose()asyncio.run(main())
0/1 sentences from the cache, 23/23 characters billed by the provider1/1 sentences from the cache, 0/23 characters billed by the provider
request_idstr
The reply’s request id, as in its TTSMetrics.
segment_idstr | None
The reply’s segment id when streamed, as in its TTSMetrics; None for synthesize().
sentencesint
The sentences the reply was split into.
cached_sentencesint
Those served from the cache.
charactersint
The characters of all the reply's sentences.
billed_charactersint
The characters the provider synthesized and billed, as TTSMetrics.characters_count. cached_characters is the rest.

An interrupted reply emits both too, with TTSMetrics.cancelled set: they count the sentences the router sent in full before the interruption, so characters_count is what the provider billed for the audio your agent received.

Errors and retries#

Router errors become LiveKit's API errors, so LiveKit's retries and FallbackAdapter work as with any TTS.

  • A router error raises APIStatusError with the error’s retryable flag, so LiveKit retries only what can succeed (per your conn_options). Its status_code is the provider’s HTTP status when there is one.
  • A refused router API key raises APIStatusError with status 403, not retryable.
  • A failed connection raises APIConnectionError, and a router that stops answering APITimeoutError; LiveKit retries both on a fresh connection.