"Are there any methods in LiveKit SDK to manage me...
# ask-ai
s
"Are there any methods in LiveKit SDK to manage memory usage for long-running agents? We're seeing memory/CPU accumulation in AudioStream._run and _ParticipantAudioInputStream._forward_task over time."
t
Thanks for reaching out, MBMBMB. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thank you for reaching out! I understand you're looking into managing memory usage for
AudioStream._run
and
_ParticipantAudioInputStream._forward_task
. While the LiveKit SDK doesn't offer direct methods for these specific tasks, it does provide some helpful process-level memory management features to keep things running smoothly: • You can use
WorkerOptions
to set a
job_memory_limit_mb
, which will automatically stop job processes if they go over a certain memory limit. • There's also
job_memory_warn_mb
, which helps by logging warnings when memory usage gets close to critical levels. • The memory monitor runs checks every 5 seconds and applies these limits across all processes, including audio streams, to help prevent issues. On top of that, the SDK takes care of task lifecycle management within
_ParticipantAudioInputStream._forward_task
by canceling previous tasks, flushing any pending audio frames, and handling pre-connect audio buffers if you've set that up. If you're looking for more detailed memory profiling or want to optimize within the audio processing pipeline itself, it might be helpful to add custom monitoring in your application or use platform-level tools to get a closer look at memory usage patterns. Please feel free to ask if you'd like guidance on setting any of this up or if you have more questions—I'm here to help! Sources: livekit-agents/livekit/agents/worker.py
s
On top of that, the SDK takes care of task lifecycle management within
_ParticipantAudioInputStream._forward_task
by canceling previous tasks, flushing any pending audio frames, and handling pre-connect audio buffers if you've set that up. how ? i still have cpu staircasing and memory accumulation
t
I'm glad you reached out! Let's take a closer look at the
_ParticipantAudioInputStream._forward_task
method in the LiveKit SDK together. This method manages audio frame forwarding and the task's lifecycle, but sometimes you might notice CPU staircasing and memory buildup under certain situations. Here are some helpful insights and suggestions to guide you: • Task Lifecycle: This method cancels any existing task before starting a new one. If you're experiencing frequent track subscription or unsubscription events (perhaps due to network instability), it can cause the task to restart repeatedly, which might explain those periodic CPU spikes you're seeing. • Memory Accumulation: If the component consuming
self._data_ch
processes frames more slowly than they are produced, this can cause a backlog in the channel and lead to memory growth. The SDK doesn’t currently set explicit buffer size limits here, so this is something to keep an eye on. • Infrastructure Sensitivity: If you’re running this on burstable EC2 instances (like t3 or t4), CPU throttling might occur, which can impact performance. For smoother and more consistent results, I recommend using dedicated instances such as
m5
or
c5
. Here are some friendly recommendations to help improve your experience: • Make sure that downstream consumers (like STT or VAD) are processing audio frames efficiently to avoid any channel backlog. • Keep an eye out for frequent task restarts and consider optimizing how you handle track state changes to reduce those CPU spikes. • Try to use dedicated infrastructure with enough resources — something like 4 CPU cores and 8GB RAM per worker is a good starting point for real-time processing. • Enable debug-level logging so you can observe task lifecycle events closely and spot any potential bottlenecks. • If frame processing is heavy, think about increasing buffer sizes or optimizing resampling logic to ease the load. For even more detailed monitoring, you might want to add custom instrumentation to track CPU and memory use of this task in your production environment. Please don’t hesitate to let me know if you’d like help walking through any of these steps — I’m here to support you! Sources: livekit-agents/livekit/agents/voice/agent_session.py | Troubleshooting Latency and Timeout Errors with Turn Detection on AWS | Deploying to custom environments | LiveKit Docs | Resolving "InvalidState - failed to capture frame" errors in AudioSource