Short-Term Memory, Dialogue State, and Context Compression: Engineering Long Conversations That Don't Break
After-sales conversations fail in a characteristic way: the user first gives a wrong order number and corrects it two turns later; mid-way they say "change the refund to a return"; the next day they come back asking "how did yesterday's case go?" If the system just dumps the whole chat history into the LLM, three accidents are likely: acting on the pre-correction order number, blending the refund and the return into one muddle, and starting from zero when they return the next day.
This post is part of an experience-sharing series for AI customer service product managers and developers, drawn from hands-on practice as an intelligent service product manager at a large general e-commerce platform. In this post I want to share how to design short-term memory, dialogue state management, context compression, and information verification for complex after-sales scenarios where users modify needs mid-conversation, correct earlier information, switch topics, or re-enter after an interruption — avoiding context pollution, information loss, and wrong reasoning.
This is context engineering — the most underrated hard skill for product managers in the LLM era.

Two Key Points in Memory and State Design
- Context engineering for long and cross-session conversations: how memory is layered, state persisted, history compressed.
- Understanding of engineering pain points like context pollution: the traps and fixes for corrections, topic switches, and interruption recovery.
The Overall Design
Step 1: Layered memory design
- Short-term memory: the raw text of the last N turns, keeping language coherent.
- Task memory: structured task state — each after-sales task's goal, collected slots, and executed actions, persisted as JSON and never dependent on raw conversation text.
- Long-term memory: user-level profile and preferences (usual delivery address, after-sales history), reused across sessions, governed by consent and privacy rules.
Step 2: State management and correction handling
- When users modify or correct information, have the model emit a state-change operation (update slot / undo step) executed by the state machine with versioning, never simply append the new information to chat history — that is the core defense against context pollution. With old and new information coexisting in history, the model may cite the stale one at any moment.
- Echo key information (order number, amount, delivery address) back to the user before execution, preventing actions based on pre-correction values. One echo costs far less than one wrong execution.
Step 3: Context compression
- When a session grows long, summarize-compress historical dialogue, but task slots and executed actions are never compressed or summarized — they are structured facts; summarizing them manufactures distortion.
- On topic switches, archive completed tasks' context, keeping only conclusions, reducing irrelevant noise for reasoning.
Step 4: Interruption recovery and verification
- Persist task state at the user dimension, outside the session ID, so when the user returns the next day the system recognizes unfinished tasks and offers to resume.
- Before every write operation, run triple verification: slot completeness check + real-time business state read + comparison with the user's last confirmed information; on mismatch, re-confirm rather than execute by default.
Step 5: Metrics and evaluation
Watch three metrics: cross-session task-resumption success rate, execution correctness after information correction, and error rate caused by context pollution (quantified via manual sampling and evaluation sets). I would proactively build adversarial test cases — "user changes their mind mid-way", "wrong order number then corrected" — because real users love creating exactly these situations.
One architectural insight
The dual-track of "structured state + raw conversation": raw dialogue serves the model's understanding; structured state serves business execution. Decoupling the two is the cornerstone of conversational reliability. Raw text can be compressed, summarized, or dropped; state must be exact, versioned, and auditable. Systems that manage both in a single context stream will break in long conversations.
How This Maps to Bytedesk
Bytedesk, the open-source AI customer service system (GitHub, site), provides the foundational components for this chain: conversation and message persistence supports short-term memory and cross-session history; visitor profiles and session records form the user-level long-term layer; the AI Agent's multi-turn dialogue and tool calls carry task-level state transitions; and human transfer syncs full context to agents so users never repeat themselves. Structured task state machines and slot-level verification are deeper customizations extendable on Bytedesk's open Agent framework.
Key Takeaways
- Three memory layers: short-term raw text for coherence, task state for facts, long-term profile for preference — each with its own job.
- Handle corrections with state-change operations + version records, never naive appends; echo-verify key info before execution.
- Compression applies only to raw dialogue; slots and executed actions are never summarized; archive on topic switch.
- Interruption recovery relies on user-dimension persistence; triple-verify before writes and re-confirm on mismatch.
- Decoupling raw text from structured state is the cornerstone of reliability — validated with adversarial tests for robustness.
