mysterious-computer-23868
07/22/2025, 4:55 PMAgentSession, 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.
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"])tall-airline-16756
07/22/2025, 4:55 PMtall-airline-16756
07/22/2025, 4:55 PMChatContext 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:
# 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:
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