The idea
Your agents run many sessions, and each one starts knowing nothing. Dreaming is a background pass that looks back over past transcripts with hindsight and improves the memory future sessions read: it can correct what turned out to be wrong, build a representation that's easier to search, and keep what matters within easy reach.
The pipeline
- Export sessions
Each finished session (idle or terminated) becomes one plain-text transcript with labeled blocks: USER:, AGENT:, TOOL CALL, TOOL RESULT. Tool results matter most — they show what the agent actually did.
- One message to the lead agent
You describe the work in plain words. The lead agent (Claude Sonnet 5.5) writes the workflow and starts it as a workflow run. It never reads a transcript itself.
- Analyze phase
One analyzer (Claude Haiku 5.5) per transcript, in groups of about 20. Each reads its transcript in full, then verifies, enriches and organizes the shared memory files.
- Organize phase
One organizer (Claude Haiku 5.5) runs after every analyzer finishes: it merges duplicates, fixes links, marks conflicts and completes _index.md, keeping every fact.
- Mount read-only
Your agent's next session mounts the memory store read-only and starts from _index.md.
What an analyzer does
The analyzer's prompt holds the method, so changing it changes what your memory keeps. It has four steps:
- Read the whole transcript — page through it, but don't stop part-way.
- Verify what memory already claims against it: leave unsupported claims alone, fix contradictions, and downgrade claims the transcript shows were uncertain.
- Enrich with exact identifiers, commands, values, staleness notes, and approaches that burned turns before the right one.
- Organize into small files with frontmatter (name, description), link related topics with [[slug]], and keep _index.md current.
Analyzers run at the same time on one shared directory, so the prompt tells them to re-read a file right before editing it, add with targeted edits instead of rewriting, and read every write back to confirm it landed. A last section tells them to work in as few turns as possible, because every turn resends everything they've read.
A worked example: facts go stale
The example set is eleven synthetic files about tidewick, a fictional Python project. Early on, make test only passes with TIDEWICK_DATA_DIR set; a later release removes that need. The memory has to date and supersede the early fact, not copy it — and the organizer resolved exactly that conflict as version-dependent.
Treating transcripts as untrusted
A tool result inside a transcript can say “ignore your instructions”. The cookbook's defenses:
- Prompts treat transcripts, memory files and run results as data, not instructions. The transcript's text never enters a prompt; the analyzer opens the file with its read tool.
- The analyzer holds five file tools only — read, write, edit, glob, grep — with no shell or web access and an empty network allowlist.
- Analyzers never copy secrets into memory; they name the credential (“the deploy token”) instead.
- At worst an injected instruction changes what the store says and spends up to your budget. So the store is mounted read-only everywhere except the dreaming session itself.
Cost and limits
Costs are at list price. On the 50-transcript set, the settings that keep cost down (grouping, the fewer-turns prompt section, plain-text transcripts and a Haiku organizer) took a dream from about $10 to about $8, and the resulting stores held about the same share of a fixed list of facts. The run prints an estimate first and stops if it's over BUDGET_USD ($40 by default, a ceiling rather than an expected cost).
Dreaming every night
dreaming_nightly.py runs the notebook from cron. It exports the agent's finished sessions since the last run and dreams over them into the same store:
0 1 * * * mkdir -p ~/.cache/memory_distill && cd /path/to/managed_agents && COOKBOOK_AGENT_ID=agent_... COOKBOOK_STORE_ID=memstore_... /path/to/python dreaming_nightly.py >> ~/.cache/memory_distill/nightly.log 2>&1- Credentials stay with the script. The export runs on your machine; the dreaming session has no network access and no key.
- A failed night retries: the next night takes the same sessions again.
- A session still running at 1 AM is skipped, and later nights don't look back for it.
- There's no review by default. Each run saves a copy of the store first, and every change is a memory version you can audit for 30 days.
Make it yours
Three choices shape a dream: when it runs, which transcripts it reads, and what it does with them. The knobs, all set by environment variable or prompt file:
- Schedule — edit the crontab line (0 1 * * 1 runs Mondays at 1 AM).
- Transcripts — COOKBOOK_AGENT_ID for one agent's sessions, COOKBOOK_SINCE for how far back, or point TRANSCRIPTS at a folder of logs.
- Pace — the last sentence of prompts/work_request.md sets the group size.
- Focus — INSTRUCTIONS, e.g. “This memory helps an assistant work with one person. Organize it around that person's goals, preferences and standing requests.”
- Shape — prompts/analyzer_system.md and prompts/organizer_system.md.
- Models — COOKBOOK_MODEL, COOKBOOK_ANALYZER_MODEL, COOKBOOK_ORGANIZER_MODEL. Dreaming runs in the background, so it can use a stronger model than the agent it serves.