Reducing latency in AI voice agents with Kokoro TTS and Amazon SageMaker

AI voice agents often struggle with high latency, as they typically wait for a full response to be generated before beginning audio synthesis. This design gap can result in significant delays, leaving users waiting for audio to start. A new solution, leveraging the Kokoro-82M text-to-speech (TTS) model on Amazon SageMaker AI, demonstrates how to minimize this latency by streaming individual sentences to the TTS model as soon as the agent produces them.

The mechanism for reduced latency

By implementing a bidirectional streaming architecture, the system allows audio playback to commence on the first sentence of a response, rather than waiting for the entire multi-paragraph output. The Kokoro-82M model, which contains 82 million parameters, is deployed in a custom container on a SageMaker AI real-time endpoint using an ml.g5.xlarge GPU instance. This setup maintains both low latency and cost-efficiency for agents handling real-world traffic.

To achieve this, the system uses a persistent WebSocket connection that multiplexes raw binary audio and JSON control frames. This approach avoids the overhead associated with base64-encoding audio for transport, which can inflate payloads by approximately 33% and complicate frame alignment.

Technical implementation

The solution requires handling specific WebSocket frame types and maintaining an open session between messages. Developers use an IncrementalSentenceChunker class to process incoming text fragments and identify completed sentences for immediate synthesis. This ensures that synthesis starts as soon as a grammatical unit is formed, providing a smoother experience for the end user.

Testing confirms significant performance improvements. In isolation, sending a single sentence to the endpoint cut the time to the first audio byte by 52.4%. When integrated into a real-world agent workflow, the bidirectional streaming approach demonstrated a 2.4 to 3.5 times improvement in speed compared to traditional batch-processing methods, where synthesis waits for the complete response generation.

Considerations for deployment

While the unified binary and JSON approach optimizes for latency, the provided guidance notes that for existing architectures already utilizing Server-Sent Events (SSE), a base64-encoded audio approach may remain a pragmatic, albeit less efficient, option. Engineers should prioritize robust handling of binary frames and avoid eager session closure to ensure consistent performance across multiple sentences. Developers are encouraged to review the provided AWS repository and ensure all container security pins, such as those related to PyTorch, are addressed for production use.

Source: aws.amazon.com
Original article: https://aws.amazon.com/blogs/machine-learning/stream-low-latency-speech-from-ai-agents-using-kokoro-tts-on-amazon-sagemaker-ai/

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *