I have a workflow. The first step in the workflow ...
# ask-ai
m
I have a workflow. The first step in the workflow is kind of a router agent, that routes to other agent. Everytime the user is done taking, I want the router agent to execute first. Currently, I have implemented it by updating the agent in
on_user_turn_completed
. But the latency is high! Currently preemptive generation is turned off as I am changing the instructions and tools in
on_user_turn_completed
. How can I reduce latency? Any way I can acheive this and take advantage of preemptive generation?
t
Thanks for reaching out, Vinod. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To help reduce latency in your workflow while still benefiting from preemptive generation, you can enable the
preemptive_generation
feature. This wonderful feature allows the agent to begin generating a response as soon as a user transcript is received, effectively overlapping model inference with the ongoing user audio. This can make your response feel much quicker and smoother. That said, preemptive generation works best when these conditions are met once the user turn completes: • The transcript matches exactly. • The chat context hasn’t changed. • The available tools and tool choices stay the same. If you’re making changes to instructions or tools in
on_user_turn_completed
, it might interfere with preemptive generation, leading to a new response being generated and reducing those latency benefits. To help with this, you might want to: • Keep changes in
on_user_turn_completed
to a minimum to ensure preemptive generation works smoothly. • Try using a lightweight LLM to create quick acknowledgment responses, which can make things feel more responsive. • Consider speeding up output audio playback with time-stretching techniques, so responses come through faster without affecting pitch. Please feel free to reach out if you’d like any help implementing these strategies—I’m here to support you! Sources: tests/test_agent_session.py | Agent speech and audio | LiveKit Docs | Agent Session & Client Integration | livekit/livekit_composite | DeepWiki
m
currently I am updating the tools on user turn completed. but I do not need the user's final message to do my updates. is there a better way to do my updates and take advantage of premptive generation?
t
To help you update tools without waiting for the user's turn to complete, while still benefiting from preemptive generation, you can use the
update_tools()
method. This is a great way to dynamically adjust the tools during an active session without depending on
on_user_turn_completed
. Here’s a friendly guide on how to do it: • Use
await agent.update_tools([tool1, tool2])
to update the tools on the fly. This method updates the tool context right away and works independently of the user's speech turn. • Make sure the tools match the schema expected by the LLM backend to keep everything running smoothly (for example, OpenAI Realtime checks tools against its API schema). • Keep in mind that for some models like Google Gemini, changing tools might require restarting the session, so it’s good to plan for saving the state if needed. This approach helps you avoid interrupting preemptive generation while still allowing you to update tools dynamically. If you’d like, I’m here to walk you through the implementation or answer any questions you have! Sources: Real-time AI Models & Streaming | livekit/livekit_composite | DeepWiki | Agent Session & Client Integration | livekit/livekit_composite | DeepWiki | plugins/openai/src/realtime/realtime_model.ts | livekit-agents/livekit/agents/beta/workflows/email_address.py | Tool definition and use | LiveKit Docs
m
but when should I call update_tools(). This should be called for every turn
t
To make the most out of calling
update_tools()
for every turn, here are some friendly tips to guide you: • Compare Before Updating: It’s a great idea to only call
update_tools()
when the tool set has truly changed. This way, you can compare the current tools with the new set and avoid unnecessary updates. • Validate Tools: Double-check that all tools fit the expected schema. This helps prevent any tools from being dropped during the update process. • Batch Updates: If you can, try grouping tool changes together. This helps reduce how often you update and can prevent session restarts, which some models like Google Gemini require. • Dynamic Updates: Feel free to use
update_tools()
dynamically during your workflows. This way, you can enable or disable tools depending on the conversation or what the user needs. For your situation, calling
update_tools()
at the start of each turn works well—just make sure to check if the tools have actually changed first to keep things running smoothly. If you’d like, I’m here to help you set this up! Sources: livekit-agents/livekit/agents/beta/workflows/email_address.py | Agent Session & Client Integration | livekit/livekit_composite | DeepWiki | Docs::Agents