I'm using LiveKit Agents and I notice that session...
# ask-ai
s
I'm using LiveKit Agents and I notice that session.history retains all assistant messages across turns, but only keeps the most recent user message. Previous user responses seem to get lost or overwritten. Is this expected behavior? Here's my relevant code: python Apply to console_test... async def on_user_turn_completed(self, _turn_ctx_, _new_message_): # Create evaluation context eval_chat_ctx = ChatContext() eval_chat_ctx.add_message(role="system", content=self.system_prompt) # Add conversation history from session.history for item in self.session.history.items: if hasattr(item, 'role') and hasattr(item, 'text_content'): if item.role in ['assistant', 'user']: eval_chat_ctx.add_message(role=item.role, content=item.text_content) # Add current user response eval_chat_ctx.add_message(role="user", content=new_message.text_content) What I'm seeing in the conversation history: text Apply to console_test... ASSISTANT: Question 1 ASSISTANT: Question 2 ASSISTANT: Question 3 USER: [Only the most recent response - responses to Q1 and Q2 are missing] Expected behavior: text Apply to console_test... ASSISTANT: Question 1 USER: Response to Q1 ASSISTANT: Question 2 USER: Response to Q2 ASSISTANT: Question 3 USER: Response to Q3 How should I maintain the complete conversation history including all user responses across multiple turns for an interview agent that needs to see all previous responses to make decisions?
t
Thanks for reaching out, Jason. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To keep the full conversation history, including every user response, you can use the
session.history
property. It automatically captures all the chat items as they happen—both from the user and the assistant—and updates in real time. Here are a few helpful tips to make sure the history stays intact: • Use the
conversation_item_added
event to catch each new message added to the chat history. • Double-check that your code isn’t overwriting or skipping user messages when looping through
session.history.items
. • If you want to save the conversation after the session ends, use the
add_shutdown_callback
method to store the entire history in a file or database. • When switching between agents, pass the
chat_ctx
from one agent to the next using the
Agent
or
AgentTask
constructor to keep all the previous context. For your specific code, make sure the loop over
session.history.items
properly handles every user message without accidentally skipping or overwriting anything. If you want me to walk through it with you or need a hand, just let me know! Sources: Workflows | LiveKit Docs | Sending text | LiveKit Docs