---
name: optimize-memory
description: "Search for a memory block for a deployed agent that keeps memory per subject, over conversation series in cloud trials, keeping one that beats the control on held-out subjects. Use when asked to improve or optimize how it keeps memory."
metadata:
  min-cli-version: "0.104.0"
  next: "deploy-try"
---

# Optimize: memory

An agent that serves many subjects (end users, accounts, sites) keeps one memory folder per subject. What it loads at the start and offloads at the end is set by the memory block in its system prompt, between `<!-- memory:start -->` and `<!-- memory:end -->`. This step searches over that block, not over what any one subject's memory holds.

It changes only the memory block. It never changes the rest of the prompt, the tools, the model, the provider or the judges, and it never merges or changes a runtime.

For standalone runs, read `introspection operator build/draft/runs-directory`.

## Before you start

First read the Recipe's own files: the system prompt (`SYSTEM.md`) with its memory block, `prompts/`, the approved cases in `evals/cases/`, its tools, skills and their resources (such as data that can run short), and fakes. From them, name the subject the agent keeps memory per (such as an account or site), what it observes of one, and what runs short. Ask all of step 1's questions in those terms, offering what the Recipe states as the default, before inspecting branches, past runs, runtimes or building anything.

Use the answers to confirm the control and its memory markers. Run `introspection setup --target harbor --yes`, then find the Runtime (`eval-run`).

The series run as cloud trials on the agent's Runtime, so an agent with no Runtime yet is deployed first (`deploy-connect`).

Keep the experiment's records in `<run>`, which is `<runs>/optimize-memory/<id>/`, outside the Recipe checkout.

## 1. Agree everything up front

Hold one interview, ask every question in it, and write the answers to `<run>/brief.md`. After it, run to the scorecard without asking again: a later question is answered by a default below or a check you run.

- **Subjects and how they differ:** at least three for training, and two more held out, never shown to the meta agent, that differ in kind from the rest (narrower needs, a resource that runs short, people who change between conversations). Ask what about a subject the agent should learn and use next time.
- **How a subject reacts:** for each way subjects differ, what the agent can observe of it and what an action of the agent's makes happen. This becomes the subject's model in the fakes, which the eval author writes; read it back to the owner before any run.
- **What runs short,** asked even when nothing seems to: anything a series can use up (catalogue items, slots, budget), and how many a series uses. Size it so it can't decide the result, or the winner is whichever harness rations it.
- **Metrics and guards:** scores of a good conversation, each kept apart; what memory must never keep (a one-conversation request, another subject's data); a guard in code for each prompt rule a memory block could override. Never sum metrics; report the worst subject.
- **The owner's rules for memory:** what the subject owns, what lives elsewhere, how a setting may change. Quote them in every review.
- **Conversations per subject:** three or more. The first is the same for every harness; the gain shows from the second.
- **Budget:** offer each option as its total of cloud trials, summed as [the series runner](references/series.md#budget) shows. Fit the leanest to the owner's size by trimming seed folders or generations, show the arithmetic, and get one confirmation.

## 2. Freeze the eval and seed the population

Reuse a frozen eval only if its brief and metrics match this interview. Otherwise have one eval author write the series ([brief](../../agents/eval-author-brief.md#a-memory-series)), then commit and push it as [the series runner](references/series.md) describes, and rebaseline all candidates against the new control.

The control is the Recipe's current block, at the commit that froze the eval. Choose up to four distinct alternatives from [seed harnesses](references/seeds.md). Make each candidate a pushed commit on the control that changes only its memory block, checked by diff as the series runner says.

During the owner's world review, smoke the control with one eval-runner ([brief](../../agents/eval-runner-brief.md)), so a stale Runtime surfaces then; later batches start one per run. Never run an eval yourself.

## 3. Run every candidate on the training subjects

Start one batch of eval-runners: a run per candidate, training seed folder and subject, every candidate on the same seed folders, at least two, so candidates pair and the control's spread is known. Collect each conversation's scores and evidence as the series runner says.

## 4. Have the meta agent revise the best

Keep the best two or three candidates as parents. Start a `meta-agent` subagent briefed as [agents/meta-agent-brief.md](../../agents/meta-agent-brief.md) says, with the owner's rules for memory from `<run>/brief.md` and each parent's training evidence it lists; never the held-out subjects, the evals or how a metric is computed. It replies with one revised block per parent; from the second generation on, ask for one crossover too, a block combining the strongest idea of two parents.

Review each revision as `improve-propose` step 3 says: the block's text is the diff, and the owner's rules are quoted so the reviewer can tell them from values fitted to the evals. A revision that names a subject, quotes one subject's memory, or drops or weakens one of the owner's rules for memory is rejected.

## 5. Repeat, then choose

Push each approved revision as a candidate and send the runner the next training batch: revisions, parents and control, each on a new identity. Then repeat step 4. Stop after the generations agreed, or when no revision beats its parent by more than the spread.

When gains stop, have the meta agent propose one revision for a failure the finalists’ worst conversations share.

[Checks](references/checks.md) sets headroom, power and finalists. Show the owner a scorecard per candidate: each metric, its worst subject, its gain over the control against the spread, and what its memory folders hold. A gain within the spread is a tie. The owner chooses one, none, or an idea to add as a candidate.

## 6. Prove it on held-out subjects

Send the same runner a separate held-out batch: each tied finalist and the control, on two held-out seed folders. Choose the finalist that does best there. In a final separate batch, confirm the choice against the control on seed folders no batch has run, as [checks](references/checks.md) describes. When it still beats the control by more than the spread, apply the winning memory block alone on a new branch from the Recipe's default branch. When it ties, keep the control: record both designs and their branches in `<run>/brief.md`, show the scorecard, and offer one more round on the failure the finalists share, with its size. Then check the quality floor, and write the memory floor and the report, as [floors](references/floors.md) says.

## Done when

- Every candidate ran the frozen training series on the same seed folders, at least two, as cloud trials.
- The meta agent's revisions were reviewed before they ran, and the owner chose from the scorecard.
- The chosen block beat the control on held-out and new seed folders, held the quality floor, and its pull request is open, or it tied there and the owner has the scorecard and the offer of one more round.

Then go on with `deploy-try`.
