Hi, we're experimenting with `livekit-agents` newl...
# ask-ai
m
Hi, we're experimenting with
livekit-agents
newly added concept of Tasks, however, we're experiencing this issue where with each Task needs to be created with a fresh llm=self._realtime_model, and AgentTask is likely creating a NEW session/connection for each task instead of reusing the existing one from the main AgentSession. This causes slight delay in the response.
t
Thanks for reaching out, Hugo. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm glad you're exploring the
AgentSession
in LiveKit Agents! This feature is thoughtfully designed to manage a single, persistent session for an agent, so all tasks share the same session context and lifecycle. By creating and executing tasks within the existing session using
asyncio.create_task()
, it helps avoid the need to establish new connections for each task, which is pretty efficient. The experimental
AgentTask
class works hand-in-hand with the current
AgentSession
without opening new connections. It keeps everything in sync—like room connections, media streams, and agent state—and once a task finishes, control goes right back to the original agent. If you’re noticing any delays, it could be helpful to take a closer look at how tasks are being created and make sure they’re smoothly integrated with the existing session. Please feel free to reach out if you’d like me to walk you through some examples or offer more tailored guidance—I’m here to help! Sources: Workflows | LiveKit Docs | livekit-agents/livekit/agents/voice/agent.py
m
Thank you for the analysis, for more context here's the pattern that we're experiencing:
Copy code
2025-11-03 15:04:48,157 - Completed task: Call reason
2025-11-03 15:04:48,158 - Running task: Noise location
2025-11-03 15:04:48,158 - DEBUG livekit.plugins.google - connecting to Gemini Realtime API...
2025-11-03 15:04:48,196 - DEBUG livekit.plugins.google - connecting to Gemini Realtime API...  ← RECONNECTING!
2025-11-03 15:04:48,843 - INFO google_genai.live - b'{\n  "setupComplete": {}\n}\n'  ← ~700ms delay!
t
Thank you so much for sharing the detailed logs with me. It looks like the issue is connected to the Gemini Realtime API reconnecting during task execution, which can understandably cause some delays. Let’s work through a few steps that might help improve things: 1. Configure Retry Settings: You might want to try reducing the retry delays to speed up reconnections. For instance, setting
retry_interval
to 1 second and limiting
max_retry
to 3 could make a difference. 2. Enable Session Resumption: This feature helps keep the session context across reconnections, which can really cut down on initialization time. 3. Monitor Connection Errors: Keep an eye on your logs for messages like "connection failed" or "retrying"—these can give clues about what’s happening. 4. Optimize Network: It’s a good idea to ensure your agent server is located close to the API endpoints and to watch for any network latency or packet loss that might be affecting performance. 5. Implement Graceful Degradation: Managing connection states carefully can help your system handle reconnections more smoothly. If you’d like, I’m here to help with specific code examples or walk you through any of these steps. Just let me know! Sources: Real-time AI Models & Streaming | livekit/livekit_composite | DeepWiki | Operational Guidance & Best Practices | livekit/livekit_composite | DeepWik... | Troubleshooting & Common Issues | livekit/livekit_composite | DeepWiki | livekit-agents/livekit/agents/voice/agent_activity.py | agents/src/voice/agent_activity.ts