Sub topic 10
Where the time goes: a latency budget
This chapter assumes you have built the transcription call, the speech call, and the turn-based voice loop from earlier chapters. You should be comfortable reading Python and timing code with time.perf_counter.
Why a budget helps
A voice turn feels slow when the gap between the end of the user's speech and the first sound of the reply is long. People notice pauses of more than about one second. A latency budget splits that gap into stages so you can see which stage to fix first.
In the turn-based loop, the user waits for four things in sequence:
- Upload and transcription of the recording.
- The chat model producing the full reply.
- Speech synthesis of that reply.
- Download and start of playback.
Each stage waits for the one before it. The total is roughly the sum of the stages, so one slow stage dominates.
Measure before you optimize
Do not guess the numbers. Wrap each stage with a timer and log the result. This version of the loop from the previous chapter records the time for each stage.
import time
from openai import OpenAI
client = OpenAI()
def timed_voice_turn(input_path, history, output_path="reply.mp3"):
timings = {}
start = time.perf_counter()
with open(input_path, "rb") as f:
user_text = client.audio.transcriptions.create(model="whisper-1", file=f).text
timings["stt"] = time.perf_counter() - start
history.append({"role": "user", "content": user_text})
start = time.perf_counter()
completion = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "system", "content": "Answer in one or two short spoken sentences."}] + history,
)
reply_text = completion.choices[0].message.content
timings["llm"] = time.perf_counter() - start
history.append({"role": "assistant", "content": reply_text})
start = time.perf_counter()
with client.audio.speech.with_streaming_response.create(
model="tts-1", voice="alloy", input=reply_text
) as response:
response.stream_to_file(output_path)
timings["tts"] = time.perf_counter() - start
timings["total"] = sum(timings.values())
return user_text, reply_text, timingsRun it on a dozen typical recordings and look at the median and the slowest runs, not a single call. Network conditions and server load change from request to request.
Where the time usually goes
Exact numbers depend on the provider, model, region, and your input. The table shows what tends to scale with what.
| Stage | What drives its time | Typical lever |
|---|---|---|
| Transcription | Audio length, upload size, model | Shorter or compressed audio, trim silence |
| Chat model | Prompt length, number of output tokens | Short replies, smaller model, shorter history |
| Speech synthesis | Length of reply text | Shorter replies, streaming, split into sentences |
| Playback start | Time until the first audio bytes arrive | Streaming instead of waiting for a whole file |
Ways to cut latency
- Shorten the reply. The system prompt in the loop asks for one or two short sentences. Fewer output tokens speed up both the chat stage and the synthesis stage.
- Shrink the upload. Send mono audio at 16 kHz in a compressed format. Transcription gains nothing from extra bandwidth, and a smaller file uploads faster.
- Trim history. A long conversation adds input tokens to every chat call. Keep recent turns, or summarize older ones.
- Stream the output. Start playback when the first bytes arrive, instead of waiting for the full file.
- Overlap stages. Stream the chat reply, cut it at sentence boundaries, and send each sentence to synthesis while the model is still writing the next one. The user hears the first sentence early.
Common mistakes
- Averaging only. A mean of 2 seconds can hide occasional 8-second turns. Report a high percentile such as p95.
- Timing from the wrong moment. Start the clock when the user stops speaking, not when your code begins the request. Recording, voice activity detection, and file writing add time too.
- Optimizing the small stage. If the chat stage takes most of the turn, tuning upload size will barely help.
- Ignoring retries. A retry with backoff, as in the errors chapter, adds its wait to the turn. A timeout that is too long makes a failed call look like a slow call.
Exercise
Your timings show stt 0.8 s, llm 2.4 s, tts 1.1 s for a typical turn. Name two changes that would reduce the time before the user hears the first sound, and say which stage each one targets.
Show answer
Streaming the chat reply and sending the first complete sentence to speech synthesis targets the llm and tts stages together, because synthesis starts before the full reply exists. Instructing the model to answer in one or two short sentences also targets the llm stage, which is the largest, and shortens the text sent to tts. Trimming conversation history is another valid answer for the llm stage.