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 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?
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! If you want to override the
llm_node
method in an
AgentTask
without running into the pesky
RuntimeError: no activity context found, the agent is not running
, the key is to make sure your agent is properly started within an
AgentSession
. This error pops up because the agent isn’t active yet, so runtime parts like the LLM can’t be accessed. Here’s a friendly little guide to safely override `llm_node`: 1. First, make sure you start the agent using
await session.start(agent=...)
before calling any methods that need the agent’s activity context. 2. When you override
llm_node
, call the default implementation with
super().llm_node()
or
Agent.default.llm_node()
. This keeps everything playing nicely with the agent’s lifecycle. 3. Feel free to add your own preprocessing (like injecting prompts) before handing things off to the default method. Here’s an example to make it clearer:
Copy code
class CustomAgent(Agent):
    async def llm_node(self, chat_ctx, tools, model_settings):
        # Custom preprocessing
        user_query = chat_ctx.get_user_message().text_content
        custom_prompt = f"Custom prompt: {user_query}"
        chat_ctx.add_message(role="assistant", content=custom_prompt)

        # Delegate to default implementation
        async for chunk in super().llm_node(chat_ctx, tools, model_settings):
            yield chunk
By following these steps, you’ll keep your agent active and make sure the
llm_node
method fits smoothly into the agent’s lifecycle. If you have any questions or need a hand, just let me know! Sources: Pipeline nodes and hooks | LiveKit Docs | Modifying LLM output before TTS | LiveKit Docs | examples/voice_agents/llamaindex-rag/retrieval.py