is there a way to have a livekit connection prefer...
# ask-ai
n
is there a way to have a livekit connection prefer a specific worker?
t
Thanks for reaching out, rohit. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
It sounds like you want a client connection to “stick” to a specific worker. Directly preferring a specific worker isn’t supported; the server/cluster selects the node that hosts the room. Options to influence placement: • For regions: point clients to a regional signaling endpoint/load balancer so the node selector places rooms in that region (Distributed setup). • For Agents: use explicit dispatch to target a named agent/worker pool via
agent_name
or
room_config.agents
(Agent dispatch). If you can share whether you’re on LiveKit Cloud or self-hosted and what outcome you’re aiming for (region affinity, agent routing, etc.), I can outline exact steps. Sources: Distributed Setup | LiveKit docs | Deploying to custom environments | LiveKit Docs | Agent dispatch | LiveKit Docs | Voice Assistant Applications | livekit/livekit_composite | DeepWiki | Core Platform Architecture & Authentication | livekit/livekit_composite | D...
n
explain the agent side
t
Regarding “the agent side,” here’s how agent dispatch lets you influence which workers run your agent code: • What agents/workers are: Your agent code runs in “workers.” When a room needs an agent, the server dispatches a job to an available worker; the worker starts a job process that joins the room as a participant (Agents, Worker lifecycle, Job lifecycle). • How to target specific workers (explicit dispatch): ◦ Set
agent_name
on workers you want to use as a named pool. Workers with
agent_name
don’t receive automatic dispatch; they only accept explicit requests matching that name (Agent dispatch, Worker options). ◦ Trigger dispatch by: ▪︎ Adding
room_config.agents
to the access token so the server dispatches agents with that name when a participant connects (Authentication/token config). ▪︎ Calling the dispatch API (AgentDispatchService) to start a named agent for a room (Agent dispatch). ▪︎ For SIP, include agents in your SIP dispatch rule so the room spawns the desired agent on call-in (SIP dispatch rule). • Worker-side controls: ◦ Implement
request_fnc
to accept/reject each job (e.g., based on metadata or capacity). ◦ Use
load_fnc
and
load_threshold
to control when a worker is considered available (customizable in self-hosted; fixed in Cloud) (Worker options, Deploying to custom environments). Want a short example for: setting
agent_name
, adding
room_config.agents
to a token, or using the dispatch API? Sources: LiveKit Docs | Worker lifecycle | LiveKit Docs | Job lifecycle | LiveKit Docs | Node.js Agents Architecture | livekit/livekit_composite | DeepWiki | Worker options | LiveKit Docs
n
yeah
t
Got it—here’s the agent-side flow with quick examples. Name a worker (explicit pool): • Set
agent_name
so it only accepts explicit dispatches. • Docs: Agent dispatch, Worker options Example (Node/TS):
Copy code
Worker.run(myEntrypoint, {
  agent_name: "my-agent",
  // optional: request_fnc/load_fnc/load_threshold
});
Trigger dispatch via token (room_config.agents): • Add a named agent to the participant’s access token so it’s auto-dispatched on join. • Docs: Authentication Example (Node/TS):
Copy code
const at = new AccessToken(apiKey, apiSecret, { identity: userId });
at.addGrant({ roomJoin: true, room: roomName });
at.roomConfig = new RoomConfiguration({
  agents: [ new RoomAgentDispatch({ agentName: "my-agent", metadata: "any-json" }) ],
});
const token = await at.toJwt();
Trigger dispatch via API (server-side): • Use the dispatch service to start a named agent for a room. • Docs: Agent dispatch Worker-side acceptance (optional controls): • Accept/reject jobs and manage capacity. • Docs: Worker options Example:
Copy code
Worker.run(myEntrypoint, {
  agent_name: "my-agent",
  request_fnc: async (req) => {
    if (req.metadata?.tier === "gold") return req.accept();
    return req.reject();
  },
  // load_fnc/load_threshold can gate availability (customizable self-hosted)
});
Want this in Python instead, or an example of using the dispatch API call end-to-end? Sources: Worker lifecycle | LiveKit Docs | Job lifecycle | LiveKit Docs | Node.js Agents Architecture | livekit/livekit_composite | DeepWiki | Server-Side SDKs & Authentication | livekit/livekit_composite | DeepWiki | Authentication | LiveKit Docs