I'm trying to receive past agent/user chat history...
# ask-ai
m
I'm trying to receive past agent/user chat history from my front-end, and add this to the initial chat context so that the user can "resume" the conversation. With
AgentSession
, and
aws.realtime.RealtimeModel
as my llm, my code is quite simply and everything is done in entrypoint for now. However the agent doesn't seem to be getting the context from the past conversation, even though I can see it's being received in the metadata. Am I loading the chat context the right way? I'm wondering if I'm using the wrong method, or if it's just getting wiped somehow.
Copy code
async def entrypoint(ctx: agents.JobContext):

  <http://logger.info|logger.info>(f"Connecting to room {ctx.room.name}")
  await ctx.connect()

  participant = await ctx.wait_for_participant()
  <http://logger.info|logger.info>(f"Starting voice assistant for participant {participant.identity}")

  session = AgentSession(
    llm=aws.realtime.RealtimeModel(
      voice="tiffany",
      region="us-east-1",
      temperature=0.7,
      top_p=0.95,
    ),
  )

  await session.start(
    room=ctx.room,
    agent=Kyorla(),
    room_input_options=RoomInputOptions(
      noise_cancellation=noise_cancellation.BVC(),
    ),
    room_output_options=RoomOutputOptions(
      sync_transcription=True,
    ),
  )

  history = json.loads(participant.metadata).get("chat_history", [])
  <http://logger.info|logger.info>(f"HISTORY: {history}")
  history_ctx = session.history.copy()
  for msg in history:
    history_ctx.add_message(role=msg["role"], content=msg["content"])
t
Thanks for reaching out, Ben. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To ensure the agent receives prior chat context and can "resume" conversations, use one of these supported methods: • Preferred: Build a
ChatContext
with your chat history and pass it to the
Agent
constructor before starting the session. This guarantees the LLM receives the full context from the start. • Alternative: If you need to update context after the session has started, use the
update_chat_ctx()
method on the agent to inject prior messages. Directly modifying
session.history
after
session.start()
is not a supported or reliable way to provide context to the LLM. The recommended approaches are:
Copy code
# Build ChatContext from your history
chat_ctx = ChatContext.empty()
for msg in history:
    chat_ctx.add_message(role=msg["role"], content=msg["content"])

# Pass chat_ctx to the Agent
agent = Agent(
    llm=aws.realtime.RealtimeModel(...),
    chat_ctx=chat_ctx
)

await session.start(agent=agent, room=ctx.room)
Or, after starting the session:
Copy code
chat_ctx = ChatContext()
for msg in history:
    chat_ctx.add_message(role=msg["role"], content=msg["content"])
await agent.update_chat_ctx(chat_ctx)
Switching to one of these methods will ensure the LLM receives the intended prior context and can resume conversations as expected. Sources: OpenAI Realtime API integration guide | LiveKit Docs