busy-elephant-27084
02/13/2026, 4:45 PMtall-airline-16756
02/13/2026, 4:45 PMtall-airline-16756
02/13/2026, 4:46 PMchat_ctx) is recreated without restoring the durable session state. You can fix it by persisting the full conversation in session.userdata and rebuilding the agent’s chat_ctx from that stored history when the participant reconnects.
Below is a concise plan and example steps you can follow.
What to do (high level)
• Use session.userdata as your durable conversation store (source of truth).
• Append every incoming and outgoing message to session.userdata as the conversation proceeds.
• On reconnect / agent restart, rebuild chat_ctx from the persisted session.userdata before starting the agent so the LLM sees prior messages.
• Do not create a fresh chat_ctx without first populating it from userdata.
Step-by-step example (pattern to implement)
1. Define a durable session state
from dataclasses import dataclass, field
from typing import List, Dict
@dataclass
class SessionState:
full_history: List[Dict] = field(default_factory=list)
1. Append each message to session.userdata
async def on_user_message(self, ctx: RunContext, message: ChatMessage):
state: SessionState = ctx.session.userdata # persistent across reconnects
state.full_history.append({
"role": message.role,
"content": message.text_content()
})
# proceed with usual agent handling...
1. Rebuild the agent’s chat_ctx on reconnect
@server.rtc_session()
async def entrypoint(ctx: JobContext):
# ensure session state exists
if not hasattr(ctx.proc.userdata, 'session_state'):
ctx.proc.userdata['session_state'] = SessionState()
state = ctx.proc.userdata['session_state']
# create and populate chat context from persisted history
chat_ctx = ChatContext()
for msg in state.full_history:
chat_ctx.add_message(role=msg["role"], content=msg["content"])
# start the agent with restored chat_ctx so it retains prior context
agent = MyAgent()
await session.start(agent=agent, room=ctx.room, chat_ctx=chat_ctx)
1. Use session.history as an additional canonical log (optional)
• You can also read context.session.history after reconnects to inspect or rebuild conversation state if useful.
Quick checks if this still doesn’t work
• Confirm you are actually writing to session.userdata (no local-only variable).
• Confirm the code path that starts the agent on reconnect is reconstructing chat_ctx from the persisted userdata.
• Avoid any code that intentionally resets or truncates chat_ctx before reconstruction.
If you’d like, share:
• The short snippet that initializes chat_ctx or starts the agent after reconnect, and
• Where you currently store messages,
and I’ll point out exactly where to persist or restore state in your code.
Sources: Building multi-agent architectures with LiveKit agents | LiveKit | Agents and handoffs | LiveKit Documentation