State Management in Multi-Turn Dialogue: Session State, Context Memory, and Persistence Design
User: "Check my order." AI: "Sure, please provide the order number." User: "Did you find it?" AI: "Which order do you want to check?" — What this exchange exposes is not a dumb model but missing session state. The order number the user gave five minutes ago, the logistics issue they discussed — the model forgets instantly. Experience gaps in multi-turn dialogue come from state management far more often than from model capability.
This is the twelfth post in an experience-sharing series for AI customer service product managers and developers, drawn from hands-on practice as an AI product manager at a Southeast Asian cross-border e-commerce platform. In this post I want to share how to design session state, context memory, key information extraction, and state persistence for multi-turn dialogue where a user first queries an order, then follows up on a logistics exception and refund conditions — avoiding context loss, repeated questions, and task-state confusion.

Three Key Points in Multi-Turn State Management
- Dialogue state tracking: structuring "where the conversation is" instead of leaning entirely on raw history.
- Key slot design: how critical entities like order numbers are stored, reused, and reconciled on conflict.
- Persistence and long-conversation handling: state must survive disconnections, device switches, and marathon sessions.
The Overall Design
1. Session state design: structured slots + explicit task state
Maintain a structured state object per session: user identity, current order ID, confirmed slots (tracking number, issue type), the current task stack (query order → logistics exception → refund inquiry), and each task's completion status.
Key information (order number, tracking number) is written to state and persisted on first acquisition, then reused for follow-ups — never asked twice. The current state summary is explicitly injected into the model's context, rather than dumping raw history and hoping the model infers it.
2. Layered context memory: short-term verbatim + long-term summary
- Short-term memory: the most recent N turns verbatim, preserving coreference resolution ("this order," "when will it arrive");
- Long-term memory: session-level summary + key entity table; history beyond the window is compressed into summaries so critical information doesn't slide out of the window;
- Update summaries and slots asynchronously after each turn, not on-the-fly during generation — otherwise you pay that computation cost every turn.
3. Key information extraction: incremental per turn + explicit conflict confirmation
Each turn, use a lightweight model to extract entity and intent changes (a new order number, a new follow-up topic) and merge them into state; on conflict (the user switches orders), confirm explicitly with the user instead of silently overwriting — when the user says "what about the other one," the system must confirm which order they mean.
4. State persistence: survive interruptions
- Persist session state (Redis hot storage + database), supporting recovery across devices, reconnections, and service restarts;
- Attach a TTL; after long-session expiry, re-onboard the user while retaining identity and order history;
- On handoff, pass the full state along with the ticket so users never retell the story — the dividing line of human-AI collaboration experience.
5. Consistency: tasks must not tangle
On task switches, check whether prerequisite tasks completed (user asks about refunds without an order on file → collect order info first); before each generation, verify the answer is consistent with current state, preventing already-solved problems from being reprocessed.
How This Maps to Weiyu
The Weiyu open-source customer service system (GitHub: https://github.com/bytedesk/bytedesk , website: https://www.weiyuai.cn ) provides solid foundations here: threads and messages are fully persisted, so conversations survive reconnections and device switches; AI agents and human agents share the same conversation timeline, so on handoff agents see the complete context; conversations can be associated with tickets and visitor profiles, letting multi-turn requests settle into ticket workflows. For teams needing a deeply customized slot state machine, Weiyu's open-source architecture (Spring Boot + Redis) offers clear entry points for secondary development.
Key Takeaways
- Structured state + task stack is the skeleton of multi-turn dialogue; don't make the model fight from raw history alone.
- Layered memory: short-term verbatim preserves coreference; long-term summaries stop information sliding out of the window.
- Capture key slots once, reuse throughout; confirm conflicts explicitly rather than silently overwriting.
- State persistence + full handoff transfer is what keeps experience seamless.
- Validate state consistency before each generation to prevent reprocessing solved problems.
