Where is RunContext used with agents?
# ask-ai
w
Where is RunContext used with agents?
t
Thanks for reaching out, Arthur. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
RunContext
is a handy part of LiveKit agents that helps provide important context and manage sessions smoothly. Here’s a quick rundown of what it does: • Accessing Session Data: It lets you peek into the current session, speech handle, function calls, and user data. For instance, in the
lookup_weather
function, it’s used to grab and update user data. • User Data Management: It keeps track of specific user data structures, which helps maintain the state across different interactions—just like in the restaurant agent example. • Function Implementation: It’s the backbone for tool functions that perform tasks like updating user info, handing off to other agents, and managing session states. • Handoff Workflows: It makes transferring users between agents easy by returning an
Agent
instance and a message, so everything flows smoothly. If you want to dive deeper, feel free to check out the LiveKit Documentation. It’s a great resource!
w
What attributes does a RunContext object have?
t
Hey there! Just wanted to share a quick rundown of the
RunContext
object in LiveKit and what it offers: • `session`: This gives you access to the current
AgentSession
, so you can see all the details about what's happening right now. • `speech_handle`: This is your go-to for anything speech-related, like managing audio playback or handling interruptions. • `function_call`: Here you'll find the
FunctionCall
object tied to the tool function that's running. • `userdata`: This stores user-specific info linked to the session, which you can get through
session.userdata
. On top of that, there are some handy methods to interact with: • `disallow_interruptions()`: Use this if you want to block any interruptions during the current function call. • `wait_for_playout()`: This one waits for the speech playback to finish for the current step in the function call. Sources: livekit-agents/livekit/agents/telemetry/trace_types.py
w
difference between chat context and runcontext
t
Let’s break down the difference between
ChatContext
and
RunContext
in LiveKit, so it’s easy to understand: • Purpose:
ChatContext
keeps track of the conversation history — that means messages, function calls, and their results — helping the chat flow smoothly and make sense. ◦
RunContext
is all about the behind-the-scenes details during a function’s execution, like session info, function calls, and user data. • Components:
ChatContext
includes handy classes like
ChatMessage
,
FunctionCall
, and
FunctionCallOutput
to organize everything that’s happened in the chat. ◦
RunContext
gives you access to the current session, user-specific info, and details about the runtime environment. • Functionality:
ChatContext
helps maintain the conversation history and lets you add messages or function calls to keep interactions clear and connected. ◦
RunContext
comes into play during function execution, providing the data needed to make those operations context-aware. • Usage:
ChatContext
is your go-to when chatting with the LLM to manage the state of the conversation. ◦
RunContext
is used inside function tools to tap into session and runtime details. So, to sum it up:
ChatContext
is all about keeping the conversation flowing nicely, while
RunContext
focuses on what’s happening during function execution behind the scenes.
w
what is the userdata attribute in RunContext
t
Hey there! The
userdata
attribute in
RunContext
is a handy way to store and manage custom, session-specific data within a LiveKit agent. Here’s a quick rundown: • Purpose: It keeps track of application-specific info relevant to the current session, like user inputs, preferences, or any intermediate results. • Access: You can get to it via
RunContext.userdata
, and its type is set as a generic parameter when you initialize it. • Initialization: It gets initialized when you create an
AgentSession
, with proper type hints to keep things clear. For example:`session = AgentSession[MySessionInfo]( userdata=MySessionInfo(), # ... other initializations )` • Usage: It’s great for passing state between function tools and event handlers. Any changes you make to
userdata
will stick around for the whole session. • *Example:*`@dataclass class MySessionInfo: user_name: str | None = None age: int | None = None RunContext_T = RunContext[MySessionInfo] @function_tool() async def record_name( name: Annotated[str, Field(description="The customer's name")], context: RunContext_T, ) -> str: userdata = context.userdata userdata.user_name = name return f"The name is updated to {name}"` If you want, I’m here to help you implement
userdata
in your own setup—just let me know!