faint-arm-51818
09/25/2025, 6:32 PMtall-airline-16756
09/25/2025, 6:32 PMtall-airline-16756
09/25/2025, 6:33 PMfaint-arm-51818
09/25/2025, 6:36 PMtall-airline-16756
09/25/2025, 6:37 PMentrypoint 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.pyfaint-arm-51818
09/25/2025, 6:42 PMtall-airline-16756
09/25/2025, 6:42 PMfunction_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.pyfaint-arm-51818
09/25/2025, 6:43 PMtall-airline-16756
09/25/2025, 6:44 PMself 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:
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 Docsfaint-arm-51818
09/25/2025, 6:45 PMtall-airline-16756
09/25/2025, 6:46 PMfunction_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:
@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:
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