red-television-3815
08/06/2025, 4:02 PMtall-airline-16756
08/06/2025, 4:02 PMtall-airline-16756
08/06/2025, 4:03 PMRunContext
as a parameter. This provides access to the current session and chat context.
from livekit.agents import function_tool, RunContext
@function_tool()
async def my_tool(context: RunContext, other_params: str) -> dict:
# Access chat context via context.chat_ctx
chat_context = context.chat_ctx
# Use the chat context as needed
return {"result": "success"}
2. Add the Tool to the Agent Dynamically:
After defining your agent, add the tool to its list of available tools using the update_tools()
method.
class MyAgent(Agent):
async def __init__(self):
super().__init__(instructions="My agent instructions")
# Add the tool to the agent's tools
await self.update_tools([my_tool])
3. Access Chat Context Within the Tool:
The RunContext
object passed to your tool includes the current chat context. You can access it via the context.chat_ctx
property.
@function_tool()
async def my_tool(context: RunContext, user_id: str) -> dict:
chat_context = context.chat_ctx
user_history = chat_context.items
# Access the conversation history or other chat context details
return {"user_history": user_history}
Let me know if you need further clarification or a more specific example!red-television-3815
08/06/2025, 4:14 PMtall-airline-16756
08/06/2025, 4:14 PMRunContext
object doesn’t have a chat_ctx
attribute directly. Instead, you can get to the chat context through the session
property of RunContext
. Here’s a quick fix for you:
@function_tool()
async def example_function(context: RunContext):
chat_context = context.session.chat_ctx
# Use chat_context as needed
This way, you’re grabbing the chat context the right way according to LiveKit’s setup. If you want me to walk you through anything else, just let me know—I’m happy to help!red-television-3815
08/06/2025, 4:39 PMtall-airline-16756
08/06/2025, 4:39 PM