In LiveKit Agents, how do I maintain and access th...
# ask-ai
s
In LiveKit Agents, how do I maintain and access the full conversation history across multiple turns? Currently, when I use on_user_turn_completed(turn_ctx), the turn_ctx only contains the current turn's messages, not the complete conversation history from the start of the session. I need to pass the full conversation history to my LLM on each turn for context-aware decision making. Is there a built-in way to access the complete conversation history, or do I need to manually store and maintain it in my agent class?
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 track of and access the full conversation history in LiveKit Agents over multiple turns, you can use the handy
session.history
property. This holds the entire conversation as a
ChatContext
object, and it updates automatically as your chat goes on. Here’s a quick rundown on how to use it: • Accessing History: Just grab
session.history
whenever you want to see the full conversation so far during the session. • Preserving Context: To keep the context flowing across turns or when switching agents, simply pass the current
chat_ctx
to the new agent or task, like this:
return TechnicalSupportAgent(chat_ctx=self.session.chat_ctx)
. • Real-Time Updates: You can listen for events like
conversation_item_added
to get notified whenever something new pops up in the chat history. • Saving and Loading: After the session wraps up, save
session.history
to a file (like JSON), and later reload it by rebuilding the
ChatContext
and passing it back to the agent when you start again. If you want me to walk you through any part or share some examples, just give me a shout! Sources: Session recording and transcripts | LiveKit Docs | Workflows | LiveKit Docs | Testing and evaluation | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_session.py | OpenAI Realtime API integration guide | LiveKit Docs