silly-baker-11446
08/30/2025, 9:00 PMtall-airline-16756
08/30/2025, 9:00 PMtall-airline-16756
08/30/2025, 9:01 PMRuntimeError: no activity context found pops up because the AgentTask is running outside a valid agent lifecycle context. This error shows up when the task tries to call super().llm_node() without an active AgentActivity, which is essential for pipeline nodes like llm_node.
Here’s how you can fix it:
• Make sure the AgentTask is created and awaited only within supported lifecycle contexts, like inside a tool method decorated with @function_tool or within the on_enter() or on_exit() methods of an active Agent.
• Double-check that the agent is started within an active AgentSession using session.start(agent=..., room=...). This step sets up the necessary activity context.
• If you’re overriding llm_node, be sure to call super().llm_node() inside a valid session and wrap the stream properly so the pipeline keeps running smoothly.
If you want to make persistent, session-level changes to LLM behavior, it might be easier to use a regular Agent subclass instead of an AgentTask. This way, managing the lifecycle is simpler and works nicely with the session’s activity context.
Feel free to reach out if you want me to walk you through these steps or if you have any other questions. I’m here to help!
Sources: examples/voice_agents/llamaindex-rag/retrieval.py | Workflows | LiveKit Docs | Pipeline nodes and hooks | LiveKit Docs | Modifying LLM output before TTS | LiveKit Docssilly-baker-11446
08/30/2025, 9:23 PMtall-airline-16756
08/30/2025, 9:23 PMon_user_turn_completed() method is a handy lifecycle context for AgentTask operations in LiveKit. It lets you work with user responses using your own custom logic before the agent replies. This is great for adding personalized prompts, checking user answers, and figuring out what to do next in multi-turn conversations.
To help you evaluate each user response with custom prompts and decide the next steps, here are some friendly recommendations:
• Use `on_user_turn_completed()`: This is the perfect spot to call your AgentTask or add your custom logic. You can tweak the chat context, add prompts, and assess responses right here.
• Override llm_node in the Agent class: If you want to change LLM behavior for the whole session, overriding llm_node in the Agent class is a straightforward and effective way—often simpler than using AgentTask.
• Use @function_tool decorated methods: For specific, tool-like tasks that need custom LLM logic, you can wrap that logic in @function_tool methods and call them from on_user_turn_completed().
When setting up multi-turn conversation logic, here’s a friendly pattern to follow:
1. Override on_user_turn_completed() to handle each user response.
2. Add any custom system and user prompts into the chat context as needed.
3. Use AgentTask or @function_tool methods to review the conversation and decide what to do next.
4. Update the chat context or generate a reply based on that decision.
If you want, I’m here to help with more guidance on these patterns or any other questions you have!
Sources: External data and RAG | LiveKit Docs | Pipeline nodes and hooks | LiveKit Docs | examples/voice_agents/README.mdsilly-baker-11446
08/30/2025, 9:26 PMtall-airline-16756
08/30/2025, 9:27 PMllm_node override, the best way is to call Agent.default.llm_node() instead of super().llm_node(). This lets you keep the default LLM behavior but still tweak the chat context or tools however you need.
Here’s a simple example to show you how it works:
async def llm_node(self, chat_ctx, tools, model_settings):
# Add your custom prompts to the chat context
chat_ctx.add_message(role="system", content=self.system_prompt)
chat_ctx.add_message(role="user", content=custom_user_prompt)
# Then hand it off to the default LLM processing
async for chunk in Agent.default.llm_node(self, chat_ctx, tools, model_settings):
yield chunk
This way, you stay fully compatible with the LiveKit framework and avoid that pesky RuntimeError: no activity context found issue. If anything’s unclear or you want a hand setting this up, just let me know—I’m happy to help!
Sources: Pipeline nodes and hooks | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py | Agents 0.x migration guide | LiveKit Docssilly-baker-11446
08/30/2025, 10:20 PMtall-airline-16756
08/30/2025, 10:20 PMAgent.default.llm_node() from within an AgentTask, you’ll want to pass the right agent instance as the first argument. Since self in your AgentTask points to the task itself and not the agent, you’ll need to explicitly pass the agent instance to Agent.default.llm_node().
Here are a couple of friendly ways to do this:
1. Pass the agent instance when you create the task: When you’re setting up the task, pass the agent instance as an argument and save it as a property on the task. Then, in your override, use Agent.default.llm_node(self.agent, ...).
class InterviewPlanningTask(AgentTask):\n def __init__(self, agent, *args, **kwargs):\n super().__init__(*args, **kwargs)\n self.agent = agent\n\n async def llm_node(self, chat_ctx, tools, model_settings):\n chat_ctx.add_message(role=\"system\", content=self.system_prompt)\n async for chunk in Agent.default.llm_node(self.agent, chat_ctx, tools, model_settings):\n yield chunk\n
2. Grab the agent from the task’s session: If your task is running inside an active session, you can get the agent instance through self.session.agent, as long as the session is set up properly.
async def llm_node(self, chat_ctx, tools, model_settings):\n chat_ctx.add_message(role=\"system\", content=self.system_prompt)\n async for chunk in Agent.default.llm_node(self.session.agent, chat_ctx, tools, model_settings):\n yield chunk\n
Either way, this makes sure the right agent instance gets passed to Agent.default.llm_node(), so you won’t run into the RuntimeError: no activity context found problem. If you want me to walk you through it or have any questions, just let me know—I’m happy to help!
Sources: livekit-agents/livekit/agents/voice/agent_activity.py | Pipeline nodes and hooks | LiveKit Docssilly-baker-11446
08/30/2025, 10:53 PMtall-airline-16756
08/30/2025, 10:54 PM