stale-toddler-98427
11/01/2025, 6:11 AMtall-airline-16756
11/01/2025, 6:11 AMtall-airline-16756
11/01/2025, 6:11 AMstale-toddler-98427
11/01/2025, 6:12 AMtall-airline-16756
11/01/2025, 6:12 AMchat_ctx.copy() method to smoothly transfer the chat context from the first agent to the second. This way, the conversation history, including tool calls, stays intact.
2. Transfer Tool Call Data: When copying the chat context, make sure to set exclude_function_call=False. This helps keep all tool interactions preserved for the next agent.
3. Use Shared User Data: It's a great idea to store structured session data—like customer details and tool outputs—in a shared UserData object. This allows both agents to access and update the same information effortlessly.
4. Agent Handoff: You can use a @function_tool-decorated function to trigger the handoff. This function will pass the current context to the next agent, making the transition smooth.
5. Context Personalization: To keep things personalized, use the UserData.summarize() method to inject session details into the new agent’s system prompt. This helps maintain continuity in the conversation.
If you'd like, I’m here to provide more details or examples to help you implement this workflow successfully. Just let me know!
Sources: Session recording and transcripts | LiveKit Docs | examples/warm-transfer/warm_transfer.py | complex-agents/personal_shopper/personal_shopper.py | examples/src/comprehensive_test.ts | Building Multi-Agent Architectures with LiveKit Agentsstale-toddler-98427
11/01/2025, 6:17 AMtall-airline-16756
11/01/2025, 6:17 AMclass BaseAgent:
def on_enter(self, userdata):
# Check if there is a previous agent
if isinstance(userdata.prev_agent, Agent):
# Copy and truncate the previous agent's chat context
truncated_chat_ctx = userdata.prev_agent.chat_ctx.copy(
exclude_instructions=True, exclude_function_call=False
).truncate(max_items=6)
# Avoid duplicating messages
existing_ids = {item.id for item in self.chat_ctx.items}
items_copy = [item for item in truncated_chat_ctx.items if item.id not in existing_ids]
# Extend the current agent's chat context
self.chat_ctx.items.extend(items_copy)
# Personalize the system prompt with user data
self.chat_ctx.add_message(
role="system",
content=f"You are the {self.__class__.__name__}. {userdata.summarize()}"
)
# Function to transfer to another agent
def transfer_to_agent(name, context):
userdata = context.userdata
current_agent = context.session.current_agent
next_agent = userdata.agents[name]
# Save the current agent as the previous agent
userdata.prev_agent = current_agent
# Perform the handoff
return next_agent
Key Points:
• The on_enter method thoughtfully ensures that the chat context, including tool calls, is passed along to the new agent.
• The transfer_to_agent function manages the handoff by saving the current agent as the previous one and smoothly passing the context to the next agent.
• Tool call data stays intact by setting exclude_function_call=False when copying the chat context.
If you have any questions or would like me to walk you through more examples, please don’t hesitate to ask—I’m here to help!stale-toddler-98427
11/01/2025, 7:35 PMtall-airline-16756
11/01/2025, 7:35 PMon_enter method.
Here’s a simple example you can add to your on_enter method:
def on_enter(self, userdata):
# Update chat context and other setup
...
# Trigger an LLM response
self.session.generate_reply(tool_choice="none")
This way, the LLM will generate a reply once the agent is initialized and the context is updated. If you have any questions or need a hand with implementing this, please don’t hesitate to reach out—I’m happy to assist!
Sources: livekit-agents/livekit/agents/voice/agent.py