DEV Community

shashank ms
shashank ms

Posted on

Using LLM for Speech Synthesis: A Step-by-Step Guide

Speech synthesis has evolved from monolithic text-to-speech engines to modular pipelines where a large language model controls content and a dedicated neural TTS model renders audio. For developers building voice agents, audiobook tools, or accessibility features, this separation of reasoning and generation provides both flexibility and quality. In this guide, you will build a complete LLM-driven speech synthesis pipeline using Python, the OpenAI SDK, and Oxlo.ai.

Why Decouple the LLM from the TTS Model?

A single model that both reasons about content and generates audio is theoretically convenient, but practically limiting. Large language models excel at structuring narrative flow, injecting context-aware emphasis, and formatting output for downstream consumption. A dedicated TTS model, such as Kokoro 82M, specializes in mel-spectrogram generation and vocoding, producing natural speech with minimal latency. Oxlo.ai hosts both model types behind one OpenAI-compatible API, so you can route text through Llama 3.3 70B or Qwen 3 32B for reasoning, then forward the result to Kokoro 82M for synthesis, without managing separate providers or SDKs.

Prerequisites

You need an Oxlo.ai API key, Python 3.9 or newer, and the openai package installed. Oxlo.ai uses a fully OpenAI-compatible schema, so the standard SDK works as a drop-in replacement once you point the base URL to https://api.oxlo.ai/v1.

pip install openai

Step 1. Generate Structured Speech Content with an LLM

Start by using an LLM to produce the text you want spoken. For agentic or long-form use cases, it helps to request structured output, such as a JSON object containing the script and intended tone. Oxlo.ai supports JSON mode and function calling on its chat models, making this straightforward.

from openai import OpenAI
import json

client = OpenAI(
    api_key="YOUR_OXLO_API_KEY",
    base_url="https://api.oxlo.ai/v1"
)

response = client.chat.completions.create(
    model="llama-3.3-70b",
    messages=[
        {
            "role": "system",
            "content": "You are a script writer. Return JSON with keys: script, tone."
        },
        {
            "role": "user",
            "content": "Write a 30-second welcome message for a developer conference."
        }
    ],
    response_format={"type": "json_object"}
)

payload = json.loads(response.choices[0].message.content)
script = payload["script"]
tone = payload["tone"]

print(f"Tone: {tone}")
print(f"Script: {script}")

Using Llama 3.3 70B for this step gives you a general-purpose reasoning engine that follows instructions reliably. If you are building a multilingual assistant, Qwen 3 32B is a strong alternative available on Oxlo.ai.

Step 2. Synthesize Speech with Kokoro 82M

Once you have the script, send it to the audio/speech endpoint. Oxlo.ai provides Kokoro 82M, a lightweight text-to-speech model that balances quality and speed.

audio_response = client.audio.speech.create(
    model="kokoro-82m",
    voice="af_bella",  # use an available Kokoro voice
    input=script,
    response_format="mp3"
)

audio_response.stream_to_file("welcome.mp3")
print("Audio saved to welcome.mp3")

Because Oxlo.ai exposes this through the same OpenAI-compatible SDK, you do not need a separate client or authentication flow. The audio generation step is a single request, and Oxlo.ai’s flat per-request pricing means the cost is the same whether your script is ten words or ten thousand. For exact plan details, see the Oxlo.ai pricing page.

Step 3. Orchestrate the Full Pipeline

In production, you will chain these calls. The function below generates a script, then synthesizes audio, handling errors at each stage.

def generate_speech(prompt: str, output_path: str = "output.mp3"):
    # 1. Generate script via LLM
    chat = client.chat.completions.create(
        model="llama-3.3-70b",
        messages=[
            {"role": "system", "content": "Return only the spoken text, no markdown."},
            {"role": "user", "content": prompt}
        ]
    )
    text = chat.choices[0].message.content.strip()

    # 2. Synthesize audio
    audio = client.audio.speech.create(
        model="kokoro-82m",
        voice="af_bella",  # use an available Kokoro voice
        input=text,
        response_format="mp3"
    )

    audio.stream_to_file(output_path)
    return output_path

generate_speech("Explain API rate limiting to a junior developer.")

If you are building a real-time voice agent, you can further reduce latency by streaming the LLM response and buffering chunks for the TTS request. Oxlo.ai supports streaming responses on its chat endpoints, and there are no cold starts on popular models, so the pipeline remains responsive under load.

Cost and Latency Considerations

The biggest infrastructure surprise in voice applications is cost scaling with input size. Token-based providers charge for every character or token in your prompt, which makes long-form narration and agentic loops expensive. Oxlo.ai uses request-based pricing: one flat cost per API request regardless of prompt length. For a TTS workload where a single request might contain thousands of words of context or a lengthy generated script, this can reduce costs significantly compared to token-based alternatives.

Latency is also predictable. Because Oxlo.ai keeps popular models warm, you avoid cold-start penalties on both the LLM and TTS stages. You can find current plan details at https://oxlo.ai/pricing.

Putting It into Production

Oxlo.ai gives you a unified platform for the entire voice stack. You can run reasoning on DeepSeek R1 671B for complex scripting logic, generate conversational text with Kimi K2.6, or keep it simple with Llama 3.3 70B, then pass the output to Kokoro 82M for synthesis. Everything is accessible through the same OpenAI-compatible endpoints, so your existing Python or Node.js client code requires only a base URL change.

For developers shipping voice agents, accessibility tools, or automated narration, this integration removes provider fragmentation. The flat per-request pricing model is especially effective for long-context and agentic workloads where input length varies dramatically from call to call. Start with the free tier to validate the pipeline, then scale as needed.

Top comments (0)