when I update_agent in an AgentSession, what happe...
# ask-ai
w
when I update_agent in an AgentSession, what happens to the conversation context? I want to inject the new agent instructions as a system message appended to the context
t
Thanks for reaching out, Drew. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
When you use
update_agent
in an
AgentSession
, how the conversation context behaves really depends on how the new agent is set up: • Default Behavior: Usually, the new agent starts fresh without any previous conversation history—unless you’ve set it up to do otherwise. • Preserving Context: If you want to keep the conversation going smoothly, you can pass the current chat context to the new agent when you create it, like this:
Copy code
# Pass the chat context during handoff
new_agent = HelpfulAssistant(chat_ctx=session.chat_ctx)
session.update_agent(new_agent)
About adding new agent instructions as system messages: • These instructions get turned into system messages with a special ID and are placed right at the start of the chat context. • If there’s already an instruction message, it gets updated; if not, a new one is added. And don’t worry—the full session history is always stored in
session.history
, even if each agent only sees part of the context. If you want me to walk you through this or help with the setup, just let me know—I’m here to help! Sources: Workflows | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py