chilly-nest-87883
07/21/2025, 10:26 PMtall-airline-16756
07/21/2025, 10:26 PMtall-airline-16756
07/21/2025, 10:27 PMkeep_function_call=True when using `_truncate_chat_ctx`:
if userdata.prev_agent:
items_copy = self._truncate_chat_ctx(
userdata.prev_agent.chat_ctx.items,
keep_function_call=True, # Preserve function calls
keep_last_n_messages=200
)
existing_ids = {item.id for item in chat_ctx.items}
items_copy = [item for item in items_copy if item.id not in existing_ids]
chat_ctx.items.extend(items_copy)
• Or, set exclude_function_call=False when using `copy`:
if isinstance(userdata.prev_agent, Agent):
truncated_chat_ctx = userdata.prev_agent.chat_ctx.copy(
exclude_instructions=True,
exclude_function_call=False # Preserve function calls
).truncate(max_items=6)
existing_ids = {item.id for item in chat_ctx.items}
items_copy = [item for item in truncated_chat_ctx.items if item.id not in existing_ids]
chat_ctx.items.extend(items_copy)
Manual addition of function calls is not required if you use these built-in context copying mechanisms with the correct parameters. This will ensure that all function calls and their outputs are preserved for new agents, maintaining the necessary context for multi-agent workflows.
The complete conversation history is also available in session.history if you need to reference it directly.
Sources: complex-agents/personal_shopper/personal_shopper.py | livekit-agents/livekit/agents/voice/agent.py | Agents v0.x migration guide | LiveKit Docs | Workflows | LiveKit Docschilly-nest-87883
07/21/2025, 10:35 PMtall-airline-16756
07/21/2025, 10:36 PMchat_ctx=chat_ctx works during agent handoff. It's pretty cool - this method keeps all your chat history intact, including those function calls and responses. So no worries about losing important context when switching agents!
But, I want to give you a heads up about a few things to keep in mind:
• Context Length Management: While keeping the whole chat history sounds great, it can get a bit overwhelming as your conversation grows. Most production systems actually trim things down to keep just the most relevant messages.
• Orphaned Function Calls: Without some smart trimming, you might end up with some lonely function calls floating around at the start of your history - not ideal!
• Duplicate Prevention: When you're merging contexts, using explicit copying lets you avoid repeating stuff by checking item IDs. Super helpful!
• Selective Filtering: The copy() method is your friend here - it lets you get super precise about what context you want to keep.
Your current approach totally works, but if you're looking to level up your multi-agent system, I'd recommend giving explicit copying and truncation a try. It'll help you keep things tidy, manageable, and running smoothly as your system grows. Trust me, your future self will thank you!
Sources: complex-agents/medical_office_triage/README.md | livekit-agents/livekit/agents/voice/agent.pychilly-nest-87883
07/21/2025, 10:38 PMtall-airline-16756
07/21/2025, 10:38 PM