How can I alter an Agent participant info and add ...
# ask-ai
p
How can I alter an Agent participant info and add metadata to it? I want to be able to distinguish certain agents in my room by adding a metadata to its participant info
t
Thanks for reaching out, Kaique. 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 add metadata to an Agent participant in LiveKit using either the client SDKs or server-side APIs. Here’s a quick rundown of your options: 1. Using Client SDKs: • To set metadata:
room.localParticipant.setMetadata(JSON.stringify({ key: 'value' }));
• To set attributes:
room.localParticipant.setAttributes({ key1: 'value1', key2: 'value2' });
2. Using Server APIs: • Use the
UpdateParticipant
API to update metadata:
Copy code
data, err := json.Marshal(values)
_, err = c.UpdateParticipant(context.Background(), &livekit.UpdateParticipantRequest{
  Room:     roomName,
  Identity: identity,
  Metadata: string(data),
})
3. Initial Metadata via Access Token: You can set initial metadata right when you generate the participant’s access token, so those values are ready as soon as they connect. Just a heads up: make sure the participant’s access token includes the
canUpdateOwnMetadata
permission. Also, remember that metadata and attributes have a 64 KiB size limit. If you have any questions or want a hand with this, just let me know—I’m here to help! Sources: lib/src/participant/local.dart | livekit-rtc/livekit/rtc/participant.py | Runtime/Scripts/Participant.cs | Participant attributes and metadata | LiveKit Docs | Sources/LiveKit/Participant/LocalParticipant.swift
p
But I want to add the metadata on the agent dispatch, from the agents framework
t
Hey there! If you want to add metadata to an agent participant during dispatch in the LiveKit Agents framework, here are a couple of easy ways to do it: 1. Dispatch via API (Explicit Dispatch): You can dispatch an agent to a room programmatically and include metadata like this:
Copy code
# Python Example
await lkapi.agent_dispatch.create_dispatch(
    api.CreateAgentDispatchRequest(
        agent_name="test-agent",
        room="my-room",
        metadata='{"userId": "123", "name": "John"}'
    )
)
Copy code
// Node.js Example
const dispatch = await agentDispatchClient.createDispatch(roomName, agentName, {
  metadata: '{"userId": "123", "name": "John"}',
});
2. Dispatch on Participant Connection (Token-based Dispatch): If you’re generating a participant access token, you can set up the room to automatically dispatch an agent with metadata like this:
Copy code
# Python Example
AccessToken()
.with_room_config(
    RoomConfiguration(
        agents=[RoomAgentDispatch(agent_name="test-agent", metadata='{"context": "support_call"}')]
    ),
)
Copy code
// Node.js Example
at.roomConfig = new RoomConfiguration({
  agents: [
    new RoomAgentDispatch({
      agentName: agentName,
      metadata: '{"context": "user_support"}',
    }),
  ],
});
Accessing Metadata in the Agent: Once your agent is dispatched, you can easily grab the metadata from the agent’s
JobContext
like this:
Copy code
async def entrypoint(ctx: JobContext):
    logger.info(f"job metadata: {ctx.job.metadata}")
If anything’s unclear or you want to dive deeper, just let me know—I’m here to help! Sources: Dispatching agents | LiveKit Docs | livekit-protocol/livekit/protocol/agent_dispatch.pyi | examples/agent-dispatch/index.ts | Agent dispatch | LiveKit Docs
p
When the agent is dispatched to a room, it works as a participant, I want to add metadata to the participant representation of the Dispatched agent in the room
t
Hey there! If you want to add metadata to the participant representation of a dispatched agent in the room, you can easily update the participant's metadata after the agent has joined. This is a bit different from the metadata you set during dispatch. Updating Participant Metadata: Just use the
UpdateParticipant
API to tweak the participant's metadata like this:
Copy code
# Example in Go
data, err := json.Marshal(values)
_, err = c.UpdateParticipant(context.Background(), &livekit.UpdateParticipantRequest{
  Room:     roomName,
  Identity: identity,
  Metadata: string(data),
})
This way, you can set or update the metadata for the dispatched agent’s participant representation in the room. If you have any questions or want me to walk you through it, just let me know—I’m here to help! Sources: Dispatching agents | LiveKit Docs | protobufs/livekit_agent_dispatch.proto | Managing participants | LiveKit Docs | livekit-rtc/livekit/rtc/participant.py | Dispatching agents | LiveKit Docs