How do i pass tool call history between two agents...
# ask-ai
s
How do i pass tool call history between two agents when using workflow
t
Thanks for reaching out, manik. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks for reaching out! Just to make sure I understand correctly, when you mention "tool call history," are you talking about transferring call logs, chat history, or maybe some other kind of data between agents? Also, are you using a particular feature or integration within LiveKit to handle this? I’m here to help you through this!
s
i have two agents with different set of tools. In addition to the conversation history, i also need to pass the tool call that happened in the first agent to the second
t
I'm happy to help you pass tool call history and conversation context between two agents in LiveKit workflows! Here's a friendly approach you can try: 1. Preserve Conversation History: You can use the
chat_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 Agents
s
can you show an example
t
Here is a helpful example showing how you can smoothly transfer tool call history and conversation context between two agents in LiveKit: Python Example:
Copy code
class 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!
s
when i transfer from 1 agent to another does the new agent make an LLM call or does that need to be specified in the "on_enter"?
t
I understand that the new agent doesn't automatically make an LLM call during a transfer, and I’m here to help you with that. To have the new agent generate a response right when it enters, you’ll want to explicitly specify this in the
on_enter
method. Here’s a simple example you can add to your
on_enter
method:
Copy code
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