How can I update "agentName" in the dispatch rule ...
# ask-ai
a
How can I update "agentName" in the dispatch rule based on the data coming in Header?? Can we have dynamic agent dispatch for the incoming job request?
✅ 1
t
Thanks for reaching out, Hiren. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — I’m responding to your question about updating
agentName
in a dispatch rule from incoming headers and whether you can do dynamic agent dispatch. Short answer: you cannot change a dispatch rule’s
agent_name
on-the-fly from headers. However, you can achieve dynamic dispatch by making the routing decision outside the dispatch rule and then instructing LiveKit which agent to dispatch. Ways to implement dynamic dispatch based on headers: • Generate the room access token with the desired agent via
RoomConfiguration.agents
(include
agent_name
) when your backend inspects the incoming headers; this lets you decide per-request which agent to target. • Have your backend webhook/middleware inspect headers and call the AgentDispatchService API or LiveKit CLI to create a dispatch to the chosen
agent_name
programmatically. • For SIP flows, create multiple static dispatch rules/trunks for different numbers/PINs and route incoming calls to the appropriate trunk; use external logic to pick the trunk if headers determine routing. • Use a coordinator/supervisor agent pattern: a coordinator evaluates header data and triggers dispatch to specialized agents via API. Next step: which integration are you using — SIP inbound, a web frontend, or something else? Share a sample header or flow and I’ll suggest the exact token/API pattern and a short code snippet you can use. Sources: Agents Framework | livekit/livekit_composite | DeepWiki | Agent dispatch | LiveKit Documentation | Avoiding crosstalk when multiple developers share a LiveKit project | LiveK... | How to Use the Supervisor Pattern for Multi-Agent Voice AI Systems | LiveKi..., with interactive elements
a
I'm getting incoming call request directly on my IP address of my self hosted livekit server.. where should I intercept it to change the agent_name dynamically?
t
Thanks — I’m responding to where you should intercept the incoming call on your self-hosted LiveKit server to set
agent_name
dynamically. You must intercept the request before LiveKit creates the room or before you call the dispatch API. LiveKit’s dispatch rules are static, so dynamic routing happens in your middleware/orchestration layer. A few common interception points: • If calls arrive via SIP to a SIP gateway: ◦ Intercept at your SIP gateway (LiveKit SIP gateway or an external SIP server like FreeSWITCH/OpenSIPS/Asterisk). Parse SIP headers there and either: ▪︎ Generate a room access token with
RoomConfiguration.agents
including the chosen
agent_name
, then create/join the room. ▪︎ Or call the AgentDispatchService API / LiveKit CLI to dispatch the chosen
agent_name
to the room after room creation. • If calls hit your HTTP endpoint or proxy directly: ◦ Intercept in the HTTP middleware (reverse proxy/nginx, or your app server). Inspect headers, then: ▪︎ Create the token with the
agent_name
and return/use it to join the room. ▪︎ Or trigger the AgentDispatch API to dispatch the agent. • If you have a coordinator service: ◦ Let a coordinator evaluate headers and call LiveKit’s dispatch API programmatically to target the correct
agent_name
. Next step: which protocol/gateway are you actually using (LiveKit SIP gateway, FreeSWITCH/Asterisk/OpenSIPS, or plain HTTP) and do you control the SIP/HTTP ingress? Share that and I’ll provide the exact place in the flow and a short code snippet for token creation or dispatch API call. , with interactive elements
a
I get call from asterisk.. which is adding custom headers for me to identify that this call belongs to which agent
t
Thanks — I’m responding to your setup where Asterisk adds custom headers and you want to use those headers to pick
agent_name
dynamically. Where to intercept • Intercept in Asterisk before it hands the call off to LiveKit: in the dialplan (extensions.conf) or an AGI/AMI script. Extract your custom SIP/PAI/X- headers there. • Alternatively intercept in a middleware service that Asterisk calls (an AGI or an HTTP webhook) if you prefer keeping logic out of the dialplan. What to do after extracting the header • Option A — Generate a room token with an `agent_name`: a. Read the header that identifies the agent. b. From your AGI/AMI or middleware, call your backend to create a LiveKit access token that includes
RoomConfiguration.agents
with
agent_name
set to the identified agent. c. Use that token when creating/joining the room so the specific agent gets targeted. • Option B — Create the room and programmatically dispatch the agent: a. Let Asterisk trigger room creation (or your backend create it). b. From your AGI/AMI or middleware, call the AgentDispatchService API (or LiveKit CLI) with the chosen
agent_name
to dispatch them to the room. Short example patterns • In Asterisk dialplan (pseudo): ◦ Read header:
${SIP_HEADER(X-Custom-Agent)}
or
${PJSIP_HEADER(read,X-Custom-Agent)}
◦ Call an AGI/HTTP endpoint with that value. • Backend token creation (pseudo Python):
Copy code
# create token with RoomConfiguration.agents(agent_name="agent-from-header")
  token = AccessToken().with_identity("call").with_room_config(RoomConfiguration(agents=[RoomAgentDispatch(agent_name="agent-from-header")])).to_jwt()
• Dispatch API (CLI example):
Copy code
lk dispatch create --agent-name agent-from-header --room my-room
Which option to pick • If you want the agent to be the only one eligible automatically, generate the token with
agent_name
. • If the room already exists or you want to trigger agents on demand, use the AgentDispatchService API. Next step Tell me whether you want a concrete example for: • Asterisk dialplan snippet (extensions.conf) or AGI script • A backend language (Node/Python/Go) for token creation or the exact API request to AgentDispatchService I’ll provide the exact code/snippet you need. , with interactive elements