Skip to main content

xAI TTS

XaiTTS targets xAI’s one-shot Text-to-Speech endpoint (POST https://api.x.ai/v1/tts). The response body is raw audio bytes in the requested codec, which maps cleanly onto Patter’s streaming synthesis contract and keeps the provider dependency-free (just fetch / aiohttp). The default voice is eve, and the default output is PCM-16-LE @ 16 kHz so chunks drop straight into the Patter pipeline without transcoding. For phone calls the carrier factories emit telephony-native audio — see Telephony.
Beta. The adapter is validated against the xAI TTS API spec; it has not yet been exercised against a live phone call.

Install

Authentication

The REST endpoint authenticates with an Authorization: Bearer <key> header. XAI_API_KEY is read automatically when apiKey / api_key is omitted.

Usage

Use the namespaced import (getpatter/tts/xai) or the flat re-export (XaiTTS). Both auto-resolve XAI_API_KEY from the environment when apiKey is omitted.
Plug it into an agent:

Voices

xAI ships a roster of built-in voices, each with a distinct personality — the full set ships as the XAI_VOICES catalog (ids alone as XAI_VOICE_IDS). eve is the default (XAI_DEFAULT_VOICE); voice IDs are case-insensitive (eve, Eve, and EVE all work) — Patter normalizes case for you before the request goes out. Tone and suggested use cases below are from the xAI Voice Overview; preview samples for each voice in the xAI console playground.

Looking up voices in code

Import the catalog instead of hardcoding voice ids or a count — isXaiBuiltinVoice is case-insensitive and returns false (never throws) for a custom cloned voice id, so it is safe to use as a plain guard:
The same catalog backs the xai() / xaiAsr() config-envelope helpers, for code that builds provider config from data rather than constructing adapters directly:

Custom voices

Clone a voice from a short reference clip (WAV, ≤120 s) with the custom-voices helper. It returns a voice_id usable as the voice on both XaiTTS and the xAI Realtime engine:

Language

language is required by xAI and defaults to "auto" (the model detects the language of the text). Pass a BCP-47 code — e.g. "en", "it", "pt-BR" — for consistent results. Language validation is case-insensitive.

Speech tags

xAI supports inline speech tags embedded in the synthesized text for expressive delivery — for example [pause] and [laugh] for vocal expressions, plus wrapping tags to change delivery style (whispering, singing). Because Patter forwards the reply text verbatim to xAI, any tags your LLM (or system prompt) produces in the text are honored:
A single request accepts up to 15,000 characters. The adapter warns and forwards the text as-is past that limit (the API rejects it); split long text upstream — the pipeline already synthesizes per utterance.

Telephony

The constructor default codec="pcm" @ sampleRate=16000 is correct for web playback and 16 kHz pipelines. For real phone calls use the carrier factories, which negotiate the carrier-native codec so the pipeline skips resampling:
forTwilio() emits G.711 μ-law @ 8 kHz — exactly Twilio’s wire codec, which xAI supports natively — so the audio passes straight through with zero resampling and zero PCM → μ-law encoding.

Output formats

Control the codec and sample rate directly when not using the carrier factories: Supported sampleRate values: 8000, 16000, 22050, 24000 (xAI’s own default), 44100, 48000. bitRate applies to mp3 only (32000192000).

Rates

xAI bills TTS per character. Patter’s default rate (provider key xai_tts) is **0.015/1kcharacters(0.015 / 1k characters** (15.00 / 1M chars). Override per-project via new Patter({ pricing: { xai_tts: { price: ... } } }). See Metrics for the full rate table.

Options

bitRate is a TypeScript-only constructor option; on the Python side pass it via the low-level provider if you need a non-default MP3 bit rate.

Low-level usage

The pipeline-mode wrapper adds XAI_API_KEY resolution on top of the underlying provider. To use it directly, import from getpatter/providers/xai-tts and pass the API key first:

What’s Next

TTS

All TTS providers side by side.

xAI STT

Grok speech-to-text.

xAI Realtime

Grok Voice Agent engine.

Metrics

Cost tracking and rates.