How to ensure that multiple subagents in workflow ...
# ask-ai
f
How to ensure that multiple subagents in workflow share common attributes and methods (use the same listeners, tts, llm, stt, etc.)?
t
Thanks for reaching out, Taras. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks so much for reaching out! Could you please share a bit more about your workflow setup and the specific tools or frameworks you're using? For instance, are you working with a particular LiveKit SDK or integrating with external services for TTS, LLM, and STT? Understanding this will really help me support you better.
f
I want to use two agent to perform different tasks in my workflow (validation and questioning). I wan to define the STT, LLM, TTS, and on_enter listener in one pace to be used by both agents (but not in entrypoint function).
t
In LiveKit workflows, shared configurations for STT, LLM, TTS, and listeners are usually set up within the
entrypoint
function when you initialize the
AgentSession
. This setup helps ensure that the core services can be easily inherited or customized by individual agents as needed. While it’s true that individual agents can override these services in their
__init__
method, the initial shared configuration is always established in the
entrypoint
. Also, the
on_enter
listener is designed as an async method within each agent class, so it isn’t configured globally outside the
entrypoint
. If you’re thinking about avoiding the
entrypoint
function for a particular reason, I’d love to hear more about your situation. That way, I can help you explore some alternative approaches that fit within the standard framework and best support your needs. Sources: examples/voice_agents/restaurant_agent.py | pipeline-tts/tts_comparison.py | main.py
f
can I define function_tool separately from agent?
t
Absolutely! You can definitely define
function_tool
separately from an agent in LiveKit workflows. This is a great way to reuse tools across different agents or even create them on the fly based on what you need at runtime. Here are some friendly tips to get you started: • Define tools outside agent classes: Try defining a tool as a standalone function and then pass it to multiple agents when you initialize them. It’s a neat way to keep your code clean and easy to manage. • Create tools programmatically: You can use
function_tool
as a function to build tools dynamically. For example, you might create specific tools from a generic function or load them from external sources—super flexible! • Use raw JSON schemas: For more advanced scenarios, you can define tools directly using a raw JSON schema. This is especially handy if you’re integrating with external function definitions. • Update tools dynamically: Need to change things up during a session? No problem! You can add, remove, or replace tools at runtime using the
agent.update_tools()
method. This lets you adapt based on the session context or what the user needs. If you’d like, I’m here to help with examples or to dive deeper into any of these options—just let me know! Sources: Tool definition and use | LiveKit Docs | Tool definition and use | LiveKit Docs | examples/voice_agents/raw_function_description.py
f
how can I retain access to self in this function?
t
To help you keep access to
self
in a
function_tool
, a great approach is to define the tool as a method inside your
Agent
class. This way, the tool can easily work with instance variables and methods through
self
. Here’s a simple example to guide you:
Copy code
from livekit.agents import Agent, RunContext, function_tool

class MyAgent(Agent):
    def __init__(self):
        super().__init__(instructions="You are a helpful assistant.")
        self.light_on = False  # Instance variable accessible via self

    @function_tool()
    async def turn_on_light(self, context: RunContext):
        """Turn on the light and record the state."""
        self.light_on = True  # Accessing instance variable via self
        return "Light is now on"

    @function_tool()
    async def turn_off_light(self, context: RunContext):
        """Turn off the light and record the state."""
        self.light_on = False  # Accessing instance variable via self
        return "Light is now off"
With this setup, the
turn_on_light
and
turn_off_light
methods update the agent’s internal state using
self
. Because they’re decorated with
@function_tool
and defined inside the
Agent
class, they’re automatically registered as tools. If anything isn’t clear or you’d like me to walk you through more examples, please don’t hesitate to ask—I’m here to help! Sources: examples/voice_agents/realtime_joke_teller.py | Agents 0.x migration guide | LiveKit Docs
f
what if function_tool outside of the Agent class definition?
t
I understand it can be a bit tricky when a
function_tool
is defined outside of the
Agent
class and you want to access
self
. Since the function isn’t bound to an instance, you can’t access
self
directly. But don’t worry! You can use the
RunContext
parameter to get to the current agent instance indirectly. Here’s a friendly example to guide you:
Copy code
@function_tool()
async def example_tool(context: RunContext) -> str:
    # Access the current agent instance
    agent = context.session.current_agent
    # Use `agent` as a proxy for `self`
    return f"Current agent is: {agent.__class__.__name__}"
This way, your shared tools can still interact with agent-specific state and methods through
context.session.current_agent
, which is pretty handy. If you want your tool to be even more closely tied to the agent instance, a great approach is to create tools dynamically inside the agent’s
__init__
method. This lets the tool capture
self
naturally via closure:
Copy code
class MyAgent(Agent):
    def create_shared_tool(self):
        async def tool_with_self_access(context: RunContext, value: str):
            self.process_value(value)  # Direct access to agent method
            return f"Processed {value} using agent logic"

        return function_tool(tool_with_self_access, name="shared_tool")

    def __init__(self):
        super().__init__(
            tools=[self.create_shared_tool()],
            instructions="Agent with self-aware tool."
        )

    def process_value(self, value: str):
        # Agent-specific logic
        pass
Please don’t hesitate to reach out if you’d like me to walk you through this further or provide more examples—I’m here to help! Sources: Tool definition and use | LiveKit Docs | Workflows | LiveKit Docs