Building a Real-Time Voice Assistant with Claude API
A real-time voice assistant built on Claude needs three pieces working together with minimal delay: speech-to-text (STT), the language model generating a response, and text-to-speech (TTS) speaking it back. The "real-time" part isn't about Claude being instant — it's about architecting the pipeline so the user never feels like they're waiting on a request/response cycle.
The practical answer to "how do I use the Claude API for a real-time voice assistant" is: stream everything. Stream audio into your STT engine, stream partial transcripts into Claude as soon as they're stable, stream Claude's token output into your TTS engine the moment the first sentence is complete, and start playing audio before the full response has even finished generating. Do this correctly and perceived latency drops from several seconds to under one.
The pipeline, piece by piece
A typical real-time voice assistant looks like this:
- Audio capture — microphone input, chunked into small buffers (e.g. 100–250ms).
- STT — a streaming speech recognizer (Whisper streaming, Deepgram, AssemblyAI, or a browser's native Speech Recognition API) converts audio to text incrementally.
- LLM — Claude receives the transcript and generates a response, ideally as a stream of tokens.
- TTS — a streaming text-to-speech engine (ElevenLabs, PlayHT, Amazon Polly streaming) converts Claude's output to audio as it arrives.
- Playback — audio chunks play back-to-back with no gap.
The latency budget matters more than raw model speed. If STT takes 400ms to finalize a transcript, Claude takes 600ms to produce the first sentence, and TTS takes 300ms to synthesize the first chunk, you're at 1.3 seconds before any sound comes out — and that's before network overhead. Every stage needs to overlap with the next, not run sequentially.
Why streaming responses matter here
Waiting for Claude to finish a full response before doing anything with it is the single biggest latency killer in voice assistants. Instead, consume the response as a stream and hand sentence-sized chunks to your TTS engine as soon as they're complete:
const response = await fetch("https://api.subtoapi.app/v1/messages", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.SUBTOAPI_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "claude-sonnet-4",
max_tokens: 400,
stream: true,
messages: [{ role: "user", content: transcript }]
})
});
let buffer = "";
for await (const event of parseSSE(response.body)) {
if (event.type === "content_block_delta") {
buffer += event.delta.text;
if (/[.!?]\s$/.test(buffer)) {
sendToTTS(buffer);
buffer = "";
}
}
}
if (buffer) sendToTTS(buffer);
This pattern — accumulate until a sentence boundary, then flush to TTS — gives you natural-sounding speech without waiting for the entire reply. For details on event types and parsing SSE correctly, see /docs/streaming.
Handling interruptions (barge-in)
Real voice assistants need to handle a user talking over the assistant mid-response. The practical approach:
- Keep the STT stream always listening, even while TTS is playing.
- If new speech is detected above a confidence threshold, immediately stop audio playback and cancel the in-flight Claude request.
- Discard the partial response buffer and start a fresh turn with the new transcript.
Canceling the request matters for cost as much as UX — you don't want to keep paying for tokens the user will never hear. Most HTTP clients support aborting a fetch via AbortController; wire that into your barge-in detection.
Giving the assistant actions, not just words
Voice assistants are far more useful when they can do things — check a calendar, look up an order, control a device — rather than just talk. Claude's tool use lets you define functions the model can call, and the assistant pipeline treats a tool call the same way it treats a sentence: as an event to react to, just without TTS output.
{
"tools": [
{
"name": "get_order_status",
"description": "Look up the status of an order by ID",
"input_schema": {
"type": "object",
"properties": { "order_id": { "type": "string" } },
"required": ["order_id"]
}
}
]
}
When Claude returns a tool_use block instead of plain text, you execute the function, return the result, and let the model continue — speaking the final answer once it has the data. See /docs/tools for the full tool-use request/response shape.
Where SubToAPI fits
If you're prototyping or running a production voice assistant and don't want to manage Claude credentials, rate limits, and billing separately from the rest of your infrastructure, SubToAPI turns your existing Claude access into a standard HTTPS API with a sub_live_ key, SSE streaming support, and per-key usage metadata — useful if your voice assistant is one of several AI features billed to the same customer. Setup is a five-minute signup and copy-paste integration; see /docs/quickstart. Plans start at €9/month on Solo, scaling to Team and Scale tiers with per-seat pricing at /pricing.
Latency checklist before shipping
- First audio byte should play within 1–1.5 seconds of the user finishing speaking.
- Use
max_tokensconservatively — a voice response doesn't need 2000 tokens; cap it around 300–500 for snappy replies. - Keep system prompts short; a 2000-token system prompt adds processing overhead on every turn.
- Test on real network conditions (3G throttling, not just localhost) before assuming your pipeline is fast enough.
- Log time-to-first-token and time-to-first-audio-chunk separately — they fail independently.
FAQ
Can Claude generate audio directly? No. Claude outputs text only. You pair it with a separate STT engine for input and a TTS engine for output, connected via streaming for low latency.
What's the main latency bottleneck in a Claude-based voice assistant? Usually the round trip of waiting for a full response before starting TTS. Streaming tokens into TTS sentence-by-sentence, rather than waiting for completion, is the single biggest latency fix.
Do I need tool use for a basic voice assistant? No — a simple conversational assistant works fine without it. Tool use becomes necessary once the assistant needs to fetch live data or trigger actions rather than just converse.