I'm implementing a custom AgentTask with an llm_no...
# ask-ai
s
I'm implementing a custom AgentTask with an llm_node override as you previously recommended. The task is being used within an active AgentSession that was started with await session.start(agent=agent, room=ctx.room). However, when my task's llm_node method calls super().llm_node(chat_ctx, tools, model_settings), I'm getting this error: RuntimeError: no activity context found, the agent is not running The error occurs at this line in my AgentTask: async for chunk in super().llm_node(chat_ctx, tools, model_settings): yield chunk My AgentTask is being called from within the agent's on_user_turn_completed method like this: async def on_user_turn_completed(self, _turn_ctx_, _new_message_): result = await self.interview_task.evaluate_and_plan( _turn_ctx_=turn_ctx, _new_message_=new_message.text_content, _current_question_count_=self.interview_task.questions_asked ) And the task's evaluate_and_plan method calls self.llm_node(turn_ctx) directly. Is there a different way I should be calling the parent llm_node method from within an AgentTask, or should I be using a different approach to get LLM responses from within a task that's running inside an active AgentSession?"
t
Thanks for reaching out, Jason. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! The error pops up because the agent's activity context isn’t set when you call
super().llm_node()
. This context only gets initialized when the agent is actively running inside an
AgentSession
. To fix this, just make sure you: • Create an
AgentSession
before doing any LLM operations. • Start the session with `await session.start(agent=agent, room=ctx.room)`—this sets up the agent’s activity context. • Call
super().llm_node()
only while the session is active. Here’s a quick example of how it should look:
Copy code
from livekit.agents import AgentSession

session = AgentSession()  # Create the session
await session.start(agent=agent, room=ctx.room)  # Start with agent and room

# Now the agent is active and can use super().llm_node()
Just double-check that your
AgentTask
is set up and used inside this active session to avoid running into that
RuntimeError
. Hope this helps! Sources: livekit-agents/livekit/agents/voice/generation.py
s
Thanks for the clarification! You're right that the AgentSession is properly started with await session.start(agent=agent, room=ctx.room). The issue is that I'm calling super().llm_node() from within my custom AgentTask class, but the task doesn't have the agent's activity context. My current structure is: python Apply to total_agent.... class InterviewPlanningTask(AgentTask): async def llm_node(self, _chat_ctx_, tools=[], _model_settings_=None): _# Add custom prompts to chat_ctx_ chat_ctx.add_message(role="system", content=self.system_prompt) chat_ctx.add_message(role="user", content=custom_user_prompt) # This line fails with "no activity context found" async for chunk in super().llm_node(chat_ctx, tools, model_settings): yield chunk Since AgentTask doesn't have the agent's activity context, what's the correct way to get LLM responses from within a task? Should I: 1. Access the agent's LLM directly somehow from within the task? 2. Use a different method to call the LLM from within the task? 3. Or is there a way to pass the agent's context to the task's llm_node method? What's the recommended pattern for custom LLM processing within an AgentTask that's running inside an active AgentSession?"
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!
s
really? please try to answer it
a
AttributeError: 'JobContext' object has no attribute 'chat_ctx' I'm getting above error when I'm using this command ctx.chat_ctx.add_message(role="system", content="IMPORTANT INSTRUCTION:")