creamy-eye-58634
07/31/2025, 7:18 PMtall-airline-16756
07/31/2025, 7:19 PMtall-airline-16756
07/31/2025, 7:19 PMfrom 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:
@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 Docscreamy-eye-58634
07/31/2025, 7:38 PMtall-airline-16756
07/31/2025, 7:38 PMchat_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:
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:
@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!creamy-eye-58634
07/31/2025, 7:39 PMtall-airline-16756
07/31/2025, 7:40 PMself.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.pycreamy-eye-58634
07/31/2025, 7:40 PMtall-airline-16756
07/31/2025, 7:41 PMchat_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:
from livekit.agents.llm.chat_context import ChatContext
chat_ctx = ChatContext()
1. Add Messages to ChatContext: Just add user and assistant messages like this:
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:
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!creamy-eye-58634
07/31/2025, 7:42 PMtall-airline-16756
07/31/2025, 7:42 PMchat_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:
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:
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:
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:
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...creamy-eye-58634
07/31/2025, 7:47 PMtall-airline-16756
07/31/2025, 7:48 PMcreamy-eye-58634
08/01/2025, 3:48 PM