tall-autumn-57974
08/14/2025, 3:39 PMtransfer
feature, where you can return an agent in a tool, I often have this exception:
2025-08-14 17:36:15,848 - ERROR asyncio - Task exception was never retrieved
future: <Task finished name='_update_activity_task' coro=<AgentSession._update_activity_task() done, defined at /Users/pybuche/Work/Vocca/livekit-poc/.venv/lib/python3.13/site-packages/livekit/agents/utils/log.py:13> exception=RuntimeError('cannot start agent: an activity is already running')>
Traceback (most recent call last):
File "/Users/pybuche/Work/Vocca/livekit-poc/.venv/lib/python3.13/site-packages/livekit/agents/utils/log.py", line 16, in async_fn_logs
return await fn(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/pybuche/Work/Vocca/livekit-poc/.venv/lib/python3.13/site-packages/livekit/agents/voice/agent_session.py", line 863, in _update_activity_task
await self._update_activity(task)
File "/Users/pybuche/Work/Vocca/livekit-poc/.venv/lib/python3.13/site-packages/livekit/agents/voice/agent_session.py", line 820, in _update_activity
raise RuntimeError("cannot start agent: an activity is already running")
RuntimeError: cannot start agent: an activity is already running {"pid": 81667, "job_id": "AJ_bwycwVLAzS27"}
Whereas my transfer_to_agent
tool code is pretty simple and copied from LiveKit's docs and examples
@function_tool()
async def transfer_to_agent(
self,
context: RunContext_T,
agent_name: Literal[
"booking", "reschedule", "cancel", "delay", "questions", "welcome"
],
) -> Agent:
next_agent = context.userdata.agents[agent_name]
await next_agent.update_chat_ctx(self.session.history)
return next_agent
How can I fix this? This causes the bot to not answer the caller, which forces the caller to repeat their question.tall-airline-16756
08/14/2025, 3:39 PMtall-airline-16756
08/14/2025, 3:40 PMtall-autumn-57974
08/14/2025, 3:42 PMtall-airline-16756
08/14/2025, 3:43 PMupdate_agent
, double-check that the current activity is drained or paused. You can do this by explicitly calling await session.drain()
or await session.pause()
if needed.
• Make sure the AgentSession
is properly started before you switch agents.
• Take a look to see if the new agent already has an active activity. If it does, you might want to pause or close that existing activity first.
If you’re still running into trouble, just let me know—I’m here to help you debug your specific setup! Sources: complex-agents/role-playing/agents/base_agent.py | livekit-agents/livekit/agents/voice/agent_activity.pytall-autumn-57974
08/14/2025, 3:56 PMawait next_agent.update_chat_ctx(self.session.history)
tall-airline-16756
08/14/2025, 3:57 PMupdate_chat_ctx
.
• Try using lifecycle hooks like on_user_turn_completed
to update the chat context safely.
• Check the agent’s state before updating the chat context. For example: if hasattr(self, '_activity') and self._activity is not None:
await self.update_chat_ctx(new_context)
else:
self._chat_ctx = new_context.copy(tools=self._tools)
• Add some error handling to manage conflicts, like retrying the update if an activity is already running.
If you’re still running into trouble, you might want to try deferring the update with a retry mechanism to handle timing conflicts. Feel free to reach out if you want a hand with putting these changes in place—I’m here to help! Sources: livekit-agents/livekit/agents/voice/agent_activity.py | Pipeline nodes and hooks | LiveKit Docs