Tell me how to retrieve identity in the agent code...
# ask-ai
b
Tell me how to retrieve identity in the agent code. The identity is inserted when creating a token by the following code.
Copy code
async def create_token_with_agent_dispatch() -> str:
    token = (
        api.AccessToken()
        .with_identity("my_participant")
        .with_grants(api.VideoGrants(room_join=True, room=room_name))
        .with_room_config(
            api.RoomConfiguration(
                agents=[api.RoomAgentDispatch(agent_name="test-agent", metadata="my_metadata")],
            ),
        )
        .to_jwt()
    )
    return token
✅ 1
t
Thanks for reaching out, toshi. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — you're asking how to read the participant identity (the "my_participant" you set on the token) from inside your agent code. You can get that identity from the RemoteParticipant objects exposed on the agent's room context. Here are concise ways to access it inside your agent entrypoint (assuming an async agent using
JobContext
): 1) Wait for participants to join (async iterator)
Copy code
@server.rtc_session(agent_name="my-agent")
async def my_agent(ctx: JobContext):
    async for participant in ctx.room.remote_participants:
        # participant.identity will be "my_participant"
        print(f"Remote participant identity: {participant.identity}")
        break
2) Read the current remote participants mapping (if someone already joined)
Copy code
@server.rtc_session(agent_name="my-agent")
async def my_agent(ctx: JobContext):
    # get first remote participant
    remote = next(iter(ctx.room.remote_participants.values()))
    print(f"Remote participant identity: {remote.identity}")  # "my_participant"
Notes: • The identity on the token is the remote user’s identity (not the agent’s identity). Use
participant.identity
on a
RemoteParticipant
to read it. • Use the async iterator if you need to wait for a participant to join; use the
.values()
mapping if participants are already present. If you want, tell me whether your agent needs to: • Read the identity for the first participant only, or for every participant, and • React immediately when someone joins or just check current room members. I can then provide a tailored snippet showing event handling, logging, or access-control usage. Sources: Server options | LiveKit Documentation | Job lifecycle | LiveKit Documentation, with interactive elements