Providers
Deepgram
Deepgram's Aura and Flux through the cache: in a LiveKit agent with our plugin or LiveKit's Deepgram plugin, or with Deepgram's own speak APIs and the Deepgram SDK, changing only the base URL and key.
With our LiveKit plugin#
Deepgram’s Aura and Flux with every setting of LiveKit’s Deepgram plugin (TTS and TTSv2). Deepgram’s model id is the model and the voice joined: model="deepgram/aura-2" and voice="thalia-en" is aura-2-thalia-en. With Aura, the first sentence of a reply after a pause of about five seconds or more takes about 0.7–1.2 s longer to start than with LiveKit’s plugin; sentences from the cache, and Flux, are unaffected.
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( model="deepgram/aura-2", voice="thalia-en",)Pass tts to AgentSession(tts=tts). Set TTS_CACHE_ROUTER_API_KEY in the agent’s environment, and store your Deepgram key under Provider keys. TTS’s own arguments are on the plugin’s page.
Options#
tcr.TTS(...)’s settings for Deepgram, in the router’s names: model, voice, language and speed as arguments, and the rest in the options dict (options={"name": value}). The router checks them; the languages per model are in Models and options.
modelstringRequiredOne of deepgram/aura-2, deepgram/aura-1, deepgram/flux
Which model synthesizes the replies, with its provider.voicestringRequired- A voice of the chosen model, passed to Deepgram as given. See models and voices.
speednumberOptionalRange 0.7 – 1.5
Speech speed, aura-2 only. 1.0 is normal speed; left out, the provider's default.sample_rateintDefault24000One of 8000, 16000, 22050, 24000, 32000, 44100, 48000
Of the audio you receive, in Hz.options.mip_opt_outbooleanDefaultfalse- Opt out of Deepgram's Model Improvement Program.
Changing settings#
Replies that start afterwards use the new settings:
tts.update_options(voice="andromeda-en")With LiveKit's Deepgram plugin#
Keep livekit-plugins-deepgram and point it at the router: your router API key in place of your Deepgram key, and the router’s address.
pip install livekit-plugins-deepgramimport osfrom livekit.plugins import deepgramtts = deepgram.TTS( model="aura-2-thalia-en", api_key=os.environ["TTS_CACHE_ROUTER_API_KEY"], base_url="https://api.media.ttscacherouter.dev/deepgram/v1/speak",)For Flux, use deepgram.TTSv2(model="flux-alexis-en", …) with base_url ending in /deepgram/v2/speak. The plugin streams your agent’s reply word by word; the router splits it into sentences and caches each one, with a few differences from Deepgram’s own messages.
Requests, responses and errors keep Deepgram’s own shape; the endpoints are below.
Which plugin to choose#
Both use your org's cache and your stored provider key, and sound the same.
- Our plugin reports only the characters Deepgram billed in
TTSMetrics(cache hits count zero), and switches provider withupdate_options. - Deepgram’s plugin is the smallest change to an agent that already uses it. Its metrics count every character, cached or not; Usage has the real split.
Deepgram's Python SDK#
Deepgram’s speak APIs, mirrored under https://api.media.ttscacherouter.dev/deepgram: /v1/speak (Aura) and /v2/speak (Flux), REST and WebSocket. Keep the Deepgram SDK: give it the router as its environment and your router API key as its api_key.
import asyncioimport osfrom deepgram import AsyncDeepgramClientfrom deepgram.environment import DeepgramClientEnvironmentasync def main(): client = AsyncDeepgramClient( api_key=os.environ["TTS_CACHE_ROUTER_API_KEY"], environment=DeepgramClientEnvironment( base="https://api.media.ttscacherouter.dev/deepgram", production="wss://api.media.ttscacherouter.dev/deepgram", agent="wss://agent.deepgram.com", agent_rest="https://agent.deepgram.com", ), ) with open("speech.mp3", "wb") as f: async for chunk in client.speak.v1.audio.generate( text="Your order has shipped. It arrives on Friday.", model="aura-2-thalia-en", ): f.write(chunk)asyncio.run(main())speak.v1 and speak.v2, REST (audio.generate) and WebSocket (connect), work through the router with every model, voice, encoding and setting. The router splits your text into sentences and sends each to Deepgram with your settings and the Deepgram key stored under Provider keys (see What is cached); your key goes in Authorization: Token, as Deepgram’s does. For a LiveKit agent, see LiveKit’s Deepgram plugin above.
REST#
POST /deepgram/v1/speak: Aura. The audio in the encoding and container the query asks for, with Deepgram’s ownContent-Type, streamed sentence by sentence as each is ready.POST /deepgram/v2/speak: Flux, batch. The same, with Flux’s parameters.GET /deepgram/v1/modelsandGET /deepgram/v2/models: Deepgram’s models, Aura’s voices onv1and Flux’s onv2. Never cached. GET /v1/voices lists them in the router’s own shape.
Any other path under /deepgram answers 404.
curl -N "https://api.media.ttscacherouter.dev/deepgram/v1/speak?model=aura-2-thalia-en&encoding=linear16&container=none&sample_rate=24000" \ -H "Authorization: Token $TTS_CACHE_ROUTER_API_KEY" \ -H "Content-Type: application/json" \ -d '{"text": "Your order has shipped."}' \ --output speech.pcmcurl -N "https://api.media.ttscacherouter.dev/deepgram/v2/speak?model=flux-alexis-en" \ -H "Authorization: Token $TTS_CACHE_ROUTER_API_KEY" \ -H "Content-Type: application/json" \ -d '{"text": "Your order has shipped."}' \ --output speech.mp3WebSocket#
wss://api.media.ttscacherouter.dev/deepgram/v1/speak: Aura’s streaming API. SendSpeaktext andFlush; you get the audio as binary messages,Flushedafter each flush’s audio,ClearedafterClear.wss://api.media.ttscacherouter.dev/deepgram/v2/speak: Flux. Each turn (the text up to aFlush) getsSpeechStarted, its audio,FlushedandSpeechMetadata;Interrupt,ConfigureandClosework as at Deepgram.
The router splits your text into sentences and serves each sentence from the cache or synthesizes it at Deepgram, so the messages are Deepgram’s with these differences:
- Audio for a sentence starts once the sentence is complete. Deepgram’s Aura starts at a comma.
- Aura’s opening
Metadataand Flux’sConnectedcarry the router’s own request and model ids, andmodel_versionisunknown: no Deepgram request exists yet when you connect. A Flux turn’sspeech_idis the router’s own too. - A Flux turn’s
SpeechMetadatacovers the whole turn.billable_character_countcounts only the sentences Deepgram synthesized: sentences served from the cache cost nothing at Deepgram. SpeechInterruptedsplits the turn’s text intotext_spokenandtext_remainingat sentence boundaries: a sentence whose audio had started playing atplayback_offsetcounts as spoken.Configureapplies to the sentences after it.- Aura’s limit of 20 flushes a minute doesn’t apply.
- A failure at Deepgram (a wrong voice, your Deepgram quota) arrives as an
Errormessage once a sentence needs Deepgram, then the router closes the connection (code 1011). A connection idle for 30 minutes is closed.
What is cached#
- REST: each sentence. The router splits
textat sentence ends and at the opening sentence’s first comma, and sends each piece to Deepgram as a request of its own with your query. A sentence your org has synthesized before with the same settings comes from the cache, whatever text surrounds it. A piece with nothing to speak is skipped. - The sentences reach you as one response, streamed as Deepgram streams it: raw samples or MP3 frames one after another, and with a WAV container one header, at the start.
- A parameter you leave out counts as Deepgram’s default for it:
aura-asteria-enon Aura,mp3at abit_rateof 48000, awavcontainer forlinear16,mulawandalaw, Aura’ssample_rate(24000 forlinear16, 8000 formulawandalaw),mip_opt_out=falseand Flux’sexpressivity0. LiveKit’s Deepgram plugin, /v1/stream and /v1/audio/speech ask Aura for raw audio (container=none) or MP3: an Aura request here with the same model, encoding and settings shares their cache. A WAV container and Flux’s batch API keep entries of their own (the router’s other ways in use Flux’s WebSocket). - Prefer
mp3,linear16,mulaworalawoveropus,flacand Flux’saac. Those take a file of their own per request, so their sentences don’t join into one: the whole text goes to Deepgram as one request and is cached as one entry. Only the same text with the same settings replays it, so a sentence your agent repeats inside other replies is synthesized and paid for every time. Text with Deepgram’s inline pronunciation or pause controls (\{…\}), which a split could cut through, is cached whole in the same way. - Only a complete take is cached: a 200 in the format you asked for whose stream Deepgram ended cleanly. Errors are never cached.
- If your client disconnects mid-request, the sentence Deepgram is synthesizing finishes and is cached; the sentences after it aren’t sent.
- A request the router doesn’t read as one Deepgram accepts (a query parameter Deepgram’s reference doesn’t list, which Aura ignores and Flux refuses; one given twice; a
callback; a body with fields besidestext; text with nothing to speak or over 2,000 characters; Aura’saac, which Deepgram answers with no audio) goes to Deepgram as you sent it, isn’t cached, and gets Deepgram’s own answer with itsdg-*headers. - A response the router assembled carries only
Content-Type: no one Deepgram request produced it, so there are nodg-request-id,dg-char-countor otherdg-*headers. - WebSocket: each sentence, so a sentence you have sent before with the same settings comes from the cache whatever came around it. Aura’s sentences share the cache with the router’s own /v1/stream and REST requests with the same settings.
- Aura on the WebSocket: the first sentence of a reply after a pause of about five seconds or more takes about 0.7–1.2 s longer to start than on Deepgram’s own WebSocket. Sentences from the cache, and Flux, are unaffected.
- Audio is kept for your org’s retention, counted from its last use. At retention
0, nothing is cached and every sentence goes to Deepgram.
Limits and timeouts#
- A REST request body is at most 80,000 bytes; Deepgram takes at most 2,000 characters of text.
- Each sentence’s first byte within 10 seconds, then 10 seconds at most between chunks. A request sent to Deepgram as is gets the same.
- Each REST request uses one connection to Deepgram from your org’s pool and synthesizes one sentence at a time, so Deepgram’s own limits on your key apply as when you call it directly. A sentence from the cache makes no request to Deepgram.
Errors#
Errors arrive as Deepgram sends them, so your client's error handling keeps working.
- A wrong or missing router key gets Deepgram’s own refusal, HTTP 401, on REST and at the WebSocket handshake. So does an org with no Deepgram key stored, with a message that links to the dashboard.
- Errors from Deepgram, such as a model your Deepgram project can’t use, a format setting Deepgram refuses or a rate limit (429), reach you with Deepgram’s status, body and
dg-errorheader. - The router’s own come in Deepgram’s shape (
err_code,err_msg): 402insufficient_creditswhen your org is out of credit, 400 for a body over 80,000 bytes, 502provider_unavailablewhen Deepgram can’t be reached and 504provider_timeoutwhen it sends nothing in time. - A REST response begins with the first sentence’s audio, so a later sentence that fails breaks the response off before its end, as a dropped connection to Deepgram would.
HTTP/1.1 401 Unauthorized{"err_code": "INVALID_AUTH", "err_msg": "Invalid credentials.", "request_id": null}