# Vaakini

> Vaakini through the cache: in a LiveKit agent with our plugin or Vaakini's LiveKit plugin, in Pipecat, or with Vaakini's own HTTP and WebSocket APIs and the Vaakini SDK, changing only the base URL and key.

> **Write each word in its own script**
>
> Vaakini pronounces a word most reliably when it is written in the script of its language: a Hindi word in Devanagari, a Tamil word in Tamil script, an English word in Latin letters. The same Hindi word spelled in Latin letters (`dhanyavaad` for `धन्यवाद`) is more likely to be mispronounced.
>
> If an LLM writes your agent’s replies, say so in its system prompt, for example: “Write every word in the script of its own language: Hindi words in Devanagari, English words in English letters. Never write a Hindi word in English letters.”

## With our LiveKit plugin

Vaakini’s `vaakini-tts-1`: one model for 12 Indian languages, in Vaakini’s voices or your Vaakini org’s custom voices. Language codes are bare (`hi`, not `hi-IN`); leave `language` out and Vaakini picks it from the script of the text.

```shell
pip install livekit-plugins-tts-cache-router
```

**agent.py**

```python
from livekit.plugins import tts_cache_router as tcr

# The API key comes from TTS_CACHE_ROUTER_API_KEY.
tts = tcr.TTS(
    model="vaakini/vaakini-tts-1",
    voice="Monika",
    language="hi",
)
```

Pass `tts` to `AgentSession(tts=tts)`. Set `TTS_CACHE_ROUTER_API_KEY` in the agent’s environment, and store your Vaakini key under [Provider keys](https://app.ttscacherouter.dev/app/provider-keys). `TTS`’s own arguments are on [the plugin’s page](https://docs.ttscacherouter.dev/livekit.md#tts).

### Options

`tcr.TTS(...)`’s settings for Vaakini, 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](https://docs.ttscacherouter.dev/reference/models.md#vaakini).

| Field | Type | Required or default | Values | Description |
| --- | --- | --- | --- | --- |
| `model` | string | Required | vaakini/vaakini-tts-1 | Which model synthesizes the replies, with its provider. |
| `voice` | string | Required |  | A voice of the chosen model, passed to Vaakini as given. See [models and voices](https://docs.ttscacherouter.dev/reference/models.md#vaakini-models). |
| `language` | string \| null | Optional |  | A Vaakini language code from [languages](https://docs.ttscacherouter.dev/reference/models.md#vaakini-languages), passed through as given. Leave it out and Vaakini detects the language. |
| `sample_rate` | int | Default `24000` | 24000 | Of the audio you receive, in Hz. |
| `options.normalize` | boolean \| null | Default `null` |  | Speak numbers, currency, dates, times, OTPs, phone numbers, URLs and emails as words in the request's language. Null: Vaakini's default (on). |
| `options.temperature` | number \| null | Default `null` | 0.1 – 1.5 | Sampling temperature: lower is steadier, higher is more varied. Null: Vaakini's default. |
| `options.top_k` | integer \| null | Default `null` | 1 – 2048 | Sample from the k likeliest codes. Null: Vaakini's default. |
| `options.top_p` | number \| null | Default `null` | 0.0 – 1.0 | Nucleus sampling, above 0 and at most 1. Null: Vaakini's default. |
| `options.repetition_penalty` | number \| null | Default `null` | 0.0 – 5.0 | Penalty on recently repeated codes. Null: Vaakini's default. |

### Changing settings

Replies that start afterwards use the new settings:

```python
tts.update_options(voice="Bunty", options={"temperature": 0.6})

# Another language, in the same voice or another.
tts.update_options(language="ta")
```

## With Vaakini's LiveKit plugin

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

```shell
pip install livekit-plugins-vaakini
```

**agent.py**

```python
import os

from livekit.plugins import vaakini

tts = vaakini.TTS(
    voice="Monika",
    language="hi",
    api_key=os.environ["TTS_CACHE_ROUTER_API_KEY"],
    base_url="wss://api.media.ttscacherouter.dev/vaakini",
)
```

The plugin derives its WebSocket address from `base_url`: `wss://api.media.ttscacherouter.dev/vaakini/v1/stream`. It streams 16-bit PCM at 24000 Hz, and takes Vaakini’s options as arguments of their own (`normalize`, `temperature`, `top_k`, `top_p`, `repetition_penalty`).

The plugin streams a reply’s text as the LLM writes it; the router splits it into sentences and caches each one. Its requests are the ones our plugin makes, so the two share cached sentences. Its `TTSMetrics.characters_count` comes from the reply’s `done` message, which through the router counts the router’s pieces, cached or not ([below](https://docs.ttscacherouter.dev/providers/vaakini.md#sentences)).

Requests, responses and errors keep Vaakini’s own shape; the endpoints are [below](https://docs.ttscacherouter.dev/providers/vaakini.md#websocket).

## 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 Vaakini billed in `TTSMetrics` (cache hits count zero), and switches provider with `update_options`.
- **Vaakini’s plugin** is the smallest change to an agent that already uses it. Its metrics count every character, cached or not; [Usage](https://docs.ttscacherouter.dev/dashboard.md#usage) has the real split.

## Voices and languages

`voice` is one of Vaakini’s voices by name, in any case (`Monika`), or the id of a custom voice of your Vaakini org (`cv_…`). Vaakini checks it.

- [GET /v1/voices](https://docs.ttscacherouter.dev/reference/voices.md) with `?provider=vaakini` lists them live from Vaakini with your stored key, never cached: Vaakini’s built-in voices, then the custom voices of the Vaakini org your key belongs to. [Vaakini’s voice catalog](https://docs.vaakini.com/voices) describes them.
- The model is `vaakini-tts-1`, the only one: `vaakini/vaakini-tts-1` for our plugins and `/v1/audio/speech`.
- `language` is optional. Left out, Vaakini picks it from the script of each sentence.
- Vaakini takes no `speed`: it speaks at one pace.

| language | Language |
| --- | --- |
| as | Assamese |
| bn | Bengali |
| en | English |
| gu | Gujarati |
| hi | Hindi |
| kn | Kannada |
| ml | Malayalam |
| mr | Marathi |
| or | Odia |
| pa | Punjabi |
| ta | Tamil |
| te | Telugu |

The options are `normalize` (numbers, currency, dates, times, phone numbers, URLs and emails spoken as words; on by default), `temperature` (0.1 to 1.5), `top_k` (1 to 2048), `top_p` (above 0, at most 1) and `repetition_penalty` (0 to 5). One left out, or null, takes Vaakini’s default. They are listed with the formats in [Models and options](https://docs.ttscacherouter.dev/reference/models.md#vaakini-settings).

| Format | /v1/stream and our plugins | /v1/audio/speech |
| --- | --- | --- |
| pcm | Yes: 16-bit little-endian mono | Yes |
| mp3 | Yes | Yes |
| wav | No | Yes: PCM with a WAV header |

Every format is 24000 Hz, mono, and made by Vaakini itself. There is no `mulaw`, `alaw`, `aac`, `opus` or `flac`.

## With Pipecat

Our Pipecat service takes the same settings as our LiveKit plugin. Vaakini's own Pipecat service works too, pointed at the router.

```shell
pip install pipecat-tts-cache-router
```

**bot.py**

```python
import os

from pipecat_tts_cache_router import TTSCacheRouterTTSService

tts = TTSCacheRouterTTSService(
    api_key=os.environ["TTS_CACHE_ROUTER_API_KEY"],
    settings=TTSCacheRouterTTSService.Settings(
        model="vaakini/vaakini-tts-1",
        voice="Monika",
        language="hi",
    ),
)
```

Put `tts` in your pipeline where a TTS service goes, after the LLM. The rest is on [the service’s page](https://docs.ttscacherouter.dev/pipecat.md).

To keep `pipecat-vaakini`, change its `base_url` and use your router API key in place of your Vaakini key:

```shell
pip install pipecat-vaakini
```

**bot.py**

```python
import os

from pipecat_vaakini import VaakiniTTSService

tts = VaakiniTTSService(
    api_key=os.environ["TTS_CACHE_ROUTER_API_KEY"],
    base_url="wss://api.media.ttscacherouter.dev/vaakini",
    settings=VaakiniTTSService.Settings(voice="Monika", language="hi"),
)
```

## Vaakini's Python SDK

The `vaakini` package, with `base_url` set to `https://api.media.ttscacherouter.dev/vaakini` and your router API key as `api_key`. `AsyncVaakini` takes the same two arguments.

```shell
pip install vaakini
```

**Speech**

```python
import os

from vaakini import Vaakini

client = Vaakini(
    api_key=os.environ["TTS_CACHE_ROUTER_API_KEY"],
    base_url="https://api.media.ttscacherouter.dev/vaakini",
)

audio = client.speech.create(
    input="नमस्ते, मैं सिटी हॉस्पिटल से प्रिया बोल रही हूँ। आपकी अपॉइंटमेंट गुरुवार को है।",
    voice="Monika",
    language="hi",
)
with open("speech.mp3", "wb") as f:
    f.write(audio)

for voice in client.voices.list():
    print(voice.id, voice.category, voice.languages)
```

**Streamed text**

```python
import os
import wave

from vaakini import Vaakini

client = Vaakini(
    api_key=os.environ["TTS_CACHE_ROUTER_API_KEY"],
    base_url="https://api.media.ttscacherouter.dev/vaakini",
)

pcm = bytearray()
with client.stream(voice="Monika", language="hi") as stream:
    # Send text as it arrives, an LLM's tokens for instance; flush ends the reply.
    stream.send_text("नमस्ते, आपका ऑर्डर भेज दिया गया है। ")
    stream.send_text("यह शुक्रवार को पहुँचेगा।")
    stream.flush()

    for event in stream:
        if event.type == "audio":
            pcm += event.data  # 16-bit mono PCM, 24000 Hz
        elif event.type == "sentence":
            print(event.index, event.text)
        elif event.type == "done":
            print(event.sentences, "sentences,", event.characters, "characters")

with wave.open("speech.wav", "wb") as w:
    w.setnchannels(1)
    w.setsampwidth(2)
    w.setframerate(24000)
    w.writeframes(pcm)
```

`client.speech`, `client.stream()`, `client.voices`, `client.models` and `client.custom_voices` all work through the router; the endpoints they call are below.

## WebSocket

Vaakini’s `/v1/stream` WebSocket, mirrored at `wss://api.media.ttscacherouter.dev/vaakini/v1/stream`: Vaakini’s messages, in and out. Send your router API key as `Authorization: Bearer`, where Vaakini takes its key; the router sends your text to Vaakini with the Vaakini key stored under [Provider keys](https://app.ttscacherouter.dev/app/provider-keys).

```json
{"type": "start", "segment_id": "c0ffee", "model": "vaakini-tts-1", "voice": "Monika",
 "language": "hi", "sample_rate": 24000, "format": "pcm", "options": {"temperature": 0.6}}
{"type": "text", "segment_id": "c0ffee", "text": "नमस्ते, आपका ऑर्डर भेज दिया गया है। यह शुक्रवार को पहुँचेगा।"}
{"type": "flush", "segment_id": "c0ffee"}
```

- In: `start` (`segment_id`, `model`, `voice`, `language`, `sample_rate`, `format`, `options`), `text`, `flush`, `cancel` and `warm`. Out: `sentence`, then that sentence’s audio in binary frames, and `done`, `cancelled` or `error` to end the segment. [Vaakini’s reference](https://docs.vaakini.com/websocket) has every field.
- A connection carries segments one after another, one reply each, as at Vaakini. The router splits each segment’s text into sentences as it arrives, plays each one your org has synthesized before from the cache, and sends the rest to Vaakini.
- `format` is `pcm` or `mp3`, at 24000 Hz.
- `cancel` stops the segment at Vaakini, for a barge-in; nothing of the stopped sentence is cached.
- Vaakini’s own clients use this endpoint through the router: its SDK’s `stream()` ([above](https://docs.ttscacherouter.dev/providers/vaakini.md#sdk)), its LiveKit plugin ([above](https://docs.ttscacherouter.dev/providers/vaakini.md#provider-plugin)) and its Pipecat service ([above](https://docs.ttscacherouter.dev/providers/vaakini.md#pipecat)).

### Sentences and counts

The one difference from calling Vaakini directly. The router cuts a reply at the first comma of its first sentence, as it does for every provider, so the first audio starts sooner. The `sentence` messages, and `done`’s `sentences` and `characters`, describe the router’s pieces: a reply whose first sentence has a comma followed by a space is one more `sentence` than at Vaakini, and `characters` leaves out the space at the cut. Vaakini directly:

```json
{"type": "sentence", "index": 0, "text": "नमस्ते, आपका ऑर्डर भेज दिया गया है।"}
{"type": "sentence", "index": 1, "text": "यह शुक्रवार को पहुँचेगा।"}
{"type": "done", "segment_id": "c0ffee", "sentences": 2, "characters": 59}
```

Through the router:

```json
{"type": "sentence", "index": 0, "text": "नमस्ते,"}
{"type": "sentence", "index": 1, "text": "आपका ऑर्डर भेज दिया गया है।"}
{"type": "sentence", "index": 2, "text": "यह शुक्रवार को पहुँचेगा।"}
{"type": "done", "segment_id": "c0ffee", "sentences": 3, "characters": 58}
```

`characters` counts what was spoken, from the cache or by Vaakini. What Vaakini charged and what the cache served are on the dashboard’s [Usage](https://docs.ttscacherouter.dev/dashboard.md#usage) page.

## HTTP, voices and custom voices

Vaakini’s HTTP API, mirrored under `https://api.media.ttscacherouter.dev/vaakini`: Vaakini’s requests, responses and errors, with your router API key as `Authorization: Bearer`.

- `POST /vaakini/v1/audio/speech`: the text you have in full, as `mp3` (Vaakini’s default), `pcm` or `wav`. The router splits `input` into sentences, caches each one, and streams the audio back in order as one file; audio starts as soon as the first sentence is ready.
- `GET /vaakini/v1/voices` and `GET /vaakini/v1/models`: Vaakini’s lists, relayed live with your stored key, never cached. [GET /v1/voices](https://docs.ttscacherouter.dev/reference/voices.md) lists the same voices in the router’s own shape.
- `/vaakini/v1/custom-voices`: your Vaakini org’s custom voices, relayed to Vaakini with your stored key. `GET` lists them, `POST` creates one, `GET`, `PATCH` and `DELETE /vaakini/v1/custom-voices/{voice_id}` read, rename and delete one, and `PUT /vaakini/v1/custom-voices/{voice_id}/recording` replaces its recording. So a team whose Vaakini key lives only with the router can still manage its voices.

Any other path under `/vaakini` answers Vaakini’s 404.

**Speech**

```shell
curl https://api.media.ttscacherouter.dev/vaakini/v1/audio/speech \
  -H "Authorization: Bearer $TTS_CACHE_ROUTER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "vaakini-tts-1", "voice": "Monika", "language": "hi",
       "input": "नमस्ते, मैं प्रिया बोल रही हूँ।", "response_format": "wav"}' \
  --output speech.wav
```

**Voices**

```shell
curl https://api.media.ttscacherouter.dev/vaakini/v1/voices -H "Authorization: Bearer $TTS_CACHE_ROUTER_API_KEY"
```

The router’s own [/v1/audio/speech](https://docs.ttscacherouter.dev/clients/speech.md) takes `vaakini/vaakini-tts-1` as the model, with the options as top-level fields, and serves every provider.

## Splitting

Vaakini is split into sentences, as every provider is.

- The router splits the text at sentence ends (including `।`) and at the opening sentence’s first comma, with the splitter it uses for Sarvam and Raya, on every way in: our plugins, `/v1/stream`, `/v1/audio/speech` and the drop-in.

## What is cached

- Each sentence, sent to Vaakini on its own with your settings. A sentence your org has synthesized before with the same voice, language, options and format comes from the cache, whatever text surrounds it.
- Every way in shares cached sentences when the request is the same: our plugins and [/v1/stream](https://docs.ttscacherouter.dev/clients/stream.md) (Vaakini’s WebSocket), [/v1/audio/speech](https://docs.ttscacherouter.dev/clients/speech.md) (Vaakini’s HTTP endpoint) and the drop-in’s two. PCM and WAV share entries; MP3 has its own.
- An option counts when you send it: a request that leaves `temperature` out and one that sends a value for it are cached apart.
- A voice’s name matches in any case: `monika` and `Monika` share entries. When a voice’s recording is replaced at Vaakini, its sentences are synthesized afresh: the cache never serves the old recording’s audio for the new one.
- Only a complete take is cached: a sentence Vaakini ended with `done`, or an HTTP body that ended cleanly. Errors, cancelled sentences and cut-off bodies are never cached.
- Audio is kept for your org’s [retention](https://docs.ttscacherouter.dev/index.md#retention), counted from its last use. At retention `0`, nothing is cached and every sentence goes to Vaakini.

## Billing

- Vaakini bills the Vaakini org your stored key belongs to for the characters it synthesizes, at Vaakini’s price, as when you call it directly.
- The router charges **$0.002 per 1,000 characters served from the cache**, as for every provider, and nothing for what Vaakini synthesizes. See [Pricing and billing](https://docs.ttscacherouter.dev/pricing.md).
- Get a Vaakini key (`vk_…`) from [Vaakini’s dashboard](https://app.vaakini.com) and store it under [Provider keys](https://app.ttscacherouter.dev/app/provider-keys).

## Errors

On our plugins, `/v1/stream` and `/v1/audio/speech`, Vaakini’s refusals become the router’s [error codes](https://docs.ttscacherouter.dev/reference/errors.md):

| Code | When |
| --- | --- |
| provider_key_missing | Your org has no Vaakini key stored. |
| provider_rejected | Vaakini refused the request, with its message passed through: an unknown voice, a wrong stored key, a Vaakini org out of credit. |
| provider_rate_limited | Vaakini is at capacity. Retry after a moment. |
| provider_unavailable | Vaakini can't be reached or failed. |
| provider_timeout | Vaakini sent no audio in time. |
| bad_request | An unknown language, an option out of range or a `speed`, refused before anything reaches Vaakini. |

On the drop-in, errors keep Vaakini’s shape and statuses, so its SDK raises the errors it raises directly. Over HTTP that is a JSON body; on the WebSocket, an `error` message with the same fields, after which the connection closes.

```json
{"error": {"code": "unauthorized", "message": "…", "retryable": false}}
```

- A wrong or missing router API key is refused as Vaakini refuses a key: HTTP 401 `unauthorized`, at the WebSocket handshake too. An org with no Vaakini key stored gets the same answer.
- An org out of router credit gets HTTP 402 `insufficient_credits`, at the WebSocket handshake too.
- Over HTTP, the status is sent with the first audio, so a sentence that fails after audio has started can only cut the body short. Treat a body that ends early as a failed request.
