Sub topic 11
Errors, rate limits, retries, and cost
This chapter assumes you can already call the transcription and speech endpoints from Python and have built a basic voice loop. It covers what goes wrong in production and what the calls cost.
Failures you will see
Speech calls fail in two ways. Some failures are temporary, so a retry can succeed. Others are permanent, so a retry will fail the same way.
| Error | Typical cause | Retry? |
|---|---|---|
| `RateLimitError` (429) | Too many requests or too much usage per minute | Yes, after a delay |
| `APITimeoutError` | Slow network or a long audio file | Yes, with a longer timeout |
| `APIConnectionError` | Dropped connection | Yes |
| `InternalServerError` (5xx) | Provider-side fault | Yes |
| `BadRequestError` (400) | Unsupported format, corrupt file, file over the size limit | No. Fix the input |
| `AuthenticationError` (401) | Missing or wrong API key | No. Fix the key |
Only retry the temporary errors. Retrying a 400 wastes time and money, because the same file is rejected every time.
Retrying with exponential backoff
Exponential backoff doubles the wait after each failure: 1 second, 2 seconds, 4 seconds. This gives a rate-limited service time to recover. The function below retries a transcription and re-raises the error after the last attempt.
import time
from openai import OpenAI, RateLimitError, APITimeoutError
client = OpenAI(timeout=60.0)
def transcribe_with_retry(path, attempts=4):
for attempt in range(attempts):
try:
with open(path, "rb") as f:
return client.audio.transcriptions.create(model="whisper-1", file=f).text
except (RateLimitError, APITimeoutError):
if attempt == attempts - 1:
raise
time.sleep(2 ** attempt)The file is reopened on every attempt. A file handle that was partly read by a failed call would send truncated audio the second time.
The OpenAI SDK also retries some errors by itself. The default is two retries. You can change this with OpenAI(max_retries=...). If you add your own loop on top, the two layers multiply, so a call may be tried more often than you expect.
Rate limits
Providers limit requests per minute, and some limit audio minutes or tokens as well. When you transcribe a long recording in chunks, send them one at a time or with a small worker pool. Sending all chunks at once usually triggers a 429. If a response includes a Retry-After header, wait at least that long.
Keeping a turn from failing completely
A voice turn makes three calls: transcription, chat, and speech. Any one can fail. Decide in advance what the user hears:
- If transcription fails after retries, play a fixed prompt such as "Sorry, I didn't catch that." Keep this audio pre-generated, because the speech endpoint may be what is failing.
- If speech synthesis fails but the chat reply succeeded, show the text instead.
- Log the stage name with every error. "The turn failed" does not tell you which service to investigate.
Cost
Speech pricing differs from chat pricing, and the units differ between the two directions.
- Transcription is billed per minute of audio. Silence counts, so trimming silence before upload reduces cost.
- Text-to-speech is billed per character of input text, so shorter replies cost less. The system prompt "Answer in one or two short spoken sentences" saves money as well as time.
- The chat step is billed per token, and it grows with history length.
Prices change, so read the provider's current pricing page rather than hard-coding figures. To estimate monthly cost, multiply the audio minutes per day by the per-minute rate, and add the reply characters times the per-character rate. Log the audio duration and character count of every call so you can check the estimate against the invoice.
Retries also affect cost. A call that times out may still have been processed and billed, so a retry can charge twice. Set the timeout above your longest expected processing time to avoid this.
Exercise
Your retry function catches every exception and retries up to 4 times. A user uploads a corrupt audio file. What goes wrong, and which change fixes it?
Show answer
A corrupt file causes a 400 BadRequestError, which will never succeed. The function waits 1, 2 and 4 seconds and makes 4 failing calls, and it may bill or count against rate limits for nothing. Catch only temporary errors (RateLimitError, APITimeoutError, APIConnectionError, 5xx) so that a 400 is raised immediately.