How to properly inject conversation history into a...
# ask-ai
j
How to properly inject conversation history into an AgentSession? We're building a voice agent that needs to have access to previous conversation history when a user reconnects. Currently, we're trying to provide this context by passing a pre-populated ChatContext to our Agent subclass: # Our current approach (which seems wrong) initial_ctx = llm.ChatContext() # Add previous messages to initial_ctx... assistant = CoraAssistant(chat_ctx=initial_ctx) session = AgentSession(...) await session.start(agent=assistant) However, we've discovered that: 1. The Agent instance is shared across multiple sessions/users 2. AgentSession maintains its own _chat_ctx that starts empty 3. Our Agent's initial context doesn't seem to reach the LLM during inference Questions: 1. What's the correct way to provide session-specific conversation history to an AgentSession? 2. Should we be modifying the AgentSession's _chat_ctx after creation, or is there a better pattern? 3. Is there a way to initialize an AgentSession with pre-existing conversation context without affecting other sessions? We want each user to have their own conversation history without it leaking to other users. What's the recommended approach for this use case?
t
Thanks for reaching out, Albert. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To provide session-specific conversation history to an
AgentSession
without affecting other sessions, follow these steps: 1. Create a ChatContext: Initialize a
ChatContext
object and populate it with the desired conversation history using the
add_message
method. Specify the role (e.g., "user" or "assistant") and content for each message. 2. Pass ChatContext to AgentSession: When starting an
AgentSession
, pass the
ChatContext
to the
Agent
instance. This ensures the session-specific context is used without affecting other sessions. 3. Use the update_chat_ctx Method: If you need to modify the context during a session, use the
update_chat_ctx
method to ensure changes are session-specific. Here’s an example:
Copy code
from livekit.agents import ChatContext, Agent, AgentSession

# Create ChatContext and add messages
chat_ctx = ChatContext()
chat_ctx.add_message(role="user", content="Hello!")
chat_ctx.add_message(role="assistant", content="How can I help you today?")

# Create Agent with ChatContext
agent = Agent(instructions="You are a helpful assistant.", chat_ctx=chat_ctx)

# Start AgentSession
session = AgentSession()
await session.start(agent=agent, room=...)
By following this approach, each user will have their own conversation history, ensuring no data leaks between sessions. Let me know if you need further clarification or assistance! Sources: OpenAI Realtime API integration guide | LiveKit Docs | Workflows | LiveKit Docs | Testing and evaluation | LiveKit Docs | livekit-agents/livekit/agents/voice/agent.py