Only one `AgentSession` can be the primary at a time Error when starting a session

hi, im getting this error

{"message": "unhandled exception while running the job task\nTraceback (most recent call last):\n  File \"/usr/local/lib/python3.10/site-packages/opentelemetry/util/_decorator.py\", line 71, in async_wrapper\n    return await func(*args, **kwargs)  # type: ignore\n  File \"/usr/local/lib/python3.10/site-packages/livekit/agents/ipc/job_proc_lazy_main.py\", line 286, in _traceable_entrypoint\n    await self._job_entrypoint_fnc(job_ctx)\n  File \"/opt/deployment/voice-agents-backend/main.py\", line 356, in entrypointx\n    await agentToRun.entrypoint(ctx)\n  File \"/opt/deployment/voice-agents-backend/screening_assistant.py\", line 2483, in entrypoint\n    await session.start(\n  File \"/usr/local/lib/python3.10/site-packages/livekit/agents/voice/agent_session.py\", line 595, in start\n    raise RuntimeError(\nRuntimeError: Only one `AgentSession` can be the primary at a time. If you want to ignore primary designation, use session.start(record=False).", "level": "ERROR", "name": "livekit.agents", "exc_info": "Traceback (most recent call last):\n  File \"/usr/local/lib/python3.10/site-packages/opentelemetry/util/_decorator.py\", line 71, in async_wrapper\n    return await func(*args, **kwargs)  # type: ignore\n  File \"/usr/local/lib/python3.10/site-packages/livekit/agents/ipc/job_proc_lazy_main.py\", line 286, in _traceable_entrypoint\n    await self._job_entrypoint_fnc(job_ctx)\n  File \"/opt/deployment/voice-agents-backend/main.py\", line 356, in entrypointx\n    await agentToRun.entrypoint(ctx)\n  File \"/opt/deployment/voice-agents-backend/screening_assistant.py\", line 2483, in entrypoint\n    await session.start(\n  File \"/usr/local/lib/python3.10/site-packages/livekit/agents/voice/agent_session.py\", line 595, in start\n    raise RuntimeError(\nRuntimeError: Only one `AgentSession` can be the primary at a time. If you want to ignore primary designation, use session.start(record=False).", "pid": 822938, "job_id": "AJ_TPydKPrtXUgz", "room_id": "RM_n8viB9P2pQnW", "timestamp": "2026-04-06T15:27:24.501752+00:00"}

when i try to run two sessions one after the other, and i completely close the previous session and only then start new session, another thing is first session is record=False and only second session is record=True so from what i have read, since the first session is non primary and its closed completely before second session has started its giving this erroreven though im

  1. giving record - False to first session, and record = True for second session

  2. and closing the first session completely using drain and aclose before starting next session,

when trying to start next session i am getting this error, it doesn’t always happen btw, it happens inconsistently not everytime but alot of times.

Reading your exception message you shared. It looks like you’re running into the following issue.

An earlier “primary” session in the same room (maybe from a different agent or prior run in entrypoint?) is already started and still active when line 2483 of screening_assistant.py calls session.start.

This typically happens when your code creates and starts multiple AgentSession instances without marking the additional ones as non-primary. For example, if you’re handing off between agents or running a second session for a different purpose within the same job context.

Try adding some logging around that line before starting the next agent and ensure the state of the previous agent. :robot:

yes that is what we are doing, but based on the logs

at

20:57:22.912

the first session is closed meaning - await session.aclose() line is executed and then our next log is printed (i also do drain before at 20:57:20.805)

and the session.start for the next session is called at - 20:57:24.479 which is almost 2 seconds after the previous session is closed but we are still seeing that error in some calls. the record value for the first session is false and second session is true btw

This sounds like it may be a race condition, and with the first agent never primary (record=False), that may be the source of the problem. Below are a few things to try. Hope that helps.

Check if there’s an implicit primary session in your entry point, or the framework’s job setup.

  1. Start the second session with record=False temporarily to confirm the theory. If it never errors, you know it’s purely a primary-designation conflict.
  2. Look at the LiveKit agents SDK source around agent_session.py line 595 to see exactly how primary is tracked — is it a room-level flag on the server, or a local in-process check? If it’s local/in-process, there may be a class-level or context-level variable you can inspect or reset before starting the second session.
  3. aclose() may not fully release the primary, even though aclose() returns. A 2 second gap seems good, but might not be enough under network load. You can add a guard before starting the second session