We have an issue that we sometime see that the age...
# ask-ai
g
We have an issue that we sometime see that the agent gets a fatal error from the LLM provider, and then we want to automatically restart the whole conversation, but we have not found a way to clear the old conversation's context so sometimes the agent becomes confused when we force it to restart because it has that previous history, and the System prompts gets totally lost and it looses its role etc. How can we clear all of the conversation's history and do a fresh restart without forcing the user to close down the room and start a totally new conversation
t
Thanks for reaching out, Jens. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hi there! To help you clear the conversation history and start fresh without closing the room, here are a few friendly methods you can try: • Update Chat Context Directly: If your model allows dynamic updates, you can easily clear the conversation history by creating an empty chat context and updating the session like this:
Copy code
# Create an empty chat context
empty_chat_ctx = llm.ChatContext.empty()

# Update the session's chat context directly
await session.update_chat_ctx(empty_chat_ctx)
• Session Replacement: For a more thorough reset, you might want to end the current session and start a new one with an empty context. Here's how you can do it:
Copy code
# End the current session
async def end_session(self):
    if self.current_agent is None or self.current_model is None:
        return
    await utils.aio.gracefully_cancel(self.current_model.sessions[0]._main_atask)
    self.current_agent = None
    self.current_model = None

# Replace with a new session
async def replace_session(self, ctx, participant, agent, model):
    await self.end_session()
    self.current_agent = agent
    self.current_model = model
    agent.start(ctx.room, participant)
• Agent Session Shutdown and Restart: Another way is to shut down the current session and start a new one with an empty context, like so:
Copy code
# Shutdown the current session
session.shutdown()

# Create a new session with empty context
new_session = AgentSession()
empty_chat_ctx = llm.ChatContext.empty()
agent = Agent(instructions="Your instructions here", llm=your_llm_model, chat_ctx=empty_chat_ctx)
await new_session.start(agent=agent, room=ctx.room)
For the easiest and quickest option, I recommend trying the first method if your model supports it. And of course, feel free to reach out if you'd like me to walk you through any of these steps or if you have any other questions. I'm here to help! Sources: livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti... | Workflows | LiveKit Docs | Real-time AI Models & Streaming | livekit/livekit_composite | DeepWiki | agent/main.py