silly-baker-11446
08/30/2025, 11:09 PMtall-airline-16756
08/30/2025, 11:09 PMtall-airline-16756
08/30/2025, 11:10 PMAgentTask in LiveKit can access the session and agent instance—it’s pretty straightforward:
• Session Access: You can get to the session through the session property of the AgentTask. This works by grabbing the related AgentSession using _get_activity_or_raise().session. For example, if you want to generate a reply, you’d use self.session.generate_reply().
• Agent Instance Access: Since AgentTask inherits from the Agent class, it is an agent instance itself. So, inside its methods like on_enter or on_exit, self refers to the agent instance. That means you can easily access its properties and methods, like self.instructions or self.chat_ctx.
Behind the scenes, the framework keeps the connection between the AgentTask and its AgentSession running smoothly through an AgentActivity object. This way, everything works together seamlessly—making your life a bit easier!
Sources: livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti... | Workflows | LiveKit Docssilly-baker-11446
08/30/2025, 11:16 PMtall-airline-16756
08/30/2025, 11:16 PMRuntimeError: no activity context found pops up because the _get_activity_or_raise() method expects a running AgentActivity. This activity only gets set up when your agent is properly started within an AgentSession. Here’s how you can fix it:
1. Make Sure Everything’s Initialized Right: First, create an instance of your Agent subclass and set it up with all the parameters it needs.
2. Start the AgentSession: Use the AgentSession class to handle the agent’s lifecycle. For example:async def main():
sess = AgentSession(
turn_detection=..., stt=stt_instance, llm=llm_instance, tts=tts_instance
)
await sess.start(agent)
await sess.aclose()
3. Understand the Activity Context: When you call AgentSession.start(), it creates an AgentActivity behind the scenes. This sets up the context so methods like llm_node can run smoothly without errors.
Just a heads-up: if you try calling llm_node directly on an AgentTask before the task is managed by a started AgentSession, you’ll hit this error. So, make sure your task is connected to the session by running it within the session lifecycle. Hope this helps clear things up!
Sources: livekit-agents/livekit/agents/voice/generation.py | Pipeline nodes and hooks | LiveKit Docssilly-baker-11446
08/30/2025, 11:18 PMtall-airline-16756
08/30/2025, 11:18 PMAgentTask with AgentSession and make sure the activity context is just right, here’s a friendly step-by-step guide:
1. Define the Task: Start by extending the AgentTask class and implementing methods like on_enter to kick off your interactions. When your task wraps up, just call self.complete() to return the results. Here’s a quick example:class InterviewPlanningTask(AgentTask[bool]):\n async def on_enter(self):\n await self.session.generate_reply(instructions=\"Start planning the interview.\")\n def complete_task(self, result):\n self.complete(result)
2. Run the Task: Next, create and await the task while your AgentSession is active. For example:class TotalInterviewAgent(Agent):\n async def on_enter(self):\n result = await InterviewPlanningTask()\n if result:\n await self.session.generate_reply(instructions=\"Interview planning completed.\")
3. Context Management: Make sure you create and await the task only inside on_enter, on_exit, or tool functions of another Agent. This way, the AgentSession provides the proper activity context you need.
4. Session Configuration: Your task will naturally inherit settings like llm, stt, and tts from the AgentSession, unless you specify them when creating the task.
5. Typed Results: If you want to structure your outputs neatly, use custom dataclasses. Here’s an example:from dataclasses import dataclass\n\n@dataclass\nclass InterviewPlan:\n steps: list\n\nclass InterviewPlanningTask(AgentTask[InterviewPlan]):\n def complete_plan(self, steps):\n self.complete(InterviewPlan(steps=steps))
To wrap it up, remember that AgentTask needs to run within the active lifecycle of an AgentSession. It’s not meant to be manually created and called outside this context. So, keep your task creation and awaiting inside your session’s agent methods, like on_enter. If you have any questions or want to chat more about this, I’m here to help!
Sources: livekit-agents/livekit/agents/__init__.py | Workflows | LiveKit Docs