Give me examples of using AgentTask. I need to exe...
# ask-ai
b
Give me examples of using AgentTask. I need to execute 3 tasks in sequence
t
Thanks for reaching out, Delivery. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd love to help you execute 3 tasks in sequence using
AgentTask
. Here’s a simple pattern you can follow:
Copy code
async def execute_three_tasks_in_sequence(agent_session):
    # Task 1: Collect user information
    user_info_task = CollectUserInfoTask()
    user_info = await user_info_task

    # Task 2: Process the information  
    processing_task = ProcessDataTask(user_info.data)
    processed_result = await processing_task

    # Task 3: Generate final response
    response_task = GenerateResponseTask(processed_result.output)
    final_result = await response_task

    return final_result
Here are a few important things to keep in mind: • Make sure to await each task before moving on to the next one. • These tasks run within the context of an active
Agent
inside an
AgentSession
. • You can get typed results from tasks using generics, which helps keep things clear and organized. If you’d like, I’m here to help you implement a specific sequence of tasks. Just let me know what kind of tasks you’re working with, and we can figure it out together! Sources: Workflows | LiveKit Docs | tests/test_agent_session.py
b
How to run those tasks within agent session?
t
Hi there! To help you run tasks within an
AgentSession
, here’s a friendly step-by-step guide you can follow: 1. Define the Task: Start by extending the
AgentTask
class and implement the
on_enter
method to describe what your task should do. When your task finishes, just call
self.complete(result)
to send back the result.
Copy code
from dataclasses import dataclass
from livekit.agents import AgentTask

@dataclass
class RandomResult:
    random_number: int

class InlineAgent(AgentTask[RandomResult]):
    async def on_enter(self) -> None:
        # Perform task logic
        self.complete(RandomResult(random_number=42))
1. Run the Task: Next, create and await the
AgentTask
instance inside a function tool of an active
Agent
. This way, your task will run smoothly and return its result automatically.
Copy code
@function_tool
async def start_random_generator(self, ctx: RunContext):
    random_result = await InlineAgent()
    return random_result.random_number
1. Start the AgentSession: Then, start the session with an agent that knows about your task. You can call the task through tool calls as you interact.
Copy code
async with openai.LLM(model="gpt-4o-mini") as llm, AgentSession(llm=llm) as sess:
    await sess.start(AshAgent(oneshot=True))
    result = await sess.run(user_input="Start the random generator?")
If you want to run tasks one after another, simply chain them by awaiting each task before moving on to the next one, like this:
Copy code
class MultiTaskAgent(Agent):
    @function_tool
    async def run_multiple_tasks(self, ctx: RunContext):
        # First task
        first_result = await FirstTask()
        
        # Second task (runs after first completes)
        second_result = await SecondTask(input_data=first_result.data)
        
        # Third task
        final_result = await ThirdTask(previous_results=[first_result, second_result])
        
        return final_result.summary
If you’d like, I’m here to help you implement this in your specific situation or answer any questions you might have. Just let me know! Sources: Workflows | LiveKit Docs | tests/test_agent_session.py | agents/src/voice/agent_activity.ts