Compress a conversation without losing task constraints
In this article (5 sections)
Long conversations accumulate goals, formatting requirements, rejected choices, permissions and unresolved questions. A prose summary can shorten them while silently dropping the one constraint that prevents a harmful action.
Treat compression as a state transformation with invariants.
Preserve typed fields
The offline foundations lab defines five required values.
from foundation_cases import compression_case
result = compression_case()
assert result["passes"] is True
assert result["missing_keys"] == []
assert result["changed_values"] == []
print(result["compressed"])The compressed record retains the goal, citation requirement, 120-word limit, approval requirement and open policy-version question. This deterministic equality check covers an authored fixture; real conversations need richer schemas and review.
Separate durable state from dialogue
Store fields such as objective, user constraints, verified facts, source references, decisions, rejected options, tool results, approvals, pending questions and next action. Include provenance: which message established the value and whether it was later superseded.
Do not convert a proposal into an approval. “We could email the customer after review” must not compress to “email the customer.” Keep authorization scope, actor and expiry explicit. Secrets and unnecessary personal data should be excluded or protected according to the application policy.
Evaluate compression by downstream tasks
Create conversations with changed dates, negative constraints, nested exceptions and conflicting corrections. Compare the structured state before and after compression. Then run the next task using full and compressed context and measure constraint violations, answer support and token reduction.
When fields conflict, preserve both with their timestamps or escalate rather than choosing silently. Allow the user or reviewer to inspect and correct durable state in consequential workflows. Version the compression prompt and schema like any other component.
The Generative & Agentic AI course connects this pattern to agent state, checkpoints and recovery.
Exercise
Write a 20-turn synthetic conversation containing one correction, one prohibition, one approval and one open question. Compress it into a typed record, add invariants for all four and demonstrate that an intentionally bad summary fails.
Continue learning
This article is part of the LLM fundamentals and prompt design sequence. Use the neighbouring tasks when you need the prerequisite or the next application.
- Review the prerequisite or neighbouring task in Context ordering: test rather than assume.
- Continue with Separate model knowledge from current external information.
Reference: LangGraph persistence concepts.
Pankit Kumar has 10 years in Data Science & AI, building and shipping production systems in regulated pharma and clinical environments. He is a freelance trainer at Boston Institute of Analytics, AnalytixLabs and Scaler, and has taught this material to thousands of working professionals.
This article is part of our Generative & Agentic AI programme — 3 months. Add practical GenAI, retrieval and agent-building skills to your existing toolkit.
Explore Generative & Agentic AI