By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A support agent finishes a forty-turn conversation with one customer. At turn two, the record states a refund cap of 40 US dollars. At turn thirty, the agent offers 120. No tool failed. The loop ended on end_turn, exactly as designed. The transcript contains no error at any point.
The fact was still in the conversation history, but it was no longer in the prompt. Compaction, which replaces old turns with a shorter summary so a request fits the window, had collapsed turns one to twenty-five into six sentences. The summary kept the complaint and dropped the number. This lesson is about keeping the facts a later turn depends on.
The context window is everything the model can read in one request: the system prompt, the conversation so far, the tool results and the current instruction. A long window does not give every position the same influence. Models attend most reliably to the start and the end of what they read. They attend least reliably to material buried in the middle. People call this lost in the middle.
That bias would be manageable if the transcript stayed intact. Compaction makes it worse. The summary is written once, then trusted on every later turn. Prose summaries keep the story and discard the parameters. A number, an identifier or a negative rule such as never refund above the cap carries no narrative weight, so it is among the first details to go.
| Where the fact sits | What happens over forty turns | How far to trust it |
|---|---|---|
| Pinned block at the start | Sent unchanged on every request | High, if nothing rewrites it |
| Middle of the transcript | Buried, then summarised away | Low: the classic lost-in-the-middle case |
| Recent turns at the end | Present, but only for a few turns | High until it scrolls out of the window |
| Summary of old turns | Rewritten by a model, lossy by design | Medium: read it, do not trust it blindly |
A fact survives when two things are true. Something places it near an edge of the prompt, and something checks that it is still there after compaction. The diagram shows four positions in one conversation, and the one failure worth designing against.
This snippet builds the prompt in the order the table describes. It stores durable facts as JSON, so a retry rewrites the file instead of appending a duplicate. It uses plain Python and calls no model, so you can run it as written.
import json
from pathlib import Path
# Durable facts live in a file, not in the chat transcript.
SCRATCHPAD = Path("state") / "case-4471.json"
def load_facts(path: Path) -> dict:
if not path.exists():
return {}
return json.loads(path.read_text(encoding="utf-8"))
def save_facts(path: Path, facts: dict) -> None:
# Rewrite the whole file so a retry cannot append a duplicate fact.
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(facts, indent=2, sort_keys=True), encoding="utf-8")
def assemble(system: str, facts: dict, summary: str, recent: list[str]) -> str:
# Rules and facts first, recent turns last: both edges are read closely.
head = [
system.strip(),
"",
"## Case facts",
json.dumps(facts, sort_keys=True),
"",
"## Summary of earlier turns",
summary,
]
tail = ["", "## Recent turns", *recent]
return "\n".join(head + tail)
facts = {"order": "A4471", "refund_cap_usd": 40, "tier": "gold"}
prompt = assemble(
system="Never refund more than the case refund_cap_usd.",
facts=facts,
summary="Customer reported a late delivery twice; a 10 USD credit was already offered.",
recent=["customer: this is the third time", "agent: checking order A4471"],
)
print(prompt)Read the order in that output. Rules come first, then facts, then the summary, then the most recent turns. The cap no longer depends on a summariser choosing to keep it.
The opening failure has a cheap test. Take ten real transcripts. For each one, list the five facts a correct answer needs. Run your compaction step, then ask a model to answer from the compacted prompt alone. Count how many of the fifty facts survive. That count, not the transcript length, tells you whether compaction is safe.
Take a conversation of at least twenty turns, from your logs or written by hand. List the five facts a correct answer needs. Write them into a scratchpad, assemble the prompt with the function above, then replace the recent turns with a two-sentence summary and assemble again. Produce a table with three columns: fact, where it sits, survived. You are done when every critical fact appears in the pinned block or the scratchpad, and your table shows that it survived the cut.
Key takeaways
Models attend unevenly: the start and end of a long prompt are more reliable than the middle.
A prose summary keeps the story and drops the parameters, so compaction is lossy by design.
Pin critical facts at the start, and put the current question at the end.
A scratchpad file holds durable state that a summary cannot lose.
Test compaction by counting which required facts survive, not by reading the summary.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.