miniature-lifeguard-87511
07/22/2025, 7:09 AMtall-airline-16756
07/22/2025, 7:09 AMtall-airline-16756
07/22/2025, 7:11 AMsession = AgentSession( vad=ctx.proc.userdata["vad"], llm=openai.LLM(model="gpt-4o-mini"), stt=deepgram.STT(model="nova-3", language="multi"), tts=openai.TTS(), turn_detection=MultilingualModel(), preemptive_generation=True, )
• Monitor and Optimize: Use the Agents metrics API and RealtimeModelMetrics to measure latency at each stage (VAD, STT, LLM, TTS) and identify bottlenecks.
• Provider-Specific TTS Optimizations: For TTS services like Rime, set reduce_latency=true to skip text normalization, and adjust sample_rate, audio_format, and speed_alpha for further tuning.
• Incremental Processing: For STT, use incremental transcriptions rather than sending long speech segments to reduce processing time.
• Provider Selection: Choose low-latency providers for each pipeline component and follow their optimization recommendations.
Monitor Time To First Token (TTFT) and Time To First Byte (TTFB) to track improvements. Fine-tune parameters and use logging/telemetry to validate changes.
These steps will help you achieve lower latency and a more responsive voice agent experience.
Sources: Live Conversations with AI using ChatGPT and WebRTC | Agent speech and audio | LiveKit Docs | examples/voice_agents/preemptive_generation.py | Rime integration guide | LiveKit Docs | VPAOptions | LiveKit Agentsminiature-lifeguard-87511
07/22/2025, 7:20 AMtall-airline-16756
07/22/2025, 7:21 AMimport logging
from dotenv import load_dotenv
from livekit.agents import Agent, AgentSession, JobContext, JobProcess, WorkerOptions, cli, metrics
from livekit.agents.llm import ChatContext, ChatMessage
from livekit.agents.voice import AgentStateChangedEvent, MetricsCollectedEvent
from livekit.plugins import deepgram, openai, silero
from livekit.plugins.turn_detector.multilingual import MultilingualModel
load_dotenv()
logger = logging.getLogger("preemptive-generation")
class MyAgent(Agent):
def __init__(self):
super().__init__(
instructions="Your name is Kelly. You would interact with users via voice. "
"With that in mind keep your responses concise and to the point. "
"You are curious and friendly, and have a sense of humor."
)
async def on_enter(self):
self.session.generate_reply(instructions="say hello to the user")
async def on_user_turn_completed(self, turn_ctx: ChatContext, new_message: ChatMessage):
# Update the chat context or tools here will cancel the preemptive generation
pass
async def entrypoint(ctx: JobContext):
session = AgentSession(
vad=ctx.proc.userdata["vad"],
llm=openai.LLM(model="gpt-4o-mini"),
stt=deepgram.STT(model="nova-3", language="multi"),
tts=openai.TTS(),
turn_detection=MultilingualModel(),
preemptive_generation=True, # Enable preemptive generation
min_endpointing_delay=0.5, # Minimum delay for endpointing
max_endpointing_delay=6.0, # Maximum delay for endpointing
allow_interruptions=True, # Allow user interruptions
)
last_eou_metrics = None
@session.on("metrics_collected")
def _on_metrics_collected(ev: MetricsCollectedEvent):
nonlocal last_eou_metrics
metrics.log_metrics(ev.metrics)
if ev.metrics.type == "eou_metrics":
last_eou_metrics = ev.metrics
@session.on("agent_state_changed")
def _on_agent_state_changed(ev: AgentStateChangedEvent):
if (
ev.new_state == "speaking"
and last_eou_metrics
and last_eou_metrics.speech_id == session.current_speech.id
):
<http://logger.info|logger.info>(
f"End-to-end latency: {ev.created_at - last_eou_metrics.last_speaking_time}"
)
await session.start(agent=MyAgent(), room=ctx.room)
def prewarm(proc: JobProcess):
proc.userdata["vad"] = silero.VAD.load()
if __name__ == "__main__":
cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint, prewarm_fnc=prewarm))
I've got some awesome features packed into this code! We're talking preemptive generation (so quick!), super low delays, and the ability to jump in and interrupt. Plus, we're tracking metrics to make sure everything runs smoothly. Feel free to tweak the STT, TTS, and LLM providers to fit exactly what you need. Let me know if you want to dive deeper!
Sources: examples/voice_agents/basic_agent.py | Agent speech and audio | LiveKit Docsminiature-lifeguard-87511
07/22/2025, 7:33 AMtall-airline-16756
07/22/2025, 7:34 AMmin_endpointing_delay, max_endpointing_delay, min_interruption_duration settings to make your conversation flow super naturally.
• Consecutive Speech Delay Management: Use min_consecutive_speech_delay to keep your conversations feeling smooth and not too choppy. It's all about that conversational rhythm!
• Remote Processing Architecture: For those heavy-duty tasks, imagine having a dedicated teammate who jumps right into the conversation and handles complex processing - that's what this technique does!
• Provider-Specific Optimizations: Shop around for the speediest providers and follow their insider tips. Every little optimization counts!
• Comprehensive Metrics and Monitoring: Keep an eye on those performance metrics like Time To First Token and component-level latencies. It's like having a health tracker for your voice agent!
By mixing these smart techniques with some proactive planning, you'll be well on your way to creating an incredibly responsive and delightful voice agent. You've got this!
Sources: examples/voice_agents/preemptive_generation.py | Bringing AI avatars to voice agents | Agents Core Architecture | livekit/livekit_composite | DeepWiki | AI Agents Framework | livekit/livekit_composite | DeepWiki | Voice Assistants | livekit/livekit_composite | DeepWiki