can I add custom data to the JobContext context?
# ask-ai
f
can I add custom data to the JobContext context?
t
Thanks for reaching out, Taras. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Absolutely, you can add custom data to the `JobContext`—just not by changing the object directly. Instead, there are a few handy ways to pass and access your custom data smoothly: • Job Metadata: You can pass structured data (like JSON) through the
metadata
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 Docs
f
how I can get participant sip.trunkPhoneNumber and sip.phoneNumber inside the Agent class?
t
Hey there! You can easily access the
sip.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:
Copy code
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 Docs
f
where do I define on_participant_connected?
t
Hey there! The
on_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:
Copy code
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!
f
how I can save participant sip.trunkPhoneNumber and sip.phoneNumber inside ctx.room.metadata?
t
Hey there! To save a participant's
sip.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:
Copy code
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