Your Agent Keeps Making the Same Mistake. Give It a Playbook That Learns.
Agents often repeat the same failures because their context never really learns. Build an evolving playbook that reflects on outcomes, stores small reusable lessons, and improves without constantly rewriting the prompt or retraining the model.
On this page
Most agents do not really learn from yesterday.
They may have memory. They may have a long system prompt. They may even store summaries after each run.
But when the same edge case appears next week, they often fail in exactly the same way.
That is usually not a model problem. It is a context problem.
If every fix becomes another line in the system prompt, the prompt keeps growing until nobody knows which rule still matters. If every run rewrites one big memory summary, useful details slowly disappear.
I prefer a different pattern: let the agent maintain a small playbook of concrete lessons and update that playbook from real execution feedback.
Not by rewriting the whole thing.
By adding and refining small deltas.
That is the useful idea behind Agentic Context Engineering (ACE): the agent acts, reflects on what happened, and curates reusable lessons into context that can improve the next run.
Do not turn every failure into another prompt patch
Imagine a finance agent that mishandles the same multi-currency invoice every Monday.
The manual fix is obvious:
When an invoice contains multiple currencies,convert each line into the account base currencybefore calculating the total.You add that to the system prompt.
Then another issue appears.
Then another.
A few months later the prompt contains dozens of patches, some duplicated and some contradictory.
The other common approach is to keep one memory string and ask the model to rewrite it after every task:
async function updateMemory( oldMemory: string, task: string, outcome: string,) { return llm(`Current memory:${oldMemory}Task:${task}Outcome:${outcome}Rewrite the memory with the useful lessons.Keep it concise.`);}That looks reasonable, but it has a hidden problem.
Every update regenerates the entire memory.
The model is encouraged to compress, simplify, and paraphrase. After enough cycles, the memory may still read nicely while the exact details that once mattered have disappeared.
The fix is structural: stop rewriting the whole memory.
Use a playbook made of small lessons
I would store lessons as separate items instead of one blob.
Each item should be concrete enough to act on by itself.
type PlaybookItem = { id: string; topic: string; lesson: string; helpful: number; harmful: number; createdAt: number;};type Playbook = { items: PlaybookItem[];};A useful item might look like:
topic: invoiceslesson:For multi-currency invoices, convert every line itemto the account base currency using the invoice-date ratebefore summing.That is much better than:
Handle currencies carefully.The first one preserves the reason the lesson exists.
The second one is the kind of vague summary that looks clean and helps nobody.
Split learning into three jobs
The ACE paper separates the loop into three roles: a Generator, Reflector, and Curator.
I like that separation because each role has one responsibility.
The Generator should focus on doing the work.
The Reflector should look at what happened and propose lessons.
The Curator should decide whether those lessons deserve a place in the playbook.
That is different from asking one model call to solve the task, judge itself, rewrite memory, and preserve everything correctly at the same time.
The Reflector should produce deltas, not a new memory
The Reflector does not need permission to rewrite the playbook.
It only proposes small candidate lessons.
type LessonDelta = { topic: string; lesson: string;};async function reflect(input: { task: string; trajectory: string; outcome: string;}): Promise<LessonDelta[]> { const response = await llm(`Task:${input.task}What happened:${input.trajectory}Outcome:${input.outcome}Extract 0-3 reusable lessons.Each lesson must be concrete and self-contained.Return JSON only.`); return JSON.parse(response);}I would run reflection on successful tasks too.
Failures teach the agent what to avoid.
Successful runs can teach useful strategies, tool argument formats, or domain-specific shortcuts worth repeating.
The Curator is where quality matters
The Curator should have narrow powers.
It can append a new lesson, reinforce an existing one, or periodically retire weak entries.
It should not regenerate the entire playbook.
function curate( playbook: Playbook, delta: LessonDelta,) { const existing = findSimilarItem(playbook, delta); if (existing) { existing.helpful += 1; return; } playbook.items.push({ id: crypto.randomUUID(), topic: delta.topic, lesson: delta.lesson, helpful: 0, harmful: 0, createdAt: Date.now(), });}The main rule is simple:
new feedback should change a small part of the context, not rewrite all of it.
That is what makes the system easier to inspect and much harder to accidentally erase.
Let outcomes grade the playbook
A lesson should not become permanent just because the Reflector wrote it once.
Track whether it actually helps.
If an item was present in context before a successful task, increase its helpful signal.
If it repeatedly precedes failures, increase its harmful signal.
Then prune on a schedule instead of after every run.
For example:
every 50 tasks:- remove items that hurt more than they help- merge obvious duplicates- keep stronger, recent lessons- cap each topic to a reasonable sizeThis matters because self-improvement can become self-poisoning very quickly.
A wrong lesson that survives forever is worse than no lesson at all.
Only inject the part of the playbook the task needs
A growing playbook creates another problem: token cost.
Do not dump the entire thing into every task.
If the current task is about invoices, the agent probably does not need lessons about SQL migrations or support-ticket formatting.
I prefer selecting context by topic and relevance:
function selectPlaybook( playbook: Playbook, topics: string[],) { return playbook.items .filter((item) => topics.includes(item.topic)) .sort((a, b) => { const aScore = a.helpful - 2 * a.harmful; const bScore = b.helpful - 2 * b.harmful; return bScore - aScore; }) .slice(0, 20);}The exact number is not important.
The design is.
The playbook can be large while the active context stays small.
The dangerous part: who is allowed to teach the agent?
A self-updating playbook is also a persistence layer for mistakes and attacks.
If untrusted content can influence the outcome signal or the Reflector directly, a prompt injection can potentially turn into a lesson that survives into future tasks.
That means the Curator needs the same kind of boundary I would put around any state-changing agent tool.
A new lesson should never be able to grant permissions, create credentials, change tool access, or rewrite security policy.
For important workflows, I would also keep provenance:
lessonsource taskoutcometimestampmodel/versionapproval statusThen when an item behaves badly, you can trace where it came from.
Where this helps most
This pattern is useful when the agent repeatedly encounters domain-specific quirks that are too small for fine-tuning but too important to rediscover every run.
Examples include:
- one client's unusual API behavior;
- recurring invoice edge cases;
- tool argument formats that are easy to misuse;
- SQL conventions inside one data warehouse;
- deployment quirks in one environment;
- support workflows that have exceptions not captured in public docs.
I would not use a self-improving playbook for stable security rules, core authorization policy, or facts that already have a reliable source of truth.
Those belong in deterministic systems.
The playbook is for experience, not authority.
The takeaway
A useful agent should not need a human to patch the system prompt every time it learns something.
It also should not continuously rewrite one giant memory and slowly erase the details that made those lessons useful.
Keep the context as small, inspectable items.
Let the agent act.
Reflect on the result.
Curate only the useful deltas.
Grade those lessons with future outcomes and prune them deliberately.
That gives the agent something much closer to accumulated experience without touching the model weights.
The model stays the same.
The context gets better.
And that is often enough to stop the agent from making the same mistake next Monday.
References
KEEP READING
Related engineering notes
A Failover Route Is Not a Recovery Path: Proving Independence in AI Infrastructure
Two LLM providers can share the same failure domain. Build an evidence-backed recovery graph that checks cloud, networking, identity, control-plane and capacity dependencies before admitting fallback routes, then test correlated outages without weakening safety.
Read articleAn Eval Platform Sunset Can Erase Your Release Evidence: Build an Evidence-Escrow Migration Gate
When a hosted LLM evaluation platform retires, switching runners is not enough. Preserve the historical evidence, reconstruct grader semantics, validate scoring parity, and enforce a fail-closed release gate that survives the service shutdown.
Read articleWhen the Package Runs Before Your App: Quarantining AI Framework Releases
The 2026 agent-framework PyPI compromise exposed a blind spot in AI build pipelines: Python startup hooks execute before application imports. Design an artifact-quarantine gate, inspect wheels without installing them, separate credentials, and recover from suspected exposure.
Read article