I created an agent using code and deployed, the is...
# ask-ai
b
I created an agent using code and deployed, the issue I am facing is when a participant rejoins the conversation the agent is not persisting the conversation context and restarts the conversation from the beginning. how do I fix that?
t
Thanks for reaching out, Roni. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — I can help with that. You’re describing the agent restarting the conversation whenever a participant rejoins. That happens when the agent’s short-lived LLM chat context (
chat_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
Copy code
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
Copy code
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
Copy code
@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