5-11-03 12:39:14,122 - ERROR livekit.agents - Agen...
# ask-ai
i
2025-11-03 123914,122 - ERROR livekit.agents - AgentSession is closing due to unrecoverable error Traceback (most recent call last): File "/Users/melvinramos/.pyenv/versions/3.12.9/envs/sales-receptionist/lib/python3.12/site-packages/livekit/agents/stt/stt.py", line 289, in _main_task return await self._run() ^^^^^^^^^^^^^^^^^ File "/Users/melvinramos/.pyenv/versions/3.12.9/envs/sales-receptionist/lib/python3.12/site-packages/livekit/agents/inference/stt.py", line 416, in _run ws = await self._connect_ws() ^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/melvinramos/.pyenv/versions/3.12.9/envs/sales-receptionist/lib/python3.12/site-packages/livekit/agents/inference/stt.py", line 469, in _connect_ws ws = await asyncio.wait_for( ^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/melvinramos/.pyenv/versions/3.12.9/lib/python3.12/asyncio/tasks.py", line 520, in wait_for return await fut ^^^^^^^^^ File "/Users/melvinramos/.pyenv/versions/3.12.9/envs/sales-receptionist/lib/python3.12/site-packages/aiohttp/client.py", line 1100, in _ws_connect raise WSServerHandshakeError( aiohttp.client_exceptions.WSServerHandshakeError: 401, message='Invalid response status', url='wss://agent-gateway.livekit.cloud/v1/stt'
t
Thanks for reaching out, Melvin. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm sorry to hear you're encountering the
WSServerHandshakeError 401
. This usually happens when the WebSocket connection handshake doesn’t go through because the request isn’t authorized, often due to an invalid or missing authentication token. Let’s work through this together to get it sorted out: • Verify API Credentials: Please double-check that your
api_key
and
api_secret
are correct and have the right permissions for the STT services. • Set Credentials Properly: Make sure you’re either passing the
api_key
and
api_secret
directly when initializing the STT instance, or that they’re set as environment variables (
LIVEKIT_API_KEY
and
LIVEKIT_API_SECRET
). • Check Base URL: Confirm that the WebSocket URL (`wss://agent-gateway.livekit.cloud/v1/stt`) is accurate and accessible from your environment. • Inspect Token Generation: Take a look at your
create_access_token()
function to ensure it’s generating a valid token with the correct inputs. Once you’ve checked these points, things should hopefully start working smoothly. If you run into any more issues or have questions along the way, please don’t hesitate to reach out—I’m here to help! Sources: livekit-plugins/livekit-plugins-rtzr/livekit/plugins/rtzr/stt.py | Baseten STT plugin guide | LiveKit Docs | Core Platform Architecture & Authentication | livekit/livekit_composite | D...
i
yes this is new? It was working before, then stopped working, so, I've regenerated
t
It sounds like the WebSocket connection stopped working after regenerating your API credentials. No worries—let’s walk through some steps together to get this sorted out: • Update API Credentials: First, please make sure your new
LIVEKIT_API_KEY
and
LIVEKIT_API_SECRET
are correctly updated in your environment variables or configuration. • Restart the Application: After updating, try restarting your application or agent worker. This helps reload the new credentials since just updating environment variables alone might not take effect immediately. • Verify Token Generation: Double-check that the
create_access_token()
function is using those updated credentials to generate a valid token. • Check Logs: Take a peek at your logs for any authentication or connection errors during the WebSocket handshake. This can give clues if something’s still off. • Test Connectivity: You might want to use a WebSocket testing tool to confirm connectivity to the LiveKit endpoint (`wss://agent-gateway.livekit.cloud/v1/stt`) with your new credentials. Please feel free to reach out if these steps don’t quite fix the issue or if you’d like me to help you troubleshoot further. I’m here to help! Sources: Diagnosing Connection Errors with Connection Test Utility | livekit-plugins/livekit-plugins-rtzr/README.md | README.md | Core Platform Architecture & Authentication | livekit/livekit_composite | D...