advise me Setting Up LiveKit Agent Dispatch Sin...
# ask-ai
m
advise me Setting Up LiveKit Agent Dispatch Since you're not using SIP and need pattern-based room matching for app-generated rooms, you have two options: Option 1: Use a Wildcard/Prefix Pattern (If Supported) In the dispatch rule creation form: 1. Rule name: Mother Moon Agent Dispatch 2. Rule type: Keep as Direct 3. Room name: Try entering mother-moon-* or just mother-moon to see if it accepts wildcards or prefix matching 4. Agent dispatch: Already enabled ✓ 5. Agent name: Enter the name that matches your Python agent worker (it's showing "My telephony agent" as placeholder) 6. Dispatch metadata: You can add: { "agent_type": "mother_moon", "version": "1.0" } Option 2: Explicit Room Name (For Testing) If wildcards aren't supported, you'll need to either: 1. Use a fixed room name in your iOS app: ⏺️ *Update*(MotherMoonV2/LiveKitVoiceAssistantView.swift) ⎿ Updated MotherMoonV2/LiveKitVoiceAssistantView.swift with 2 additions and 1 removal 456 room!.add(delegate: self) 457 458 // Generate token with agent configuration 459 - let roomName = "mother-moon-\(UUID().uuidString.prefix(8))" 459 + // Use a fixed room name for dispatch rule matching 460 + let roomName = "mother-moon-room" // Fixed name for dispatch rule 461 let participantName = "user-\(UUID().uuidString.prefix(8))" 462 463 print("LiveKit: Creating room: \(roomName) for participant: \(participantName)") ⏺️ 2. Create multiple dispatch rules for different room patterns if needed Option 3: External Dispatcher (Production Solution) For production with dynamic room names, you'll need an external dispatcher service that: ⏺️ *Write*(agent/external_dispatcher.py) ⎿ Wrote 73 lines to agent/external_dispatcher.py #!/usr/bin/env python3 """ External dispatcher for LiveKit agents Monitors room creation and dispatches agents based on room name patterns """ import asyncio import logging from livekit import api from dotenv import load_dotenv … +63 lines (ctrl+r to expand) ⏺️ Recommended Setup Steps: For Immediate Testing: 1. In the LiveKit dashboard dispatch rule form: - Rule name: Mother Moon Voice Agent - Room name: mother-moon-room (exact match since we changed iOS app to use fixed name) - Agent name: Leave empty or enter your agent identifier - Click Create 2. Make sure your Python agent is running: cd /Users/jesselinson/mother-moon-v2/agent python simple_agent.py dev 3. Build and run the iOS app - it will now connect to the fixed room name For Production: 1. SIP Trunks Option: If you can use SIP trunks, you can create pattern-based dispatch rules in the dashboard 2. External Dispatcher: Run the external dispatcher service that monitors rooms and dispatches agents programmatically 3. Server-Side Token Generation: Generate tokens on your backend and include agent dispatch metadata The key insight from the documentation is that LiveKit Cloud's built-in dispatch rules work best for SIP scenarios. For app-generated dynamic room names, you'll need either: - Use fixed room names (simplest for testing) - Implement an external dispatcher service (best for production) Once you create the dispatch rule in the dashboard with the fixed room name mother-moon-room, your voice agent should automatically join when the iOS app creates that room.
t
Thanks for reaching out, Jesse. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
LiveKit Agent Dispatch currently only supports exact room name matching in dispatch rules. Wildcard or pattern-based matching (such as using prefixes or wildcards like
mother-moon-*
) is not supported for agent dispatch rules. For dynamically generated room names, the recommended methods are: • Explicit Dispatch via API: Programmatically create dispatches for each room as they're created. This is done using the LiveKit API. For example:
Copy code
dispatch = await lkapi.agent_dispatch.create_dispatch(
    api.CreateAgentDispatchRequest(
        agent_name=agent_name, 
        room=room_name, 
        metadata='{"user_id": "12345"}'
    )
)
Token-based Dispatch: Configure participant tokens to dispatch agents upon connection:
Copy code
token = (
    AccessToken()
    .with_room_config(
        RoomConfiguration(
            agents=[
                RoomAgentDispatch(agent_name="test-agent", metadata='{"user_id": "12345"}')
            ],
        ),
    )
)
SIP Dispatch Rules: Pattern-based (prefix) matching is only available for SIP/telephony use cases, not for general agent dispatch rules. For testing, using a fixed room name is the simplest option. For production and dynamic room names, you should implement an external dispatcher or programmatically create agent dispatches via API as new rooms are created. If you'd like step-by-step guidance to implement one of these solutions, let me know which method you're interested in. Sources: packages/livekit-server-sdk/src/AgentDispatchClient.ts | Agent dispatch | LiveKit Docs | SIP dispatch rule | LiveKit Docs
s
If I wanted to manually generate the JWT token what would the JWT payload need to look like for me to be able to do token based dispatch
t
Hey there! Let me walk you through generating a JWT token for agent dispatch in LiveKit. It's pretty straightforward, and I'll break it down in a friendly way. Example JWT payload:
Copy code
{
  "iss": "your-api-key",
  "sub": "participant-identity",
  "exp": 1234567890,
  "nbf": 1234567890,
  "video": {
    "room": "room-name",
    "roomJoin": true
  },
  "roomConfig": {
    "agents": [
      {
        "agentName": "your-agent-name",
        "metadata": "{\"user_id\": \"12345\"}"
      }
    ]
  }
}
Let me explain the key fields that make this work: • iss: Your LiveKit API key (think of it like your special pass) • sub: The participant's identity (who's joining the room) • video.room: The room name as a string • video.roomJoin: Set to
true
to give the green light to join • roomConfig.agents: Your list of agents to dispatch, each with an agentName (matching your agent worker's name) and optional metadata When you use this token to connect, the specified agent will automatically join the room. Want to bring in multiple agents? Just add more
RoomAgentDispatch
objects to the
agents
array. Need help with encoding or have specific requirements? I'm all ears! Just drop me a message, and I'll be happy to help you out. Sources: Agent dispatch | LiveKit Docs | packages/livekit-server-sdk/src/AccessToken.test.ts | Authentication | LiveKit Docs | livekit-api/livekit/api/access_token.py
s
this token is allowing the room and participant to be added but the agent is not being dispatched
t
Hey there! Having trouble getting your agent dispatched? No worries, I'll walk you through some quick troubleshooting steps that might help solve the mystery. • Agent Name Match: Double-check that the
agentName
in your token's
roomConfig.agents
is an exact match (yes, even down to capitalization!) with the
agent_name
in your agent worker's
WorkerOptions
. • Agent Worker Running: Make sure your agent worker is up and running, connected to the LiveKit server, and successfully registered. Think of it like making sure all your team members are clocked in and ready to go. • Token Configuration: Take a moment to verify that the token's
roomConfig
looks perfect and that the agent name is spelled exactly the same way in both places. Tiny typos can be sneaky! • Authentication: Confirm that your agent worker has the correct
LIVEKIT_URL
,
LIVEKIT_API_KEY
, and
LIVEKIT_API_SECRET
. It's like making sure everyone has the right security badge to enter the building. • Logs: Peek into your agent worker logs and look for registration, connection, and job assignment messages. The LiveKit server logs can also reveal any agent dispatch hiccups. • Direct Dispatch Test: As a pro troubleshooting tip, try using the API to manually dispatch the agent to the room. This can help you figure out if the issue is with the token or something on the agent/service side. Getting an agent dispatched via token is all about making sure everything is configured just right. If you're stuck, I'd be happy to help you dig into those logs or walk you through some test commands. You've got this! Sources: Dispatching agents | LiveKit Docs | LiveKit Agents | README.md | Web and mobile frontends | LiveKit Docs
s
{ "iss": "key", "sip": { "admin": true, "call": true }, "sub": "identity", "video": { "roomList": true, "roomAdmin": true, "roomCreate": true, "roomJoin": true, "room": "room-name" }, "roomConfig": { "agents": [ { "agentName": "agent-name" } ] } } above is an example of my JWT payload and I have confirmed that all details are correct and matching the agent working including, agent name, keys, secrets, and url
t
Hey there! I totally get how frustrating JWT payload issues can be. Let me help you troubleshoot this LiveKit agent dispatch challenge in a way that'll hopefully make things click. • Field naming must match
camelCase
exactly
: - Use
agentName
(not
agent_name
) in both your token and your agent worker configuration. - Use
roomConfig
(not
room_config
). • The
roomConfig.agents
array must match exactly as expected
(including naming and structure). • The agent must be started with
agent_name="agent-name"
in WorkerOptions.
Optional: Add a
metadata
field to your agent entry if you want to pass custom data but it is not required for dispatch. I know these little details can drive you crazy! Here are some common gotchas that might be causing your silent dispatch failure: • Case sensitivity or field naming differences (e.g.
agentName
vs.
agent_name
).
Agent worker is NOT running with explicit
agent_name
supplied (it must be).
Trust me, even the tiniest naming difference can totally block dispatch. Double-check that your JWT field names and values match up perfectly, and keep everything in that crisp
camelCase
format. If you've confirmed everything looks good and you're still stuck, no worries! The next step would be checking those agent worker/server logs for registration and job assignment events. I'd be happy to walk you through what to look for if you need an extra hand. We'll solve this together! Sources: Authentication | LiveKit Docs | packages/livekit-server-sdk/src/AccessToken.test.ts | CHANGELOG.md | Dispatching agents | LiveKit Docs
m
solve this ConnectionError: could not establish signal connection: invalid token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiSmVzc2UgTGluc29uIiwibWV0YWRhdGEiOiJ7XCJ1c2VyX3R5cGVcIjogXCJjdXN0b21lclwiLCBcInJvb21fbmFtZVwiOiBcImJkNzEtNWMzZC02YmQ1XCIsIFwiZW52aXJvbm1lbnRcIjogXCJQUk9EXCJ9IiwidmlkZW8iOnsicm9vbUpvaW4iOnRydWUsInJvb20iOiJiZDcxLTVjM2QtNmJkNSIsImNhblB1Ymxpc2giOnRydWUsImNhblN1YnNjcmliZSI6dHJ1ZSwiY2FuUHVibGlzaERhdGEiOnRydWUsImNhblVwZGF0ZU93bk1ldGFkYXRhIjp0cnVlfSwic3ViIjoiY3VzdG9tZXJfamVzc2UrOTA1OTQzNEBib29rdmlkLmNvbSIsImlzcyI6IkFQSXdiUUZVZ3BKUUVvUSIsIm5iZiI6MTc1MjIwMzA3NiwiZXhwIjoxNzUyMjI0Njc2fQ.4iJkK1wn0Gu1zPS6ZQso67HBfxomKdyF5OsMT16HkDY, error: go-jose/go-jose: error in cryptographic primitive
t
Hey there! Looks like you're running into a tricky JWT verification error with LiveKit. Don't worry, we'll help you get this sorted out. 😊API Secret Mismatch: Let's double-check your API secret. Make sure it's an exact match between what you're using to sign the token and what's set up in your LiveKit project. • Signing Algorithm: Quick tip - you'll want to use
HS256
(HMAC SHA-256) for signing. Other algorithms won't work here. • Token Structure and Claims: Your JWT needs to have all the key players:
iss
,
sub
,
exp
,
nbf
, and
video
with
roomJoin
and
room
fields. Think of it like a guest list for your token party! 🎉Issuer Claim: The
iss
in your token should match your LiveKit API key exactly. • Encoding: When signing, make sure your API secret is encoded as bytes. No hex strings or other sneaky formats! • Expiration: Just a quick check - your token isn't expired, right? Make sure the
exp
claim is valid and in the future. Here's how we can fix this: 1. Whip up a fresh test token using the LiveKit SDK or a reliable signing function. 2. Use the exact same API key and secret from your LiveKit dashboard - both for signing and server settings. 3. Do a quick verification that your signature is
HS256
and your payload has all those required claims. Pro tip: This error is almost always about a little mix-up with your token's secret, algorithm, or structure. We've got this! 💪 Sources: packages/livekit-server-sdk/src/AccessToken.ts | Authentication | LiveKit Docs