Whenever I run workflows, my memory seems to going...
# ask-ai
w
Whenever I run workflows, my memory seems to going up and eventually crashing the instance. What should I be looking at ? I just followed the following recipe by livekit: https://github.com/livekit/agents/blob/main/examples/voice_agents/restaurant_agent.py more or less it is the same implementation. I create the userdata assign it to the session in agent.py and also attach the initial agent. What should be the pain points that I should be looking at to solve the memory issue. it doesn't happen in single agent flow.
t
Thanks for reaching out, jaswant. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd love to help you sort out the memory issue you're experiencing. To get a clearer picture, could you share a bit more about your setup? For example: • Are you running multiple agents at the same time? If yes, how many? • What kind of data do you keep in
userdata
? Also, do you clear or update it during the session? • Have you noticed any particular moments when memory usage spikes? • Are you using any custom changes to the
restaurant_agent.py
example? Knowing these details will really help us find the best way to resolve this together. Sources: Operational Best Practices | livekit/livekit_composite | DeepWiki
w
I keep a lot of data but all int or str , maybe some 40-50 keys with integers or strings. Earlier I used to store the Agents as well in the userdata but not anymore, I lazy load them and transfer without keeping them in userdata. at any moment of time only one agent would be active. Custom changes as in , I have made changes to suit our flow but the structure is more or less same
t
I understand that managing memory in your workflow can be a bit tricky, so here are some friendly tips to help optimize it: • Optimize UserData: Try using a structured
dataclass
for your
userdata
. This way, related data stays nicely grouped together, and you can avoid accidentally sharing mutable data. For instance, using
field(default_factory=list)
for collections ensures each instance is independent. • Lazy Loading: It’s great that you’re already using lazy loading! Keep fetching or constructing agent data only when it’s really needed—this helps keep the initial load light. • Minimize Data Retention: Regularly tidying up unused or outdated data in your
userdata
can make a big difference. Try to avoid holding onto large or unnecessary data longer than needed. • Pass Context During Agent Handoff: When switching between agents, passing along the existing
userdata
helps keep everything seamless without creating duplicate states. • Store References: Instead of copying big data objects, keeping references to shared resources can really help reduce memory use. I hope these suggestions make your multi-agent workflow smoother and lighter on memory. If you want to dive deeper into any of these or need a hand with implementation, just let me know—I’m here to help! Sources: Workflows | LiveKit Docs | lerobot/common/utils/buffer.py | VoiceAgent/Chat/Receive/TranscriptionStreamReceiver.swift | context-readmes/USERDATA_GUIDE.md | main.py
w
async def _transfer_to_agent(self, context: RunContext_T, agent_key: str) -> Agent: userdata = context.userdata current_agent = context.session.current_agent # Store the current agent key (name) instead of the agent instance if current_agent: userdata.prev_agent = self._get_agent_key_from_class(current_agent.class) else: userdata.prev_agent = None # Create agent directly - no need to store in userdata return _create_agent_by_key(agent_key, chat_ctx=self.chat_ctx) this is how my handoff function looks like
t
I'm really glad to see your handoff function implementation is already following great practices for memory optimization! To help you take it even further, here are some friendly tips that might make your memory usage even more efficient: • Truncate Chat History: Try limiting the size of the chat context during agent handoff to keep memory from growing too large. For instance, you could keep just the last few messages by using a truncate method. • Selective Context Merging: When you merge chat contexts, consider leaving out message types that aren’t essential (like instructions or function calls) to lighten the memory load. • Summarize Context: Instead of passing the entire conversation history, creating concise summaries of the key points can be really helpful. Including these summaries in system messages for the new agent can make things smoother. • Stateless Agents: It’s great that you’re keeping agents stateless by storing session-specific data in
userdata
. This approach keeps your agents lightweight and easy to scale. • Clean Transitions: Using lifecycle methods such as
onEnter()
to initialize agents with well-managed context helps avoid duplicating data and keeps transitions clean. Following these steps should make your memory usage more efficient and help prevent crashes in multi-agent workflows. If you’d like me to explain anything further or assist with putting these into practice, just let me know—I’m here to help! Sources: main.py | Tool definition and use | LiveKit docs | Building Multi-Agent Architectures with LiveKit Agents | Workflows | LiveKit Docs | README.md