can I dispatch an agent with attributes? If not, h...
# ask-ai
c
can I dispatch an agent with attributes? If not, how would I dispatch an agent with a custom identifier that is immediately delivered to other participants upon connecting to the room?
t
Thanks for reaching out, Nathan. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! You can easily dispatch an agent with attributes or a custom identifier in LiveKit using these methods: • Explicit Agent Dispatch: Just use the API to dispatch an agent by specifying the
agent_name
and adding some custom metadata. This metadata gets passed along to the agent as part of the job context. • Dispatch on Participant Connection: You can set up a participant’s access token to automatically dispatch agents when they connect. This means specifying the agent name and metadata right in the
RoomConfiguration
. Both ways let you include custom attributes or identifiers in the metadata, which will be shared with other participants when they join. If you want, I’m happy to share more details or examples to help you get it set up! Sources: Dispatching agents | LiveKit Docs | packages/livekit-server-sdk/src/AgentDispatchClient.ts | livekit-protocol/livekit/protocol/agent_dispatch.py
c
I am doing explicit agent dispatch by specifying
agent_name
, but when this agent joins the room, I want other participants who are listening to the
participant_connected
event to immediately know of some custom attributes for this agent
t
Hey there! To make sure everyone listening to the
participant_connected
event instantly knows the custom attributes for an agent, you can add these attributes right into the agent's metadata during the dispatch process. This way, when the agent connects, everyone else can easily access that info. Here’s a simple way to do it: • Include Custom Metadata: When you dispatch the agent, just pop the custom attributes into the
metadata
field. Like this:
Copy code
const dispatch = await agentDispatchClient.createDispatch(
  roomName,
  agentName,
  { metadata: '{"custom_key": "custom_value"}' }
);
Access Metadata in `participant_connected`: Once the agent joins the room, their metadata comes along with the participant object. Other participants can grab it through the
metadata
property in the event handler.
Copy code
room.on('participantConnected', (participant) => {
  const customAttributes = JSON.parse(participant.metadata);
  console.log(customAttributes.custom_key); // Outputs "custom_value"
});
Doing it this way means everyone immediately knows the custom attributes as soon as the agent connects. If you want me to walk you through anything else, just give me a shout!