Lesson 55.2 · 2 of 4 · 4 min read · Reviewed 24 Nov 2026
Session state and scratchpads
By the end, you can
- Use session state and scratchpads.
- Keep important state outside the conversation text.
Session state
Your application can keep structured state alongside the conversation:
{
"session_id": "s-881",
"customer_id": "C-2201",
"order_id": "1042",
"identity_verified": true,
"step": "awaiting_refund_approval",
"decisions": ["refund approved up to 40"]
}
Each turn, insert a short “case facts” block from this state into the context (chapter 63). If history is trimmed or summarised, the facts remain exact.
Scratchpads and notes files
For long tasks, give the agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. a place to write:
# notes.md
Goal: find why checkout tests fail on CI only
Tried: retry (no), longer timeout (no)
Found: tests share /tmp/cart; parallel runs collide
Next: give each worker its own temp dir
The agent reads and updates it as it works. If the context is reset, the notes bring it back up to speed in a few hundred tokens. Claude’s memory tool and Claude CodeClaude Code: Anthropic’s agentic coding tool that works in your terminal, IDE and CI, reading and editing code and running commands.’s notes files work this way (56.3).
Rebuilding context
With state and notes outside the chat, you can always build a fresh, focused context: system promptSystem prompt: Instructions passed in the top-level system field that set the model’s role, rules and context for the whole conversation., case facts, notes, and the last few turns. That’s often better than carrying a long, cluttered history.
Key points
- Session state is structured data your app keeps for the session: ids, progress, decisions.
- A scratchpad (or notes file) is where an agent writes plans and findings during a task.
- Keeping state outside the chat lets you rebuild a clean context at any time.
Rebuild context from session state
Keep session state and a scratchpad outside the chat, and rebuild a small focused context from them after a reset.
You will have: A build_context function and a comparison of its size with the long history it replaces.
Steps
- Read the long HISTORY list, the STATE dictionary and the NOTES text in the starter.
- Write build_context: system prompt, a case facts block made from STATE, the notes, and the last 3 turns.
- Simulate a reset by throwing away HISTORY except the last 3 turns.
- Print the rebuilt context and compare its token estimate with the full history.
Done when
- The rebuilt context contains order_id: 1042 and identity_verified: True.
- It is at least 5x smaller than the full history.
- The last 3 turns are included in full.
Open in the Playground No API key needed.
Starter code (Python)
SYSTEM = "You are a support agent for an online shop."
HISTORY = ["turn %d: ... long discussion about delivery options and a damaged parcel ..." % i for i in range(1, 61)]
STATE = {"customer_id": "C-2201", "order_id": "1042", "identity_verified": True, "step": "awaiting_refund_approval"}
NOTES = "Goal: refund damaged parcel. Tried: replacement (out of stock). Next: approve refund up to 40 credits."
def tokens(text):
return max(1, len(text) // 4)
def build_context(state, notes, recent):
# TODO: return a list of strings: SYSTEM, a "<case_facts>" block with one "key: value" line
# per item in state, the notes, then the recent turns
return [SYSTEM]
recent = HISTORY[-3:]
ctx = build_context(STATE, NOTES, recent)
print("\n".join(ctx))
print("full history tokens:", tokens(" ".join(HISTORY)), "rebuilt tokens:", tokens(" ".join(ctx)))
Show a solution
SYSTEM = "You are a support agent for an online shop."
HISTORY = ["turn %d: ... long discussion about delivery options and a damaged parcel ..." % i for i in range(1, 61)]
STATE = {"customer_id": "C-2201", "order_id": "1042", "identity_verified": True, "step": "awaiting_refund_approval"}
NOTES = "Goal: refund damaged parcel. Tried: replacement (out of stock). Next: approve refund up to 40 credits."
def tokens(text):
return max(1, len(text) // 4)
def build_context(state, notes, recent):
facts = "<case_facts>\n" + "\n".join("%s: %s" % (k, v) for k, v in state.items()) + "\n</case_facts>"
return [SYSTEM, facts, "<notes>" + notes + "</notes>"] + list(recent)
recent = HISTORY[-3:]
ctx = build_context(STATE, NOTES, recent)
print("\n".join(ctx))
full, small = tokens(" ".join(HISTORY)), tokens(" ".join(ctx))
print("full history tokens:", full, "rebuilt tokens:", small, "(%.0fx smaller)" % (full / small))