From where is data passed into JobContext, when Jo...
# ask-ai
w
From where is data passed into JobContext, when JobContext is passed into the entrypoint function?
t
Thanks for reaching out, Arthur. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! When a job gets dispatched, data is sent over to
JobContext
through something called job metadata. You can easily access this metadata right inside the
entrypoint
function using
ctx.job.metadata
. Here’s a quick example to make it clearer:
Copy code
async def entrypoint(ctx: JobContext):
    metadata = json.loads(ctx.job.metadata)
    user_id = metadata["user_id"]
    user_name = metadata["user_name"]
    user_phone = metadata["user_phone"]
    # Use the data as needed
Usually, this metadata comes as a JSON string, which you can parse to grab whatever info you need. This way, you can include any job-specific or user-specific details when you dispatch the agent. Pretty handy, right?
w
Can I limit the number of agents to join a room to just 1?
t
Hey there! Just checking—are you hoping to limit the room to just one agent altogether, or would you like to have multiple participants but keep the number of agents capped? Understanding this will help me guide you to the best solution for what you need.
w
I want to limit the room to just one agent altogether
t
If you want to limit a room to just one agent, here’s a straightforward way to do it using explicit agent dispatch and some room settings: 1. Set the
agent_name
in Worker Options:
When you register your agent, make sure to set the
agent_name
in the
WorkerOptions
. This will turn off automatic dispatch and give you full control over which agent gets assigned.
Copy code
opts = WorkerOptions(
    ..., 
    agent_name="your-agent-name",
)
2. Create an Explicit Dispatch: Next, use the
AgentDispatchService
to send your agent to the specific room you want.
Copy code
lkapi = LiveKitAPI()
dispatch = await lkapi.agent_dispatch.create_dispatch(
    CreateAgentDispatchRequest(
        agent_name="your-agent-name",
        room="your-room-name",
    )
)
3. Configure Room Settings: When you create the room, include the agent in the room configuration through the access token.
Copy code
at = AccessToken(...).with_room_config(
    RoomConfiguration(
        agents=[
            RoomAgentDispatch(
                agent_name="your-agent-name",
            ),
        ],
    )
)
This way, only the agent you specify will be assigned to the room. If you want me to walk you through any of these steps or if something isn’t clear, just let me know—I’m happy to help! Sources: Dispatching agents | LiveKit Docs | Agent dispatch | LiveKit Docs