faint-arm-51818
09/03/2025, 4:31 PMtall-airline-16756
09/03/2025, 4:31 PMtall-airline-16756
09/03/2025, 4:32 PMmetadata field when you dispatch a job. Then, access it in your agent code using ctx.job.metadata.
• Room Metadata and Participant Attributes: Another option is to store your custom data in room metadata or participant attributes, which you can get to via ctx.room.metadata or participant.attributes.
• Custom Diagnostic Fields: If you want to enrich your logs with extra context for monitoring or debugging, use ctx.log_context_fields.
• Process-Scoped State: For things like database connections or tracking processing stages within the job process, you can store state in ctx.proc.userdata.
If you’d like, I’m happy to walk you through detailed examples or help you get started with any of these methods—just give me a shout!
Sources: Agent dispatch | LiveKit Docs | SIP dispatch rule | LiveKit Docs | examples/voice_agents/realtime_load_chat_history.py | External data and RAG | LiveKit Docs | Job lifecycle | LiveKit Docsfaint-arm-51818
09/03/2025, 4:35 PMtall-airline-16756
09/03/2025, 4:36 PMsip.trunkPhoneNumber and sip.phoneNumber attributes inside the Agent class by using the JobContext and AgentSession. Here’s a simple way to do it:
1. Wait for the Participant: Use ctx.wait_for_participant() to patiently wait for the SIP participant to join.
2. Check Participant Kind: Make sure the participant’s kind is rtc.ParticipantKind.PARTICIPANT_KIND_SIP.
3. Access Attributes: Grab the sip.trunkPhoneNumber and sip.phoneNumber from the participant’s attributes dictionary using the right keys.
Here’s a quick example to guide you:
async def on_participant_connected(session: AgentSession, participant: rtc.RemoteParticipant):
if participant.kind == rtc.ParticipantKind.PARTICIPANT_KIND_SIP:
trunk_phone = participant.attributes.get("sip.trunkPhoneNumber", "Unknown")
caller_phone = participant.attributes.get("sip.phoneNumber", "Unknown")
print(f"SIP Trunk Phone Number: {trunk_phone}")
print(f"SIP Caller Phone Number: {caller_phone}")
Just a heads up—these attributes are only available for SIP participants. If you have any questions or need a hand with anything else, feel free to ask. I’m here to help!
Sources: livekit-agents/livekit/agents/job.py | SIP participant | LiveKit Docsfaint-arm-51818
09/03/2025, 4:38 PMtall-airline-16756
09/03/2025, 4:39 PMon_participant_connected method is something you’d want to define inside your custom Agent class. It’s super handy for handling what happens when someone joins the room. You can hook this method up as an event handler for the participant_connected event in the AgentSession.
Here’s a quick example to make it clearer:
class MySIPAgent(Agent):
async def on_participant_connected(self, session: AgentSession, participant: rtc.RemoteParticipant):
if participant.kind == rtc.ParticipantKind.PARTICIPANT_KIND_SIP:
trunk_phone = participant.attributes.get("sip.trunkPhoneNumber", "Unknown")
caller_phone = participant.attributes.get("sip.phoneNumber", "Unknown")
print(f"SIP Trunk Phone Number: {trunk_phone}")
print(f"SIP Caller Phone Number: {caller_phone}")
async def entrypoint(ctx: JobContext):
session = AgentSession()
agent = MySIPAgent()
# Register the event handler
session.on("participant_connected", lambda p: agent.on_participant_connected(session, p))
# Start the session
await session.start(agent=agent, room=ctx.room)
If anything’s unclear or you want me to walk you through more details, just let me know—I’m here to help!faint-arm-51818
09/03/2025, 4:45 PMtall-airline-16756
09/03/2025, 4:47 PMsip.trunkPhoneNumber and sip.phoneNumber inside ctx.room.metadata, you'll need to update the room metadata manually using the server API. Don’t worry, I’ll walk you through it:
1. Access SIP Participant Attributes: Grab the sip.trunkPhoneNumber and sip.phoneNumber from participant.attributes when the participant connects.
2. Update Room Metadata: Use the UpdateRoomMetadata API to save these attributes into ctx.room.metadata. Just make sure to serialize the metadata as a string (like JSON) before saving.
Here’s a quick example in Python to help you out:
async def on_participant_connected(participant):\n if participant.kind == \"PARTICIPANT_KIND_SIP\":\n trunk_phone = participant.attributes.get(\"sip.trunkPhoneNumber\", \"\")\n caller_phone = participant.attributes.get(\"sip.phoneNumber\", \"\")\n\n new_metadata = {\n \"latest_sip_caller\": {\n \"caller_number\": caller_phone,\n \"trunk_number\": trunk_phone\n }\n }\n\n await api.RoomServiceClient().update_room_metadata(\n room=participant.room.name,\n metadata=str(new_metadata)\n )
Just a heads-up: double-check that your API key has the right permissions to update the room metadata. If you run into any bumps or have questions, I’m here to help!
Sources: SIP APIs | LiveKit Docs | livekit-protocol/src/livekit.rs | SIP participant | LiveKit Docs | telephony/sip_lifecycle.py | Room metadata | LiveKit Docs