Is it possible to dynamically pass variables to an...
# ask-ai
a
Is it possible to dynamically pass variables to an agent depending on the room it is in, or the user interacting with it? These variables should be accessible in agent.py.
t
Thanks for reaching out, Guid. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Absolutely, you can dynamically pass variables to an agent depending on the room or user interacting with it. You’ll find these variables accessible in
agent.py
using the following helpful methods: • Using Metadata in Agent Dispatch: You can pass variables explicitly during agent dispatch by attaching metadata. The agent then accesses these variables via
ctx.job.metadata
. Here’s a simple example:
Copy code
async def entrypoint(ctx: JobContext):
    metadata = json.loads(ctx.job.metadata)
    user_id = metadata["user_id"]
    user_name = metadata["user_name"]
• Dispatch via Participant Connection: Another way is to embed agent dispatch configuration in the access token when a user joins a room. This lets you have dynamic dispatch per user with metadata. • Access Room or Participant Data: Agents can also dynamically pull in contextual data like the room name (
ctx.room.name
) or participant attributes using
ctx.wait_for_participant()
. If you’d like, I’m here to provide more details or examples on any of these approaches—just let me know! Sources: Agent dispatch | LiveKit Docs | Job lifecycle | LiveKit Docs | Authentication | LiveKit docs | SIP Integration & Telephony | livekit/livekit_composite | DeepWiki | livekit-plugins/livekit-plugins-bey/livekit/plugins/bey/avatar.py