how to build a dterministic workflow?
# ask-ai
d
how to build a dterministic workflow?
t
Thanks for reaching out, Felipe. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
If you're looking to build a deterministic workflow in LiveKit, the Agents framework is a great place to start. It helps you break down complex processes into manageable agents or tasks, making everything run smoothly and reliably. Here’s a quick rundown: • Define Agents: You create agents by extending the
Agent
class, where you can set up instructions, speech-to-text (STT), text-to-speech (TTS), and more. • Agent Handoff: You can switch between agents easily using
session.update_agent(new_agent)
or even do it dynamically from a tool call. • State Management: Keep things consistent by passing data between agents with
session.state
or
session.userdata
, so your workflow progresses just the way you expect. • Declarative Workflow Orchestration: Set up a flow dictionary that links stages to agents and defines what comes next, giving you a clear, structured pipeline. • Tasks (Experimental): Tasks let you temporarily take control of a session for specific goals, like transcribing audio or checking input. If you want to dive deeper, check out the Workflows Documentation and the Declarative Flow Example—they have some great examples to guide you. This setup is perfect for making sure your deterministic business processes, like customer support or automated surveys, run reliably every time. Sources: Avoiding Room ID Reuse When Re-Creating Rooms With the Same Name | Building the all-in-one platform for voice AI agents | Building the all-in-one platform for voice AI agents | Workflows | LiveKit Docs | Workflows | LiveKit Docs
d
need to create an agent that orchestrates different tasks.
t
Here’s a friendly guide to creating an agent that juggles different tasks in LiveKit: • Define Custom Agents: Start by extending the
Agent
class to build agents, each tailored with specific instructions and tools for their unique role. • Orchestrate Agent Handoffs: Use
llm.handoff()
inside a tool call to smoothly pass control between agents whenever needed. • Use Tasks: Try out tasks for short-term jobs like transcription or sentiment analysis (it’s an experimental feature, so feel free to explore). • Share State: Keep things connected by using
userData
to save and share session info across agents, ensuring everything flows seamlessly. • Initialize the Session: Kick things off with an entry agent via
session.start()
, then call on other agents as the session evolves. This setup is great for managing step-by-step interactions, like customer service journeys or complex workflows. If you want me to walk you through any part or share some examples, just let me know—I’m here to help! Sources: examples/src/restaurant_agent.ts | Workflows | LiveKit Docs | Workflows | LiveKit Docs
d
why should i use tasks intead of agents
t
Think of tasks as your go-to for quick, specific jobs that have clear success or failure outcomes and return neat, structured results to the parent agent. For example, grabbing structured info like email addresses or getting recording consent. Tasks are super modular and wrap up fast, handing things right back to the calling agent. On the other hand, agents are great when you need to take the reins for a longer stretch—like managing whole conversation phases or switching hats between roles (say, from a general assistant to a billing specialist). They’re perfect for keeping things running smoothly over time and handling ongoing interactions. Just a heads-up: tasks are still a bit experimental and only available in Python for now, while agents are rock-solid and supported in both Python and Node.js. If you want me to dive deeper or share some examples, just give me a shout! Sources: Workflows | LiveKit Docs | Workflows | LiveKit Docs
d
but i can use an agent to crate tasks. why using tasks?
t
Hey there! So, while agents can mimic task-like behavior, tasks are actually built for focused, reusable, and temporary operations that give you typed results and then hand control back to the calling agent. This makes them perfect for specific actions like gathering structured data, getting consent, or walking users through a set of questions. Tasks help keep your code neat and reusable by breaking down complex agent logic into smaller, easy-to-test pieces. They also work seamlessly with tools, keep context intact, and return results asynchronously, which makes it super easy to fit them into bigger workflows. Just a heads up, tasks are still experimental, currently only available in Python, and not yet supported in Node.js. If you want me to explain more or show some examples, just let me know—I’m here to help! Sources: Workflows | LiveKit Docs | Workflows | LiveKit Docs
d
i want to create an agent that orchestates many tasks, a fomr complete agent where i can pass tasks as inputs
t
Here’s a friendly guide to creating an agent that juggles multiple tasks in LiveKit: • Define Custom Agents: Start by extending the
Agent
class to build agents tailored for specific roles or tasks. You can use tools to set up transitions or actions. • Orchestrate Tasks: Think of Tasks as short-term helpers for things like filling out forms or gathering data. Agents can call these tasks and get results once they’re done. • Agent Handoffs: Easily switch control between agents by using
session.update_agent()
or by returning a new agent instance from a tool call. • Share State: Keep everything connected by passing shared context (like
chat_ctx
) between agents so info isn’t lost during transitions. • Start the Session: Kick things off by initializing the session with an entry agent via
AgentSession
, then call on tasks or other agents whenever you need. If you want to see these steps in action, check out the Drive-thru Agent or the Restaurant Agent. And hey, if you have any questions or want a hand, just let me know—I’m here to help! Sources: Building voice agents | LiveKit Docs | Deploying to custom environments | LiveKit Docs | Workflows | LiveKit Docs
d
what can an agent return to go back to the agent that instanciated ot.
t
Hey there! Just want to make sure I understand you correctly—are you asking about how an agent can hand back control programmatically (like using certain return values or updating the session), or are you talking about managing the session state to pick up where the original agent left off? Let me know so I can give you the best possible answer!
d
si
yes
i ned to return mack control to the orchestator agent.
t
Hey there! If you want to hand control back to the agent that started another agent, here are some friendly tips to help you out: • Keep a Reference to the Original Agent: Before passing control to another agent, save the original agent in the session state (like
session.userdata
). Then, when you’re ready, just call
session.update_agent()
to switch back smoothly. • Create a Tool to Return the Original Agent: You can set up a tool inside your current agent that sends you back to the original one. Here’s a quick example:
Copy code
@function_tool()
async def return_to_original(self):
    return "Returning to the main agent.", self.original_agent
• Keep Your Data Safe: Use
session.userdata
to store any shared info along with the original agent reference. This way, nothing gets lost when you switch back. • Merge Chat History: When you pick up with the original agent again, use
chat_ctx.merge()
to blend the conversation history and keep everything feeling natural. These tips should help you create smooth handoffs and keep the conversation flowing nicely. If you want me to walk you through any part or share more examples, just let me know—I’m here to help! Sources: Tool definition and use | LiveKit Docs | examples/src/restaurant_agent.ts | livekit-agents/livekit/agents/voice/agent_session.py | Workflows | LiveKit Docs
Here’s a simple way to hand control back to the orchestrator agent: • Keep a Reference to the Orchestrator Agent: Before passing control to other agents, save the orchestrator agent in the session’s
userdata
. This way, you won’t lose track of it. • Hand Control Back: Once the current agent finishes its job, you can use a tool to return the saved orchestrator agent. For example:
Copy code
@function_tool()
async def return_to_orchestrator(self, ctx):
    orchestrator = ctx.userdata.orchestrator_agent
    return "Returning to the orchestrator.", orchestrator
• Switch Agents Smoothly: If needed, use
session.update_agent()
to switch back to the orchestrator agent programmatically. This way, the orchestrator gets control again and can keep things running smoothly. If you want me to walk you through it or share more examples, just let me know!