When using multiple agent, is there a way to store...
# ask-ai
n
When using multiple agent, is there a way to store which agent generated a message in the chat history?
t
Thanks for reaching out, Stephen. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks for reaching out! To help you better, could you please let me know which system or platform you're using for chat history and agent management? For instance, are you talking about LiveKit's features or maybe integrating with another third-party system? I'm here to assist you every step of the way!
n
Just using LiveKit's python SDLK
t
To help you store which agent generated a message in the chat history using LiveKit's Python SDK, here are some friendly strategies you can try: • Embed Agent Identity in Chat Context: You can add a system message whenever an agent joins the session to identify them. This way, their identity becomes part of the chat history. • Preserve Agent Context During Transfers: When transferring chats between agents, make sure to keep the previous chat history intact, including those helpful system messages. • Track Active Agent via Room Attributes: Updating room participant attributes to show the current agent is a great way to keep track in real time. • Access Full Chat History: You can use
session.history
to look through the entire conversation history, which includes messages from all agents involved. • Use Event Listeners: Listening for the
conversation_item_added
event lets you capture messages as they come in and tag them with agent details right away. These approaches will help make sure it's always clear who said what in your chat history. If you'd like, I’m here to provide more details or examples to make implementation easier for you. Just let me know! Sources: complex-agents/personal_shopper/personal_shopper.py | examples/voice_agents/realtime_load_chat_history.py | Session recording and transcripts | LiveKit Docs | Events and error handling | LiveKit Docs
n
Tell me more about using room participant attributes. Is this also accessible via the session on
conversation_item_added
?
t
Using room participant attributes in LiveKit's Python SDK is a wonderful way to keep track of important details about participants, like their role or language preferences. Let me walk you through how you can smoothly integrate this with the
conversation_item_added
event: • Set and Update Attributes: You can easily use
set_attributes()
on a
LocalParticipant
instance to set key-value pairs. For example:
Copy code
await room.local_participant.set_attributes({"user_language": "es", "user_role": "agent"})
• Access Attributes in `conversation_item_added`: Whenever a new message comes in, you can access the participant’s attributes through
item.participant.attributes
. Here’s how:
Copy code
@session.on("conversation_item_added")
async def on_conversation_item_added(item):
    participant = item.participant
    attrs = participant.attributes
    logger.info("Current attributes: %s", attrs)
• Listen to Attribute Changes: You can also listen for the
participant_attributes_changed
event to stay updated on any changes in real time:
Copy code
@room.on("participant_attributes_changed")
def on_participant_attributes_changed(changed_attributes, participant):
    logger.info("Attributes changed for %s: %s", participant.identity, changed_attributes)
These attributes sync across all participants, making it super handy to customize responses or keep track of agent roles. If you have any questions or want me to walk you through more examples, just let me know—I’m here to help! Sources: Participant attributes and metadata | LiveKit Docs | livekit-rtc/livekit/rtc/_proto/participant_pb2.pyi | Text and transcriptions | LiveKit Docs | basics/label_messages.py | SIP participant | LiveKit Docs
n
how do I access the room from inside an agent?
t
To access the room from inside an agent in LiveKit's Python SDK, you can use the
JobContext
object provided to the agent's entrypoint function. Here's how: • Direct Access: The room is accessible via
ctx.room
. However, some properties become available only after calling
ctx.connect()
. For example:
Copy code
async def entrypoint(ctx: JobContext):
    await ctx.connect()
    room = ctx.room
    print(f"Connected to room: {room.name}")
• Using UserData: You can pass the
JobContext
to a
UserData
class to keep access consistent across your agent's functions:
Copy code
@dataclass
class MyUserData:
    ctx: JobContext

# Access the room through UserData
room = userdata.ctx.room
• Participant Management: Once connected, you can interact with participants like this:
Copy code
# Wait for a specific participant
participant = await ctx.wait_for_participant(identity="user123")

# Access existing participants
for remote_participant in ctx.room.remote_participants.values():
    print(f"Participant: {remote_participant.identity}")
If you'd like, I'm here to help with more details or examples tailored to your specific needs—just let me know! Sources: rpc/rpc_agent.py | Connecting to LiveKit | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_session.py
n
I couldn't find the participant on the ChatMessage passed into the
conversation_item_added
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!