Hi, When using the `transfer` feature, where you c...
# ask-ai
t
Hi, When using the
transfer
feature, where you can return an agent in a tool, I often have this exception:
Copy code
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
Copy code
@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.
t
Thanks for reaching out, Pierrick. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Could you share a bit more about what you were doing right before this error popped up? Like, was the transfer happening while something else was still running, or does this error show up under certain conditions every time? Getting these details will really help us figure out what's going on.
t
I built a tool to give the ability to my bot to be able to switch agents easily, for instance booking vs cancelling agent. Very often (but not 100%), when this tool is called, I get this exception.
t
The error happens because the system is trying to start a new agent activity while another one is still running. To fix this, make sure the current activity is properly closed or paused before you start a new one. Here are some friendly tips to help you out: • Before calling
update_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.py
t
After some checks, I think it comes from this line:
await next_agent.update_chat_ctx(self.session.history)
t
The issue with await next_agent.update_chat_ctx(self.session.history) probably happens because the agent's activity state isn’t being managed quite right during the update. Here are some friendly tips to help you fix this: • Make sure the current agent activity is properly closed or paused before you call
update_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:
Copy code
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