little-king-41579
02/17/2026, 3:28 AMtall-airline-16756
02/17/2026, 3:28 AMtall-airline-16756
02/17/2026, 3:28 AMsession.update_agent(). I’ll walk through the typical steps and ask one quick question so I can tailor the answer to your code.
Short question before specifics:
• Are you using Python async agents (asyncio tasks) or another runtime? If Python asyncio, the steps below apply directly.
Steps to stop a running function/task/agent and update the session
1. Stop the running function call or asyncio task
• If the function is running as an asyncio Task, cancel the task:
task.cancel()
• After calling cancel(), ensure you handle asyncio.CancelledError in the function to stop cleanup properly.
1. Interrupt the agent (session-level)
• If the agent is mid-generation, call:
session.interrupt()
This interrupts the agent’s current activity and helps ensure the session is not waiting on the old agent.
1. Complete or cancel AgentTask objects
• If you are using AgentTask instances, call complete() when you want to mark them finished. If the AgentTask is actively running as a background asyncio task, cancel that underlying task as well.
1. Update the session with the new agent
• Once the running work is stopped/interrupted, replace the agent:
session.update_agent(NewAgent())
• Call this only after interruption/cancellation so the session switches cleanly to the new agent.
Important notes and best practices
• Always cancel the actual asyncio Task that is executing the function/agent logic; calling session.interrupt() helps but doesn’t replace cancelling the task itself.
• Ensure your agent code catches asyncio.CancelledError and performs any needed cleanup (closing resources, releasing locks).
• If you need to guarantee no stray tasks remain, track tasks you spawn (keep references) so you can cancel them explicitly.
If you share a short code snippet showing how your agent/task is started (for example, how you create the asyncio Task or how AgentTask is used), I can give exact code to cancel it and then call session.update_agent() without race conditions.
Sources: Agent session | LiveKit Documentation | Agents and handoffs | LiveKit Documentationlittle-king-41579
02/17/2026, 3:39 AMtall-airline-16756
02/17/2026, 3:39 AMsession.update_agent().
Short checklist (what to do)
• Interrupt the session’s current activity with session.interrupt() so the session stops current/queued speech and generation.
• Cancel any underlying asyncio Task you created for the agent/function call (call task.cancel() and handle asyncio.CancelledError in the coroutine).
• If using AgentTask objects, call complete() or otherwise mark them finished, and cancel any background tasks they spawned.
• After interruption and cancellation are confirmed, call session.update_agent(new_agent).
Example flow (Python asyncio)
1. Interrupt session-level activity:
# stop current speech/generation in the session
session.interrupt()
1. Cancel any explicit asyncio tasks you started:
# assume you stored the task when starting the agent work
if agent_task is not None and not agent_task.done():
agent_task.cancel()
try:
await agent_task
except asyncio.CancelledError:
# expected — task was cancelled
pass
1. Ensure AgentTask is finalized (if used):
# if using AgentTask API
if my_agent_task and not my_agent_task.completed:
await my_agent_task.complete() # or otherwise mark it finished
1. Safely update the session agent:
# now replace the agent
session.update_agent(NewAgent())
Key details and best practices
• session.interrupt() is designed to stop speaking/generation and to clear queued speech — call it before attempting update to avoid races.
• Cancel the actual asyncio Task(s). session.interrupt() may not cancel tasks you spawned directly; keep references to those tasks so you can cancel them explicitly.
• Make agent code robust: catch asyncio.CancelledError to clean up resources (audio handles, streams, locks).
• Use short waits/polling or task.await to confirm cancellation before calling update_agent() to avoid a race where the session still thinks an activity is active.
• If your code restarts agents frequently, centralize task tracking (e.g., a single list/dict of active tasks per session) so you can cancel them reliably.
If you want, share:
• A short snippet showing how you start the agent activity / create the asyncio task, and whether you use AgentTask or raw asyncio.
• Whether the activity is speech synthesis, function call, or long-running background work.
With that snippet I’ll provide an exact code patch to cancel the task, call session.interrupt() safely, and then call session.update_agent() without triggering the RuntimeError.
Sources: examples/voice_agents | livekit/livekit_composite | DeepWiki