how to clear AgentSession chat history clear messa...
# ask-ai
s
how to clear AgentSession chat history clear messages context
t
Thanks for reaching out, Rahul. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To clear the chat history or reset the message context in LiveKit's
AgentSession
, here are a couple of friendly ways you can do that: • Use
update_chat_ctx()
with an Empty Context:
This is the best and simplest way! Just call
update_chat_ctx()
on your agent and pass in a new, empty
ChatContext
. This will clear out the conversation history moving forward, giving you a fresh start.
Copy code
from livekit.agents import llm

# Create an empty chat context
empty_context = llm.ChatContext.empty()

# Update the agent's chat context (this clears the history)
await agent.update_chat_ctx(empty_context)
• Start a New `AgentSession`: If you want to reset not just the chat history but also the agent’s state or behavior, you can start a brand new
AgentSession
with a fresh agent and an empty
ChatContext
. This gives you a clean slate all around.
Copy code
# Start a new session with a clean chat context
new_session = AgentSession()
new_agent = Agent(chat_ctx=llm.ChatContext.empty())

await new_session.start(agent=new_agent, room=room)
If you have any questions or need a hand with these steps, just let me know—I’m here to help! Sources: Agents v0.x migration guide - Python | LiveKit Docs | livekit-plugins/livekit-plugins-google/livekit/plugins/google/realtime/real... | Pipeline nodes and hooks | LiveKit Docs | agents/src/voice/agent_session.ts | tools/mtmd/mtmd-cli.cpp