Hi LiveKit team, I'm trying to override the llm_no...
# ask-ai
s
Hi LiveKit team, I'm trying to override the llm_node method in an AgentTask to inject custom prompts, but I'm getting a RuntimeError: no activity context found, the agent is not running when the task tries to call the LLM. My setup: • Using AgentSession with openai.LLM(model="gpt-4o") and openai.TTS() (not Realtime API) • Custom AgentTask class that inherits from AgentTask[str] • Overriding llm_node(self, chat_ctx, tools, model_settings) method The error occurs when I try to: 1. Agent.default.llm_node(self, chat_ctx, tools, model_settings) → RuntimeError 2. self.session.agent.llm.generate(chat_ctx) → RuntimeError Question: What's the correct way to override llm_node in an AgentTask and call the LLM without getting the activity context error? Should I: • Create a standalone LLM instance within the task? • Pass the LLM instance to the task constructor? • Use a different approach entirely? Can you provide a code example of properly overriding llm_node in an AgentTask? Full error traceback: text Apply to total_agent.... File "total_agent.py", line 113, in llm_node return await self.session.agent.llm.generate(chat_ctx) ^^^^^^^^^^^^ RuntimeError: no activity context found, the agent is not running Thanks!
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.
Could you help me understand if your custom AgentTask is running inside an active AgentSession? Also, are you working with any asynchronous frameworks or special settings that might change how things run? Knowing this will make sure we find a solution that fits your setup perfectly.
s
Follow-up Response: Yes, the custom AgentTask is running inside an active AgentSession. Here are the specific details: AgentSession Setup: python Apply to total_agent.... session = AgentSession( stt=deepgram.STT(model="nova-2"), llm=openai.LLM(model="gpt-4o"), tts=openai.TTS(voice="alloy"), vad=silero.VAD.load(), _turn_detection_=MultilingualModel(), _min_endpointing_delay_=0.5, _max_endpointing_delay_=3.0 ) Task Creation Context: The AgentTask is created inside the agent's on_user_turn_completed method, which is called by LiveKit's framework when a user completes their turn: class TotalInterviewAgent(Agent): async def on_user_turn_completed(self, _turn_ctx_, _new_message_): # This is where we create the task self.interview_task = InterviewPlanningTask(...) _# Then immediately call the task's evaluate_and_plan method_ result = await self.interview_task.evaluate_and_plan(turn_ctx, new_message, 1) Task's llm_node Override: class InterviewPlanningTask(AgentTask[str]): async def llm_node(self, _chat_ctx_, tools, _model_settings_): # This is where the error occurs return await self.session.agent.llm.generate(chat_ctx) # RuntimeError here The specific error: The error happens when the task tries to access the LLM through self.session.agent.llm.generate() - it seems like even though we're inside an active session, the task doesn't have the proper activity context to access the session's LLM. Question: Should the AgentTask be accessing the LLM differently, or should we be passing the LLM instance to the task constructor instead of trying to access it through the session?