I have to handle multiple agent in the same projec...
# ask-ai
q
I have to handle multiple agent in the same project. I want to understand, how can a single worker handle multiple agents?
t
Thanks for reaching out, Harsh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking how a single LiveKit worker can run multiple agents in the same project — here’s a concise explanation and what to consider. How it works • A worker runs multiple agent sessions concurrently; each session is an independent conversation/job tied to a user or room. • Sessions are isolated (separate processes/tasks or logical isolation) so each agent keeps its own state, plugins (STT/TTS/LLM), and event handlers. • Workers register with LiveKit and receive jobs dynamically; the worker accepts multiple job requests and runs them at the same time. What to consider • Resource limits: CPU, memory, and I/O determine how many concurrent agents a single worker can handle. More CPU/memory → more concurrent sessions. • Isolation model: Use separate processes/containers or careful task isolation in a single process to avoid cross-session interference. • Scalability: Load balancing across multiple workers distributes jobs; add workers if one reaches capacity. • Lifecycle management: Ensure each agent session has clear start/stop handling, error isolation, and cleanup to avoid resource leaks. If you want, tell me: • What isolation model you plan to use (separate processes, containers, or in-process tasks), and • An estimate of expected concurrent agents and their resource needs I can then suggest concrete configuration and sizing guidance. Sources: Agent server | LiveKit docs | Deploying to custom environments | LiveKit Docs
q
What I will do is this, I will a single worker process. While dispatching I will add agent name in metadata, and in the entrypoint function I will choose the agent to use depending on the agent name provided in metadata. Will this work?
t
Yes — that will work. Why it works • The worker receives job metadata and your
entrypoint
can read
ctx.job.metadata
to determine which agent to instantiate. • A single worker process can run multiple isolated agent sessions concurrently; selecting the agent based on metadata is a supported pattern. Quick implementation notes • Parse
agent_name
(or
agent_type
) in the
entrypoint
and instantiate the corresponding agent class before starting the session. • Ensure each agent’s code is importable in the worker process. • Keep session isolation (separate tasks/processes or careful async isolation) so agent state, plugins (STT/TTS/LLM), and event handlers don’t interfere. • Prewarm shared resources if needed to reduce startup latency. • Monitor CPU/memory and scale by adding workers when concurrency approaches capacity. If you want, share: • the isolation model you’ll use (in-process async tasks vs separate processes/containers) and • an estimate of concurrent agents I’ll suggest concrete code patterns and sizing guidance. Sources: Job lifecycle | LiveKit docs | Agents v0.x migration guide - Node.js | LiveKit Docs | Agent server options | LiveKit docs | SIP participant | LiveKit docs
q
Right now I have 2 agents. How can I insure session isolation?
t
Good question — here’s how to ensure session isolation for your two agents running in a single worker. Key guarantees • Each job runs in an isolated subprocess and gets its own
JobContext
and
Room
— so sessions are separated by design. • You must avoid introducing cross-session shared state in your application code. Practical steps (do these) • Instantiate one
AgentSession
per job. Keep all session state inside that session object. • Parse
agent_name
from
ctx.job.metadata
in your
entrypoint
and create the appropriate agent instance for that session. • Do not use global mutable variables for session-specific data. Use: ◦
ctx.proc.userData
for shared, stateless prewarmed resources (models, VAD). ◦
session.userdata
or
ctx
for per-session state. • Use
prewarm()
to load heavy shared resources once (store in
proc.userData
) — only share stateless objects. • Add per-job shutdown/cleanup callbacks (via
ctx.add_shutdown_callback
) to release resources and prevent leaks. • Ensure unique participant identities per session (e.g.,
agent-<jobid>
) to avoid naming collisions. • Monitor CPU/memory and scale workers if concurrency approaches capacity. Optional security • Enable per-room E2EE if you need stronger cryptographic separation. If you want, share: • whether you’ll run sessions as in-process async tasks or rely on the framework’s subprocess model, and • expected concurrent sessions I’ll provide a short code pattern and sizing tips based on that. Sources: Job lifecycle | LiveKit docs | Agent server | LiveKit docs | Node.js Agents Architecture | livekit/livekit_composite | DeepWiki | Agent session | LiveKit docs | Agent server options | LiveKit docs