Failure knowledge is conditional knowledge

Most work on recursive self-improvement focuses on getting useful knowledge into the agent. A big part of that is failure experience, lessons from past mistakes that should help the agent do better next time. But we found that failure knowledge is actually "conditionally beneficial" for the agent. If it sits in the agent's main context, it can even distract the agent. And it only helps when the agent actually runs into the failure it describes.

Our framework, Sentry, is built around this idea and helps agents learn from failures and recover from them at test time. It keeps failure knowledge outside the agent and only steps in when a failure actually happens.

Where our research starts

We ran a Qwen3.5-9B agent on WebShop with ACE, which keeps a playbook of advice learned from past tasks. However, overall, the agent did not perform as well as expected. We checked the playbook that the agent obtained, and they were all helpful entries, so what went wrong?

When we dived deeper into the traces of this, we discovered that even if this failure knowledge was correct, it can become a distraction, because the agent can apply the advice in the wrong situation, and distinguishing when and where to apply the failure advice is relatively difficult for the agent itself. Here is an example:

On one task the agent ran search[women's pumps] twenty times and got nothing. ACE wrote three rules from that: if nothing fits, broaden the query; if page 1 has no match, check later pages; if results are still off, refine the query.

A few tasks later, the agent is asked for 20-inch hair extensions under $80. The first result is a 20-inch extension for $79.63. The agent even says so: "Item 1 fits the description perfectly. Let me also go through item 2, item 3, item 4 to make sure there is nothing better…" It never buys anything. Reward 0.

Each rule was correct and helpful, but the agent couldn't tell that they didn't apply here, so "check later pages" turned into "keep looking."

Context evolution (ACE). Earlier task 6594: the agent repeats search[women's pumps] 20 times, gets irrelevant results and reward 0. ACE learns three recovery rules and injects them into the agent's context before the next task. Later task 4494: the user asks for 20-inch hair extensions under $80; the first search result is a 20-inch extension at $79.63 that fits. The agent says item 1 fits perfectly but goes on to check items 2, 3 and 4, takes no action, and gets reward 0. The correct action, click on the item, is never taken.
Figure 1. Context evolution (ACE), Qwen3.5-9B on WebShop. Rules learned from task 6594 stay in the agent's context for task 4494. The agent finds a matching item but keeps comparing and never acts.

Then we turned to another line of methods, runtime intervention methods. These methods try to monitor agent failures and give guidance when failure happens. But they have the opposite problem. Their advice is too generic, they fix their recovery and detection strategies across different tasks, and some of these monitors' diagnoses are simply wrong.

For example, on one WebShop task, AgentGuard warns "no progress" whenever the page doesn't change. Sometimes that advice can help the agent break a real search loop. Other times the page doesn't change because the agent is picking a size and color, which is fine. The warning fires anyway, the agent leaves the page, and the options are gone. In this example, a fixed strategy cannot help the agent recover from its failures.

Runtime intervention (AgentGuard) with the fixed rule: page unchanged for n steps, warn no progress. Earlier task 3980: the agent repeats identical searches, the warning is correct, the agent selects options and buys, reward 1.0; AgentGuard stores no lesson from this rescue. Later task 4585: the agent selects mossy oak country and large, the product page is unchanged but both options are registered; the same warning fires wrongly, the agent goes back, reopens the product and buys with no options, reward 0.6.
Figure 2. Runtime intervention (AgentGuard), Qwen3.5-9B on WebShop. The fixed "no progress" warning rescues task 3980, but no lesson is stored. In task 4585 the same warning fires after valid option selections, and the agent buys the item without its options.

The idea

Failure knowledge is conditional knowledge and should be conditionally exposed.

In practice, a failure lesson should be:

Learned

Kept across tasks, so a fix that worked once can be reused.

like context evolution
Surfaced

Showing up only when corresponding failures occur.

like runtime intervention
Admitted

Saved only if the fix worked. A lesson from a failed fix will misfire later too.

neither does this

We introduce Sentry (Figure 3), a failure-management layer that runs alongside the task agent and handles failure knowledge along these three properties: it surfaces recovery lessons only when it diagnoses a failure, admits a lesson only after verifying the recovery, and learns by accumulating verified lessons across tasks. The complete playbook is maintained outside the agent's persistent background context; we refer to this as isolation. Isolation does not hide recovery knowledge from the agent: the few entries retrieved for a detected failure still enter the conversation during that intervention.

During execution, Sentry monitors the agent's recent trajectory. It routes invalid actions to hard repair, which asks the task agent for a valid replacement action, and routes progress and reasoning–grounding failures to soft repair, which combines the local diagnosis with relevant playbook entries to guide the agent's next step. After a soft repair, Sentry verifies whether the triggering failure has been resolved and adds only verified recovery lessons to the playbook for later interventions.

Sentry architecture: the task agent's recent context window feeds an action-validity check. Invalid actions go to Hard Repair. Valid actions go to a Failure Detector that routes progress failures and reasoning and grounding failures to soft repair, which retrieves matching entries from the playbook by label. A verifier checks later steps and, if the problem is solved, updates the playbook.
Figure 3. Sentry's loop. Hard repairs are checked right away, soft repairs after H steps. Only soft repairs that worked become playbook entries.

One run, step by step

This is a recorded AppWorld run with GPT-4.1 as the agent. The task: build a new Spotify playlist from songs in five existing ones. The agent starts calling account APIs before logging in.

Demo. 2.5 minutes: detect, retrieve, verify, then results. Starts muted.
AppWorld · Spotify · recorded runagent steps and Sentry events
step 1
apis.spotify.show_account()401 Unauthorized
sentry
detectThe call is well-formed, so no hard repair. But the agent isn't getting anywhere. Diagnosis: planning_stall. P1Don't repeat an unchanged request P2Stop exploring; take the justified step P3Check docs before assuming a value · not retrieved Two of three entries match and go into the soft-repair prompt.
step 4
apis.spotify.login(email=…)422 Invalid arguments
hard repairSentry asks for the login call again, matching the schema.
step 6
logged in
step 8
no token Later calls still leave out the access token.
9 – 11
token passed ✓ The loop is over.
sentry
verifyThe verifier sees the trigger and the next 10 steps. No reward, no success label. It marks the failure resolved.
learnSentry writes P4. The playbook goes from 3 to 4 entries.
P4 · When an authentication or authorization error is detected, inspect the API docs for required auth parameters, obtain or refresh credentials, and retry with proper authentication before proceeding.
end
7 / 7 official checks passed

Main results

Against runtime-intervention baselines, with Sentry starting from an empty playbook, Sentry wins on all four benchmarks. It's 37% better on average than the best baseline for each benchmark, and 77% better on WebShop. The runner-up is a different method on every benchmark.

Method WebShop
reward
AppWorld
test pass %
SWE-bench Lite
accuracy
Mind2Web Replay
completion
Base agent 0.170 25.59 0.20 0.420
AgentGuard 0.258 25.09 0.22 0.450
AgentForesight 0.168 24.17 0.24 0.430
AgentFixer 0.171 26.69 0.22 0.687
Wink 0.264 27.56 0.18 0.370
Sentry 0.468 38.53 0.30 0.736
gain over best baseline +77.3% +39.8% +25.0% +7.1%

On held-out tasks, Sentry beats ACE by 48% on WebShop and 30% on Mind2Web Replay. Reflexion, AWM, and ReasoningBank all score below ACE. Even with the playbook frozen during evaluation, Sentry does better than with retrieval off (0.251 → 0.364 and 0.533 → 0.651), so the lessons carry over to new tasks.

The gains aren't just from fixing malformed calls. Without hard repair, Sentry still beats every runtime-intervention baseline on both benchmarks we tested. With GPT-OSS-120B instead of Qwen, it's still the best method, 51% above the top baseline on average.

81.7%
of 939 detected failures fixed
86.0%
human agreement on when to step in
1.54×
tokens vs. the base agent (AgentForesight: 2.44×)
−31.7s
per task on SWE-bench Lite (−45.6 s on AppWorld)

Paired forks at 200 intervention points

final task score, Sentry branch vs. unaided branch from the same state

Sentry higher · 74tie · 101unaided higher · 25

Local recovery after 10 steps: 33.5% unaided, 60.5% with Sentry. Paper Table 11.

Things We Learned

Insight 1

Where a lesson lives matters as much as what it says

We first deleted the failure lessons from the ACE playbook entirely, and the scores even went up on both benchmarks, which means that these lessons hurt more on average when they are situated in the agent's main context.

However, deleting these failure lessons also throws the valuable reflections on failure away, so we changed the setting to conditionally retrieving them. Two Sentry variants share the same frozen playbook, detector, and retrieval. One also puts the whole playbook in the agent's context. It still gets the right lessons on demand, and it still does worse. These experiments tell us that the lesson should live conditionally, and conditionality matters as much as its quality.

Same lessons, different exposure

held-out · WebShop avg. reward / Mind2Web Replay completion

(a) Remove ACE's failure lessons · WebShop
ACE · 0.3350.335
ACE w/o failure lessons · 0.3910.391
ACEACE, failure lessons removed
(a) Remove ACE's failure lessons · Mind2Web
ACE · 0.5620.562
ACE w/o failure lessons · 0.6430.643
ACEACE, failure lessons removed
(b) Frozen playbook · WebShop
On demand only · 0.364 ± 0.0190.364
+ persistent exposure · 0.296 ± 0.0170.296
on demand only+ full playbook in context
(b) Frozen playbook · Mind2Web
On demand only · 0.651 ± 0.0230.651
+ persistent exposure · 0.599 ± 0.0180.599
on demand only+ full playbook in context

Scales: WebShop 0–0.7, Mind2Web 0–0.9. Paper §2 and Table 4.

Insight 2

Knowing when to intervene is important

Of everything we ablated, the check for whether to intervene at all mattered most. Removing it cut performance by 55% on average across WebShop and AppWorld. On WebShop, a Sentry that steps in every time does worse than no Sentry. Always intervening will frequently disrupt the agent's reasoning progress, causing it to get confused and change its behavior, which will cause worse performance.

Intervening every step is worse than not intervening

online from scratch · WebShop avg. reward

Base agent · 0.170 ± 0.0160.170
Sentry w/o intervention-need check · 0.1360.136
Sentry (gated) · 0.468 ± 0.0170.468
base agentSentry, always interveningSentry, gated

Scale 0–0.6. Dashed line: base agent. Paper Table 3.

Insight 3

Right lesson > More lessons

With everything else fixed, we changed the retrieval of these lessons, and we discovered that the closer the guidance matches the diagnosed failure, the better performance the agent achieves. We retrieve the top k entries from the playbook with the same failure label and the same failure type. Even when you have the right diagnosis, it is important to use the right entries that can actually help with the problem, as we see that random retrieval degrades performance because it will often mislead the agent.

How lessons are picked

held-out · WebShop avg. reward

Random · 0.33 ± 0.030.33
Unfiltered · 0.39 ± 0.010.39
Failure type only · 0.43 ± 0.010.43
Type + label, k=3 · 0.47 ± 0.010.47
Type + label, k=5 · 0.50 ± 0.020.50
Type + label, k=10 · 0.46 ± 0.020.46
randomwhole playbooksame typelabel k=3label k=5label k=10

"label" = same failure type plus at least one shared label. Scale 0–0.6. Mind2Web Replay follows the same order: 0.55 / 0.61 / 0.66 / 0.70 / 0.73 / 0.69. Paper Table 14.

Insight 4

Task-level and failure-level learning are complementary

ACE provides task-level knowledge before execution; Sentry supplies failure-specific guidance when something goes wrong. Their playbooks remain separate.

The combination performs best among the evaluated methods on both benchmarks, suggesting that task-level and failure-level learning address different needs and work better together.

ACE and Sentry together

held-out · WebShop avg. reward / Mind2Web Replay completion

WebShop
Base agent · 0.186 ± 0.0140.186
ACE · 0.335 ± 0.0190.335
Sentry · 0.495 ± 0.0170.495
ACE + Sentry · 0.555 ± 0.0270.555
baseACESentryACE + Sentry
Mind2Web Replay
Base agent · 0.421 ± 0.0260.421
ACE · 0.562 ± 0.0200.562
Sentry · 0.729 ± 0.0190.729
ACE + Sentry · 0.789 ± 0.0220.789
baseACESentryACE + Sentry

Scales: WebShop 0–0.7, Mind2Web 0–0.9. Paper Table 2.

Paper and code

This post is based on our paper, Sentry: Learning to Recover from LLM Agent Failures at Test Time. The paper has the full method, prompts, more ablations, and an example playbook.

The code is open source at github.com/nuglifeleoji/Sentry. If your agents keep getting stuck, give it a try. Stars, issues, and PRs are all welcome. There is also a short thread on X with the demo video.

Sentry is joint work with Amy Lu (equal contribution), Qizheng Zhang, and Kunle Olukotun at Stanford. Huge thanks to them as well as the Stanford Pervasive Parallelism Lab.

Citation
@article{ji2026sentry,
  title   = {Sentry: Learning to Recover from LLM Agent Failures at Test Time},
  author  = {Ji, Changxiu and Lu, Amy and Zhang, Qizheng and Olukotun, Kunle},
  journal = {arXiv preprint arXiv:2610.02994},
  year    = {2026}
}

Questions or feedback: leo0610@stanford.edu