I've configured agent_name in WorkerOptions for ex...
# ask-ai
n
I've configured agent_name in WorkerOptions for explicit dispatch, is there anything else I need to configure to enable the explicit dispatch
t
Thanks for reaching out, Yuhan. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're on the right track—setting
agent_name
in
WorkerOptions
is all that's required on the agent worker to enable explicit dispatch and disable automatic dispatch. After that, the agent will only be assigned to rooms when you explicitly dispatch it by one of these methods: • Token-based dispatch: include a
RoomConfiguration
with
RoomAgentDispatch
in the participant's access token. • API-based dispatch: call the
AgentDispatchService
API to create a dispatch request. • SIP dispatch rules: configure a SIP dispatch rule with
roomConfig.agents
for inbound calls. Would you like instructions for one of those dispatch methods (token, API, or SIP) for your environment? Sources: Agent dispatch | LiveKit docs | SIP dispatch rule | LiveKit docs
n
if I'm using livekit cloud playground, which dispatch method should I use
t
For the LiveKit Cloud Playground use explicit dispatch. You can dispatch your named agent either by: • Token-based dispatch: include a
RoomConfiguration
with
RoomAgentDispatch
(agent_name and optional metadata) in the participant’s access token so the agent is dispatched when the participant joins the room. • API-based dispatch: call the
AgentDispatchService
API to create a dispatch request and assign the agent to the room. Token-based dispatch is simplest for Playground testing (agent joins as the participant connects); use the API when you need programmatic control or delayed/conditional dispatch. Which method would you like step-by-step instructions for? Sources: Agent dispatch | LiveKit docs | Agents telephony integration | LiveKit docs
n
please give me step-by-step instructions for token-based dispatch
t
You're all set — follow these steps for token-based explicit dispatch in the LiveKit Cloud Playground: 1. Name your agent (disables automatic dispatch) • Python:
@server.rtc_session(agent_name="my-agent")
• Node.js:
new WorkerOptions({ agentName: "my-agent" })
1. Generate a participant access token that includes
RoomConfiguration.agents
with a
RoomAgentDispatch
entry (agent name + optional metadata). Example — Python:
Copy code
from livekit.api import AccessToken, VideoGrants, RoomConfiguration, RoomAgentDispatch

token = (
  AccessToken()
  .with_identity("participant-identity")
  .with_grants(VideoGrants(room_join=True, room="my-room"))
  .with_room_config(
    RoomConfiguration(
      agents=[RoomAgentDispatch(agent_name="my-agent", metadata='{"user_id":"12345"}')]
    )
  )
  .to_jwt()
)
Node.js example:
Copy code
import { AccessToken } from "livekit-server-sdk";
import { RoomConfiguration, RoomAgentDispatch } from "@livekit/protocol";

const at = new AccessToken("API_KEY","API_SECRET");
at.identity = "participant-identity";
at.addGrant({ roomJoin: true, room: "my-room" });
at.roomConfig = new RoomConfiguration({
  agents: [ new RoomAgentDispatch({ agentName: "my-agent", metadata: '{"user_id":"12345"}' }) ]
});
const token = await at.toJwt();
1. Start the agent server so it can accept dispatch jobs (ensure the
agent_name
matches the token). 2. Connect the participant using the generated token. When they join, the room is created if needed and the named agent is dispatched automatically. 3. Optional: read
metadata
from the agent’s
JobContext
to personalize behavior. Would you like this in a specific language or a complete minimal repo/example for the Playground? Sources: Agent dispatch | LiveKit docs | Authentication | LiveKit docs | Agents telephony integration | LiveKit docs | Web and mobile frontends | LiveKit docs
n
yes, the python example
t
Below are concise, runnable Python steps to implement token-based explicit dispatch for the LiveKit Cloud Playground. Prerequisites • Install LiveKit Server SDKs (example packages used below): pip install livekit-server-sdk livekit-proto 1) Name your agent (agent file: agent.py)
Copy code
from livekit.agents import AgentServer, JobContext

server = AgentServer()

@server.rtc_session(agent_name="my-agent")
async def my_agent(ctx: JobContext):
    # example: read metadata
    metadata = ctx.metadata or {}
    print("Dispatched with metadata:", metadata)
    # implement agent behavior here
    await ctx.wait()  # keep alive for demo; replace with real logic

if __name__ == "__main__":
    server.run()
2) Generate a participant token that includes RoomConfiguration with RoomAgentDispatch (token generation script: create_token.py)
Copy code
from livekit.api import AccessToken, VideoGrants, RoomConfiguration, RoomAgentDispatch

API_KEY = "your_api_key"
API_SECRET = "your_api_secret"

def create_token():
    token = (
        AccessToken(api_key=API_KEY, api_secret=API_SECRET)
        .with_identity("participant-identity")
        .with_grants(VideoGrants(room_join=True, room="my-room"))
        .with_room_config(
            RoomConfiguration(
                agents=[
                    RoomAgentDispatch(
                        agent_name="my-agent",
                        metadata='{"user_id":"12345"}'  # optional
                    )
                ]
            )
        )
        .to_jwt()
    )
    return token

if __name__ == "__main__":
    print(create_token())
3) Start the agent server • Run the agent so it can accept dispatch jobs: python agent.py 4) Connect the participant using the generated token • Provide the printed JWT from create_token.py to your frontend/client SDK and join "my-room". • When the participant connects, the room will be created if needed and the named agent
my-agent
will be dispatched automatically. 5) Use metadata in the agent • In the agent handler
ctx.metadata
contains the JSON string from
RoomAgentDispatch.metadata
for per-job info. Notes • Ensure
agent_name
in the token matches the
agent_name
used in the agent code. • Keep the agent process running in the Playground while testing. Would you like a minimal repository structure or a full example that includes a simple frontend to join the room with the token?
n
can I use agents.cli.run_app(agents.WorkerOptions to achieve this
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!
n
for another question, if I'm on the build plan, and after some idle time, my agent goes to "Sleeping" status. If using auto dispatch, is it going to wake up the agent? I'm currently seeing "agent dispatch error" when new session created while the agent is sleeping