Lesson 55.3 · 3 of 4 · 4 min read · Reviewed 24 Nov 2026
What to keep in context and what to store
By the end, you can
- Decide what stays in context and what goes to storage.
- Apply a simple keep, summarise, store, drop rule.
A simple rule
| Item | Decision |
|---|---|
| Needed for the next few steps | Keep in context |
| Was useful, now only the gist matters | Summarise in place |
| Might matter in a later session | Store in long-term memory |
| No longer useful | Drop |
Applied to an agent run
| Item | Decision |
|---|---|
| System prompt and tools | Keep |
| Case facts (ids, amounts) | Keep, pinned |
| A large file read, already used | Summarise: “config.ts sets timeout 30s, line 42” |
| Search results already turned into notes | Drop, notes kept |
| User preference learned (“no emojis”) | Store as semantic memory |
| What was tried and failed | Store as episodic memory |
Claude features that help
- Context editingContext editing: Removing or replacing parts of an agent’s context, such as old tool results, while it runs. can clear old tool results automatically as the context grows (chapter 58).
- The memory tool gives Claude a file-like store to save and read notes across sessions (56.3).
- CompactionCompaction: Summarising older conversation history so a long session fits in the context window while keeping what matters. in Claude CodeClaude Code: Anthropic’s agentic coding tool that works in your terminal, IDE and CI, reading and editing code and running commands. summarises long sessions (chapter 60).
End of session
Before a session ends, ask: what should the next session know? Write that to memory. Then the next session starts small and focused instead of reloading everything.
Key points
- Keep in context what the next steps need; store what may matter later; drop what won't.
- Large tool results can be summarised or replaced with a pointer once used.
- Durable facts and decisions move to long-term memory at the end of a session.
Triage context items: keep, summarise, store, drop
Apply the keep, summarise, store, drop rule to the items in a half-finished agent run and measure the space freed.
You will have: A triage function, a printed table of decisions and the tokens freed.
Steps
- Read the ITEMS list: each has a kind, a size in tokens and flags for whether it is needed next or used already.
- Write triage(item) returning keep, summarise, store or drop using the rules in the starter comments.
- Apply it and print one line per item with the decision.
- Sum the tokens before and after (summaries cost 50 tokens; stored and dropped items cost 0 in context).
Done when
- The system prompt and case facts are kept.
- The used file read is summarised, the preference is stored.
- At least 10,000 tokens are freed.
Open in the Playground No API key needed.
Starter code (Python)
ITEMS = [
{"name": "system prompt and tools", "kind": "instructions", "tokens": 3000, "needed_next": True, "used": False},
{"name": "case facts", "kind": "facts", "tokens": 120, "needed_next": True, "used": False},
{"name": "config.ts file read", "kind": "tool_result", "tokens": 9000, "needed_next": False, "used": True},
{"name": "search results (now in notes)", "kind": "tool_result", "tokens": 4000, "needed_next": False, "used": True, "in_notes": True},
{"name": "user said: no emojis", "kind": "preference", "tokens": 20, "needed_next": True, "used": False},
{"name": "small talk about the weather", "kind": "chat", "tokens": 200, "needed_next": False, "used": False},
]
def triage(item):
# TODO: rules:
# needed_next and kind != preference -> keep
# preference -> store (and it stays in use for now, cost it at its size)
# tool_result already in notes -> drop
# tool_result used -> summarise
# otherwise -> drop
return "keep"
before = sum(i["tokens"] for i in ITEMS)
after = 0
for i in ITEMS:
d = triage(i)
after += i["tokens"] if d in ("keep", "store") else 50 if d == "summarise" else 0
print("%-32s %s" % (i["name"], d))
print("tokens before:", before, "after:", after, "freed:", before - after)
Show a solution
ITEMS = [
{"name": "system prompt and tools", "kind": "instructions", "tokens": 3000, "needed_next": True, "used": False},
{"name": "case facts", "kind": "facts", "tokens": 120, "needed_next": True, "used": False},
{"name": "config.ts file read", "kind": "tool_result", "tokens": 9000, "needed_next": False, "used": True},
{"name": "search results (now in notes)", "kind": "tool_result", "tokens": 4000, "needed_next": False, "used": True, "in_notes": True},
{"name": "user said: no emojis", "kind": "preference", "tokens": 20, "needed_next": True, "used": False},
{"name": "small talk about the weather", "kind": "chat", "tokens": 200, "needed_next": False, "used": False},
]
def triage(item):
if item["kind"] == "preference":
return "store"
if item["needed_next"]:
return "keep"
if item["kind"] == "tool_result" and item.get("in_notes"):
return "drop"
if item["kind"] == "tool_result" and item["used"]:
return "summarise"
return "drop"
before = sum(i["tokens"] for i in ITEMS)
after = 0
for i in ITEMS:
d = triage(i)
after += i["tokens"] if d in ("keep", "store") else 50 if d == "summarise" else 0
print("%-32s %s" % (i["name"], d))
print("tokens before:", before, "after:", after, "freed:", before - after)