How can I debug a python agent that is not able to...
# ask-ai
m
How can I debug a python agent that is not able to connect to rooms? I'm testing on the agent playground and my agent is not able to connect
t
Thanks for reaching out, Fotis. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Let's work together to debug your Python agent that's having trouble connecting to rooms in LiveKit. Here are some friendly steps to guide you through: 1. Check Environment Variables: Please double-check that
LIVEKIT_URL
,
LIVEKIT_API_KEY
, and
LIVEKIT_API_SECRET
are all set correctly. Sometimes missing or incorrect values can cause connection hiccups. 2. Enable Debug Logging: Try setting the log level to
DEBUG
in your agent. This will give you more insight into what's happening behind the scenes. 3. Verify Token and Grants: Make sure your token includes
room_join=True
, specifies the correct room, and has the
agent=True
grant if that's needed. This helps ensure proper permissions. 4. Confirm Configuration: Check that the
agent_name
in your
WorkerOptions
matches the dispatch request, and review your room-level settings to keep everything aligned. 5. Test in Development Mode: Running the agent locally in
dev
mode can be really helpful to see how it behaves in real-time. 6. Validate Connection Code: Ensure your agent connects using
ctx.connect(auto_subscribe=AutoSubscribe.SUBSCRIBE_ALL)
right in the entrypoint function. This is key for proper connection. 7. Check Room Join URL: Take a moment to verify the
ws_url
format is correct, such as
wss://your-domain.livekit.cloud
. 8. Handle Retries: It’s a good idea to implement retry logic for connection attempts, similar to what the
livekit-cli
load tester uses. This can help with intermittent issues. If you’re still facing challenges after these steps, please make sure your LiveKit server is up and running, and take a look at the server-side logs for any error messages. Remember, you’re not alone in this—we’re here to help you get it sorted! Sources: examples/agent_dispatch.py | Voice AI quickstart | LiveKit Docs | python-agent-example-app/main.py | pkg/loadtester/agentloadtester.go | livekit-plugins/livekit-plugins-bey/livekit/plugins/bey/avatar.py
m
Elaborate on 2
t
To help you get a clearer picture of what's happening with your Python agent, let's enable debug logging together. Setting the logging level to
DEBUG
will give you detailed insights that can really help pinpoint any connection issues. Here's a simple way to do it: 1. First, import the
logging
module in your script:
Copy code
import logging
1. Next, set the logging level to
DEBUG
right at the start of your script or inside your main function:
Copy code
logging.basicConfig(level=logging.DEBUG)
1. Make sure your agent's logging statements are set up to show useful info. For example, you can add some logging around the connection part like this:
Copy code
logging.debug("Attempting to connect to the room...")
ctx.connect(auto_subscribe=AutoSubscribe.SUBSCRIBE_ALL)
logging.debug("Connection successful.")
With debug logging turned on, you'll be able to follow the flow step-by-step and spot exactly where things might be going off track. If you run into any questions or want me to walk you through this, just let me know—I'm here to help!
m
I've completed all this but my agent is still unable to connect. The logs show that the worker is being registered successfully, but the agent is still unable to join the playground room
t
It sounds like your agent's worker is registering successfully but is having trouble joining the playground room. Let’s walk through some helpful troubleshooting steps together: 1. Job Availability Timeout: Please make sure the agent responds to the
AvailabilityRequest
and connects to the room within 10 seconds. Sometimes delays in token generation or
ctx.connect()
can cause timeouts, so double-checking this can really help. 2. Verify Permissions: It’s important to confirm that the agent has the permissions
can_subscribe
,
can_publish
, and
can_publish_data
. Missing any of these might be why the agent can’t access the room. 3. Debug Initialization: Try enabling debug logging to catch any errors during the
entrypoint
function, especially around
ctx.connect()
. Sometimes silent crashes during initialization can cause this issue, and logs will give us clues. 4. Check WebSocket Connection: Double-check that your
LIVEKIT_URL
and
ws_url
are correct and point to your server’s WebSocket endpoint. Network or proxy issues can sometimes block the connection, so this is worth verifying. 5. Monitor Agent Load: Keep an eye on the agent’s load to ensure it doesn’t exceed the threshold (default is 0.7). If the load is too high, the agent might be marked as unavailable. 6. Room Configuration: Please verify that the room or participant configuration includes the correct
agent_name
if you’re using explicit dispatch, or consider using the
AgentDispatch
API for smoother handling. Enabling debug logging and reviewing logs for
AvailabilityRequest
,
JobAssignment
, and
ctx.connect()
will be really helpful. You can also use commands like
lk agent status --id <agent-id>
and
lk agent logs --id <agent-id> --log-type=deploy
to dig a bit deeper. Please feel free to reach out if you’d like me to guide you through any of these steps—I’m here to help! Sources: README.md | Getting Help & Troubleshooting | livekit/livekit_composite | DeepWiki | pkg/service/agentservice.go | Job lifecycle | LiveKit Docs | livekit-agents/livekit/agents/worker.py
m
My agent works when I run
uv run src/agent.py console
- I can speak and get a voice reply. However when I run
uv run src/agent.py dev
I'm not able to connect via the LiveKit Agents Playground. What am I doing wrong?
t
Let’s take a moment to explore the difference between running your agent in
console
mode and
dev
mode, so you feel confident about which one to use: • Console Mode: This runs locally right in your terminal, perfect for quick testing with direct audio input/output. It doesn’t connect to any external servers, keeping things simple and straightforward. • Dev Mode: This mode connects your agent to LiveKit Cloud or a self-hosted server, which means you can access it remotely through tools like the Agents Playground. Plus, it supports hot reloading, making development smoother and more efficient. To help your agent connect smoothly in
dev
mode, here are a few friendly steps to check: 1. Make sure
LIVEKIT_URL
,
LIVEKIT_API_KEY
, and
LIVEKIT_API_SECRET
are all correctly set and match what’s configured in the Playground. 2. Double-check that your agent is actively running in
dev
mode by using
uv run src/agent.py dev
. 3. Confirm that both your agent and the Playground are pointing to the same LiveKit project and room — this alignment is key! 4. Ensure your network allows access to the LiveKit server, and there aren’t any firewalls or network issues blocking the WebSocket connection. If you try these steps and still face any challenges, please don’t hesitate to reach out. I’m here to help you get this working perfectly! Sources: README.md | Voice AI quickstart | LiveKit Docs | README.md
m
All items except the last one have been confirmed. Show me how I can check whether the WS connection is blocked. I'm using a Mac
t
Let's check together if the WebSocket connection to the LiveKit server might be blocked on your Mac. Here’s a simple step-by-step guide: 1. Test the WebSocket URL: You can use a handy tool like PieSocket WebSocket Tester or something similar. Just enter your LiveKit WebSocket URL (for example,
wss://your-domain.livekit.cloud
) and try connecting. If it doesn’t connect, it could be a network issue. 2. Use the Terminal: Open your Terminal and run this command to see if you can reach the LiveKit server’s domain and port: `telnet your-domain.livekit.cloud 443`If it connects successfully, you’ll see a blank screen. If not, an error message will pop up. 3. Check Firewall Settings: On your Mac, head over to System Preferences > Security & Privacy > Firewall. Make sure your firewall isn’t blocking outgoing connections to the LiveKit server. 4. Inspect Network Logs: If you’re using a browser, open the developer tools and look at the "Network" tab for any WebSocket connection errors when you access the Playground. This can give clues if something’s going wrong. Please feel free to reach out if you hit any bumps along the way—I’m here to help!
m
I think the issue was my usage quota. Show me what do I need to add to my python agent in order to get an alert when the agent is not able to join a room due to billing & usage thresholds
t
Could you please let me know if you're using LiveKit Cloud or a self-hosted instance? This will really help me understand your situation better and guide you on the available billing-related events or APIs that suit your needs. Sources: pkg/loadtester/agentloadtester.go | Operational Guidance & Best Practices | livekit/livekit_composite | DeepWik...
m
I'm planning to use livekit cloud
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!