bitter-cpu-44145
11/06/2025, 12:05 PMtall-airline-16756
11/06/2025, 12:05 PMtall-airline-16756
11/06/2025, 12:06 PMAgentDispatchClient from the LiveKit SDK to programmatically send an agent to a room. Here’s a simple example to help you get started:
import { AgentDispatchClient } from 'livekit-server-sdk';
const agentDispatchClient = new AgentDispatchClient(
process.env.LIVEKIT_URL,
process.env.LIVEKIT_API_KEY,
process.env.LIVEKIT_API_SECRET
);
// Dispatch agent to room
const dispatch = await agentDispatchClient.createDispatch(
'interview-room', // room name
'interview-agent', // agent name (must match agent's WorkerOptions.agentName)
{
metadata: JSON.stringify({
agentRole: 'interviewer',
interviewId: '12345',
}),
}
);
console.log('Agent dispatched:', dispatch); This method is great because it lets you decide exactly when to trigger the agent to join.
2. Dispatch on Participant Connection (via Token): If you prefer, you can embed the agent dispatch setup directly into the participant’s access token. That way, the agent is dispatched automatically when the participant joins. Here’s how you can do it:
import { AccessToken } from 'livekit-server-sdk';
import { RoomConfiguration, RoomAgentDispatch } from '@livekit/protocol';
const at = new AccessToken();
at.identity = 'candidate-123';
at.addGrant({ roomJoin: true, room: 'interview-room' });
// Configure agent dispatch in room config
at.roomConfig = new RoomConfiguration({
agents: [
new RoomAgentDispatch({
agentName: 'interview-agent',
metadata: JSON.stringify({
role: 'technical interviewer',
experienceLevel: 'senior',
}),
}),
],
});
const token = await at.toJwt();
console.log('Participant token:', token); This way, the agent joins seamlessly as soon as the participant connects with this token.
Just a quick tip: make sure your agent is registered with the correct agentName in its WorkerOptions so explicit dispatch works smoothly. If you'd like to see a full working example, feel free to check out the LiveKit Node SDK agent dispatch example. I hope this helps you move forward confidently!
Sources: Job lifecycle | LiveKit Docs | Agent dispatch | LiveKit Docs | packages/livekit-server-sdk/src/AgentDispatchClient.tsbitter-cpu-44145
11/06/2025, 12:12 PMtall-airline-16756
11/06/2025, 12:12 PMRoomConfiguration isn’t included, or are you curious about how the system manages agent dispatch without any particular configuration? I’m here to help clarify this for you!bitter-cpu-44145
11/06/2025, 12:16 PMtall-airline-16756
11/06/2025, 12:16 PMbitter-cpu-44145
11/06/2025, 12:17 PMtall-airline-16756
11/06/2025, 12:18 PMbitter-cpu-44145
11/06/2025, 12:29 PMtall-airline-16756
11/06/2025, 12:31 PMbitter-cpu-44145
11/06/2025, 12:53 PMtall-airline-16756
11/06/2025, 12:54 PMprewarm_fnc in your WorkerOptions to initialize necessary resources, like loading models.
• Keep an eye on your logs for signs of prewarm activity or notice if initialization times get shorter when jobs begin.
• Check out the /worker health endpoint (available via WorkerOptions) to see active job counts and how many idle processes are ready to go.
2. Configure Prewarm Processes
You can set the num_idle_processes parameter in WorkerOptions to keep a pool of prewarmed processes available:
def prewarm(proc: JobProcess):
proc.userdata["vad"] = silero.VAD.load() # Example: preloading VAD model
if __name__ == "__main__":
cli.run_app(
WorkerOptions(
entrypoint_fnc=entrypoint,
prewarm_fnc=prewarm,
num_idle_processes=10 # Adjust based on load
)
)
For handling 10–20 sessions per minute (about 1 session every 3–6 seconds), a good starting point is num_idle_processes=5–10. You can then tweak this number based on how things perform.
3. Monitor and Optimize
• Make sure your LiveKit Cloud plan supports enough concurrent sessions and agent session minutes for your needs.
• Keep an eye on CPU usage, session concurrency, and how quickly agents start up to fine-tune your num_idle_processes setting.
• Consider using the load_fnc and load_threshold parameters to automatically adjust idle processes depending on system load.
If you have any questions or need a hand with implementation or monitoring, please don’t hesitate to reach out—I’m here to help!
Sources: examples/other/transcription/multi-user-transcriber.py | Quotas and limits | LiveKit docs | LiveKit Pricing | Worker options | LiveKit Docsbitter-cpu-44145
11/06/2025, 1:01 PMcli.runApp(new WorkerOptions({ agent: fileURLToPath(import.meta.url), production: true, numIdleProcesses: 10 }));
When I run agent locally, I see logs from prewarm for N processes but when I dispatch agent by creating AccessToken anyway it creates one more process and waiting for prewarmtall-airline-16756
11/06/2025, 1:02 PMprewarm function whenever it dispatches an agent. This happens because the prewarm function is meant to initialize resources within each isolated job process. Even if you have numIdleProcesses set, it only controls how many idle worker processes stay warm at the worker level, not at the job level.
When you dispatch an agent using an AccessToken, LiveKit assigns the job to a process—this could be a new one or one from the idle pool. The prewarm function then runs inside that specific job’s process to get everything ready, like loading models or AI engines needed for the task.
To help things run more efficiently, try to keep your prewarm function as streamlined as possible, loading only the essential resources for the job. If you find delays are still happening, you might want to increase numIdleProcesses so more prewarmed processes are ready to go, which can really reduce the need to create new ones on the fly.
Please feel free to reach out if you’d like any more tips or help optimizing your setup—I’m here to support you!
Sources: Worker options | LiveKit Docs | Node.js Agents Architecture | livekit/livekit_composite | DeepWiki | examples/src/gemini_realtime_agent.tsbitter-cpu-44145
11/06/2025, 1:57 PMtall-airline-16756
11/06/2025, 1:59 PMnumIdleProcesses set to 5 and just 1 agent assigned, LiveKit might create a new process if the warmed process queue happens to be temporarily empty. This can occur because of a few reasons:
• Initialization delays: Sometimes, processes aren’t fully initialized and added to the warmed queue just yet.
• Readiness event: The idle pool might not have reached its target count if the readiness event (idle_ready) hasn’t triggered.
• Immediate demand: If there aren’t any warmed processes available right when a job comes in, the system will spin up a new process to handle it.
A good way to handle this is to make sure your prewarm function runs efficiently and that the system has enough time to get the idle pool ready before dispatching jobs. It also helps to keep an eye on the warmed process queue to confirm it’s maintaining the expected number of idle processes.
Please feel free to reach out if you’d like any help debugging or optimizing this — I’m here to support you!
Sources: livekit-agents/livekit/agents/ipc/proc_pool.py