Providers

OpenAI

OpenAI through the cache: in a LiveKit agent with our plugin or LiveKit's OpenAI plugin, or with OpenAI's own speech API and the OpenAI SDK, changing only the base URL and key.

View as Markdown

With our LiveKit plugin#

OpenAI, with every setting of OpenAI's own LiveKit plugin. OpenAI speaks the language of the text, at 24000 Hz.

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="openai/gpt-4o-mini-tts",    voice="ash",)

Pass tts to AgentSession(tts=tts). Set TTS_CACHE_ROUTER_API_KEY in the agent’s environment, and store your OpenAI key under Provider keys. TTS’s own arguments are on the plugin’s page.

Options#

tcr.TTS(...)’s settings for OpenAI, 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.

modelstringRequired

One of openai/gpt-4o-mini-tts, openai/gpt-4o-mini-tts-2025-12-15, openai/tts-1, openai/tts-1-hd

Which model synthesizes the replies, with its provider.
voicestringRequired
A voice of the chosen model, passed to OpenAI as given. See models and voices.
speednumberDefault 1.0

Range 0.25 – 4.0

Speech speed. 1.0 is normal speed.
sample_rateintDefault 24000

One of 24000

Of the audio you receive, in Hz.
options.instructionsstring | nullDefault null
How to speak: tone, accent, emotion, pace. gpt-4o-mini-tts follows them; tts-1 and tts-1-hd ignore them.

Changing settings#

Replies that start afterwards use the new settings:

Python
tts.update_options(options={"instructions": "Speak calmly and slowly."})

With LiveKit's OpenAI plugin#

Keep livekit-plugins-openai and point it at the router: your router API key in place of your OpenAI key, and the router’s address.

pip install livekit-plugins-openai
agent.py
import osfrom livekit.plugins import openaitts = openai.TTS(    model="gpt-4o-mini-tts",    voice="ash",    api_key=os.environ["TTS_CACHE_ROUTER_API_KEY"],    base_url="https://api.media.ttscacherouter.dev/openai/v1",)

base_url ends in /v1, as OpenAI’s own does. The plugin doesn’t stream text, so an AgentSession sends it one sentence at a time, and each sentence is cached on its own.

Requests, responses and errors keep OpenAI’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 OpenAI billed in TTSMetrics (cache hits count zero), and switches provider with update_options.
  • OpenAI’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.

OpenAI's Python SDK#

OpenAI’s speech API, mirrored under https://api.media.ttscacherouter.dev/openai/v1. Keep the OpenAI SDK: pass the router as its base_url and your router API key as its api_key.

speak.py
import osfrom openai import OpenAIclient = OpenAI(    base_url="https://api.media.ttscacherouter.dev/openai/v1",    api_key=os.environ["TTS_CACHE_ROUTER_API_KEY"],)with client.audio.speech.with_streaming_response.create(    model="gpt-4o-mini-tts",    voice="ash",    input="Your order has shipped. It arrives on Friday.",    instructions="Speak in a warm, friendly tone.",) as response:    response.stream_to_file("speech.mp3")

audio.speech.create works through the router with every model, voice, format and setting, streamed or not. Requests, responses, status codes and error bodies are OpenAI’s own. The router splits your input into sentences and sends each to OpenAI with your settings and the OpenAI key stored under Provider keys (see What is cached). For a LiveKit agent, see LiveKit’s OpenAI plugin above.

This is OpenAI’s API, with OpenAI’s model names. The router’s own /v1/audio/speech takes the same request with openai/gpt-4o-mini-tts as the model, and serves every provider.

Speech#

  • POST /openai/v1/audio/speech: the audio in the requested response_format, streamed as it is made, or with stream_format: "sse" OpenAI’s speech.audio.delta events, then speech.audio.done and [DONE] (tts-1 and tts-1-hd answer that with the audio, as OpenAI does).

Any other path under /openai answers 404.

curl -N https://api.media.ttscacherouter.dev/openai/v1/audio/speech \  -H "Authorization: Bearer $TTS_CACHE_ROUTER_API_KEY" \  -H "Content-Type: application/json" \  -d '{"model": "tts-1", "voice": "alloy", "input": "Your order has shipped.", "response_format": "pcm"}' \  --output speech.pcm

What is cached#

  • Each sentence: the router splits input at sentence ends and at the opening sentence’s first comma, and sends each piece to OpenAI as a request of its own with your settings. 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, as OpenAI sends it: the audio as one stream of your response_format (one WAV header, at the start), or with SSE each sentence’s speech.audio.delta events, then one speech.audio.done whose usage adds up the tokens OpenAI spent on this request. A sentence from the cache spent none.
  • A field you leave out counts as OpenAI’s default for it (mp3, speed 1.0, the audio itself rather than SSE). LiveKit’s OpenAI plugin, /v1/stream and /v1/audio/speech send OpenAI SSE for gpt-4o-mini-tts and the audio for tts-1 and tts-1-hd: a request here with the same model, voice, format and settings shares their cache. opus and flac (one entry per request here), wav, a custom voice ({"id": …}) and the audio itself from gpt-4o-mini-tts keep entries of their own.
  • Prefer mp3, aac, pcm or wav over opus and flac. opus and flac make a file of their own per request, so their sentences don’t join into one: the whole input goes to OpenAI as one request and is cached as one entry. Only the same input with the same settings replays it, so a sentence your agent repeats inside other replies is synthesized and paid for every time.
  • Only a complete take is cached: one whose stream OpenAI ended cleanly. Errors are never cached.
  • If your client disconnects mid-request, the sentence OpenAI is synthesizing finishes and is cached; the sentences after it aren’t sent.
  • A body the router doesn’t read as a request OpenAI accepts (a field OpenAI’s reference doesn’t list, a value outside its range, SSE from tts-1 or tts-1-hd, input with nothing to speak or over 4,096 characters) goes to OpenAI as you sent it, isn’t cached, and gets OpenAI’s own answer.
  • A response the router assembled carries only Content-Type (and SSE’s Cache-Control): no one OpenAI request produced it, so there is no x-request-id.
  • gpt-4o-mini-tts is an alias OpenAI can move to a newer snapshot; audio already cached under it keeps the voice it was made with. To pin the voice, name a snapshot such as gpt-4o-mini-tts-2025-12-15.
  • Audio is kept for your org’s retention, counted from its last use. At retention 0, nothing is cached and every sentence goes to OpenAI.

Limits and timeouts#

  • A request body is at most 80,000 bytes.
  • Each sentence’s first byte within 30 seconds, then 10 seconds at most between chunks. A body sent to OpenAI as is gets the same.
  • Each request uses one connection to OpenAI from your org’s pool and synthesizes one sentence at a time, so OpenAI’s own limits on your key apply as when you call it directly. A sentence from the cache makes no request to OpenAI.

Errors#

Errors arrive as OpenAI sends them, so your client's error handling keeps working.

  • A wrong or missing router key gets OpenAI’s own refusal, HTTP 401. So does an org with no OpenAI key stored, with a message that links to the dashboard.
  • Errors from OpenAI, such as a voice the model doesn’t have, a wrong stored OpenAI key or a rate limit (429), reach you with OpenAI’s status and body.
  • The router’s own come in OpenAI’s shape: 402 insufficient_credits when your org is out of credit, 400 invalid_request_error for a body over 80,000 bytes, 502 server_error when OpenAI can’t be reached and 504 server_error when it sends nothing in time.
  • The 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 OpenAI would.
HTTP/1.1 401 Unauthorized{"error": {"message": "Incorrect API key provided.", "type": "invalid_request_error",           "param": null, "code": "invalid_api_key"}}