What is the correct way to switch agents using an ...
# ask-ai
c
What is the correct way to switch agents using an RPC in LiveKit?
t
Thanks for reaching out, Akshay. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You can switch agents in LiveKit using two main approaches: 1. Using a Tool Function: Return a new agent instance from a tool function. For example:
Copy code
from livekit.agents import function_tool

class CurrentAgent(Agent):
    @function_tool
    async def switch_to_new_agent(self, context: RunContext):
        """Switch to a different agent"""
        return NewAgent()
When this function is called, LiveKit automatically hands off control to the new agent. 1. Using RPC: Register an RPC method to handle the switch and perform the RPC call to trigger it. For example:
Copy code
@room.local_participant.register_rpc_method("switch_agent")
async def handle_switch_agent(data: RpcInvocationData):
    """Handle agent switch request via RPC"""
    new_agent = NewAgent()
    session.update_agent(new_agent)
    return "Agent switched successfully"

response = await room.local_participant.perform_rpc(
    destination_identity='agent-identity',
    method='switch_agent',
    payload='switch_to_new_agent'
)
Choose the method that best fits your use case. Let me know if you need further clarification or tailored examples! Sources: Workflows | LiveKit Docs
c
How can i pass chat_ctx while switching agent in rpc as well in tool
t
Hey there! If you want to pass
chat_ctx
while switching agents in both RPC and tool functions, here’s a simple way to do it: 1. Passing
chat_ctx
in Tool Functions:
When you’re returning a new agent, just include the
chat_ctx
parameter using
self.session.chat_ctx
. Like this:
Copy code
return NewAgent(chat_ctx=self.session.chat_ctx)
This way, the new agent will have access to the full conversation history — super handy! 1. Passing
chat_ctx
in RPC:
Make sure to explicitly use the
chat_ctx
parameter when handling the RPC method. For example:
Copy code
@room.local_participant.register_rpc_method("switch_agent")
async def handle_switch_agent(data: RpcInvocationData):
    new_agent = NewAgent(chat_ctx=self.session.chat_ctx)
    session.update_agent(new_agent)
    return "Agent switched successfully"
In both cases, using
self.session.chat_ctx
keeps the conversation flowing smoothly. If you want me to walk you through it more or need extra examples, just let me know — I’m here to help!
c
self.session?
t
In LiveKit,
self.session
is basically the session context that your agent works with. It’s super handy and offers a bunch of useful features, like: • Managing Conversation Flow: You can use methods like
generate_reply()
to send responses or smoothly switch between agents. • Preserving Context: The session keeps track of the conversation history and lets you update it with
update_chat_ctx()
. • Tool Execution: It allows you to connect with function tools to handle specific tasks or workflows. • Event Handling: It listens for things like user input or metrics, so your agent can respond dynamically. • State Preservation: Keeps session-specific data safe, making transitions between agents seamless. For example, when you switch agents,
self.session.chat_ctx
makes sure the new agent can see the whole conversation history. Feel free to ask if you want me to explain more or share some examples! Sources: livekit-agents/livekit/agents/voice/agent_activity.py | flows/simple_flow.py
c
I am not using class based rpc or tool...
t
If you're not using class-based RPC or tool functions, no worries! You can still pass
chat_ctx
in LiveKit by managing the conversation context yourself. Here's a simple way to do it: 1. Create a ChatContext Instance: Start by initializing a
ChatContext
object to keep track of your conversation history:
Copy code
from livekit.agents.llm.chat_context import ChatContext

chat_ctx = ChatContext()
1. Add Messages to ChatContext: Just add user and assistant messages like this:
Copy code
chat_ctx.add_message(role="user", content="Hello, how can I assist you today?")
chat_ctx.add_message(role="assistant", content="I'm here to help!")
1. Pass ChatContext During Agent Initialization: When you create your agent, simply pass in the
chat_ctx
instance:
Copy code
from livekit.agents import Agent

class MyAgent(Agent):
    def __init__(self, chat_ctx: ChatContext):
        super().__init__(
            instructions="You are a helpful AI assistant.",
            chat_ctx=chat_ctx
        )
This way, you can keep your conversation context going smoothly without needing class-based setups. If you want me to walk you through it more or share some examples, just let me know—I'm happy to help!
c
any other way dynamic?
t
Here’s a friendly way to keep your
chat_ctx
dynamic in LiveKit, using a few handy methods: 1. Using the
update_chat_ctx
Method:
You can update the chat context on the fly while things are running:
Copy code
await self.update_chat_ctx(chat_ctx)
This lets you tweak the context during live sessions, which is pretty neat! 1. Passing
chat_ctx
During Agent Handoff:
When you’re handing off to a new agent, just pass along the current chat context like this:
Copy code
new_agent = NewAgent(chat_ctx=current_chat_ctx)
1. Explicit Context Construction: You can build or change a
ChatContext
anytime based on what you need:
Copy code
chat_ctx = ChatContext.empty()
chat_ctx.add_message(role="user", content="Tell me a story")
chat_ctx.add_message(role="assistant", content="Once upon a time...")
1. Realtime Updates: For sessions that keep going, you can update the context dynamically like this:
Copy code
await self._rt_session.update_chat_ctx(chat_ctx)
These options give you lots of flexibility to manage and pass
chat_ctx
smoothly. If you want me to walk you through any part or share more examples, just let me know—I’m here to help! Sources: livekit-plugins/livekit-plugins-google/livekit/plugins/google/beta/realtime...
c
session.update_agent(MyBOt(chat_ctx=session.chat_ctx)) ^^^^^^^^^^^^^^^^ AttributeError: 'AgentSession' object has no attribute 'chat_ctx'
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!
c
Rpc based function tool calling... Note tool is in MyBot class agent