Agent Run Pre-Mortem and Self-Correction Loop Designer

Designs verification checks, error handling, and a self-correction loop for an agent task before it runs, based on likely failure modes.

You get: Failure table, loop specification as numbered steps with exit conditions and agent self-check instruction block

More like this

The prompt

agent-run-pre-mortem-and-self-correction-loop-designer.md
Role: You are a reliability engineer for LLM agents.Context: An agent will perform {task_description} using {tools}. The decision this answer must support: which checks and correction loops to add before letting the agent run unattended, and where to keep a human reviewer.
Task: Run a pre-mortem on the task and design a self-correction loop with measurable verification steps.Constraints: Ask for missing inputs before output. Separate assumptions from facts. Do not suggest unlimited retries. Do not claim the loop has been tested. Prefer checks that can be verified from tool output, not from the model's own confidence.

Inputs:
- Task description: {task_description}
- Tools and what each can change: {tools}
- Definition of a correct result: {correct_result}
- Known past failures or edge cases: {known_failures}
- Cost, time, and retry budget: {budget}
- Cost of a wrong result (low/medium/high, with example): {error_cost}

Workflow:
1. Identify missing inputs and ask.
2. Pre-mortem: assume the run failed; list the 8 most likely causes (bad tool output, ambiguous instruction, stale data, hallucinated field, partial completion, looping, scope creep, permission error).
3. For each cause, define a detection signal and a correction action.
4. Design the loop: act, verify, diagnose, correct, re-verify, with max {max_retries} attempts, then escalate.
5. Define what the agent must log at each step.
6. Decide which checks are automated and which need a human.

Output format:
1. Assumptions and missing information
2. Failure table: Cause | Detection signal | Correction action | Automated or human
3. Loop specification as numbered steps with exit conditions
4. Agent self-check instruction block (copy-ready, under 200 words)
5. Log schema: field | purpose
6. Next actions (max 5)

Acceptance criteria:
- Each detection signal is observable from tool output or data, not opinion.
- The loop has a hard stop and an escalation message template.
- The self-check block can be pasted into a system prompt unchanged.
- High-cost errors always have a human gate.

Quality checks:
- Confirm no correction action can repeat an irreversible side effect.
- Confirm retries are bounded by {budget}.
- Flag any failure cause you could not cover and why.

Platform notes: In Claude, put the self-check block inside <self_check> tags for system prompt reuse. In ChatGPT, also supply the log schema as JSON. In agent frameworks such as LangGraph or the OpenAI Agents SDK, map the loop to graph nodes or guardrails by name, without writing code unless I ask.

Example output

AI-written example

1. Assumptions and missing information

  • Facts given: the agent matches vendor invoices to POs. The ERP and email tools are read-only, and create_draft_payment is reversible. Budget is $2 and 10 minutes per run, with max 2 retries. Error cost is high (a duplicate payment of about $20k).
  • Assumptions: invoice numbers are unique per vendor, and the ERP has a duplicate-check query. Not covered: scanned-image invoices (no OCR tool listed).

2. Failure table

CauseDetection signalCorrectionAuto/Human
Bad tool outputERP returns empty or error codeRetry once, then skip invoiceAuto
Ambiguous instructionMultiple POs matchFlag, no guessHuman
Stale dataPO last_updated older than 24hRe-queryAuto
Hallucinated fieldExtracted value absent from source textRe-extract, else flagAuto
Partial completionDrafts count ≠ matched countRe-run only missing itemsAuto
LoopingSame call and args twiceStop, escalateAuto
Scope creepAny call outside the allowed listBlock, escalateAuto
Permission error401/403No retry, escalateHuman

3. Loop specification

  1. Act: process one invoice.
  2. Verify: the amount is within 1% of the PO, the duplicate-check query returns zero hits, and the vendor ID matches.
  3. Diagnose: map the failed check to a table row.
  4. Correct: apply that row's action. Never re-call create_draft_payment without a fresh duplicate check.
  5. Re-verify. Exit on pass.
  6. Hard stop: 2 retries per invoice, $2 or 10 minutes per run. Then escalate.
  7. Human gate: every payment batch is approved by a person before release.

Escalation template: "Invoice {id} halted after {n} attempts. Failed check: {check}. Last tool output: {excerpt}. No payment drafted."

4. Self-check block
<self_check>
Before each draft payment, confirm from tool output only: (1) the invoice amount is within 1% of the PO amount, (2) the duplicate-check query returned zero matches, (3) every extracted field appears verbatim in the source. If any check fails, diagnose it, apply one correction, and re-verify. Make at most 2 retries per invoice. Never repeat a write action without a new duplicate check. Never call tools outside the allowed list. If a call returns 401/403, stop. If the same call repeats with the same arguments, stop. On stop, send the escalation message and do not guess. Log every step. Your confidence is not evidence.
</self_check>

5. Log schema

  • invoice_id: traceability
  • step: loop position
  • tool_call: the call and its arguments
  • output_excerpt: evidence for the check
  • check_result: pass/fail per check
  • retry_count: budget enforcement
  • cost_so_far: stops runaway spend

6. Next actions

  1. Add a duplicate-check query to the ERP tool.
  2. Confirm the allowed-tool list.
  3. Run 20 historical invoices in dry-run mode.
  4. Set a human approver for batches.
  5. Decide how to handle scanned invoices.

Not covered: the loop has not been tested. Scanned-invoice extra

Generated from this prompt by AI. Your results will vary.

Tips and common edits

Run this on the task description you plan to give the agent. Use the failure table as the basis for test cases. Review the self-check block against your real tool outputs before deploying.

Questions about this prompt

More in Agentic AI

20-Case Agent Eval Set with Pass/Fail Rubric for Normal, Edge and Unsafe Requests

Builds 20 test cases for your agent's task, with expected behavior, failure signals and a pass/fail rubric covering normal, edge and unsafe requests.

Agent Tool Names, Descriptions and JSON Schemas from API Docs

Turns pasted API docs or a function list into agent-ready tool names, descriptions, JSON input schemas and when-to-use and when-not-to-use notes.

Agent Workflow Decomposition with Autonomy Levels and Stop Rules

Turns a manual process into a step-by-step agent plan with tools, approval gates, retry limits, and a build/pilot/skip recommendation.

Checkable Guardrail Rules for a Coding Agent: Blocked Commands, Approvals, Secrets and Hooks

Produces a set of testable guardrail rules for your coding agent, covering blocked commands, approval gates, secret handling and hook checks.

Research Agent Brief: Source Discovery, Evidence Review, and Citation Table

Directs an AI agent to plan source discovery, grade evidence, build a citation table, and support a decision without claiming live browsing occurred.

Tool Permission Matrix and Function Spec for a Tool-Using Agent

Produces a least-privilege tool inventory, function definitions, and approval rules for an agent that calls APIs or internal systems.

Agent Pipeline Failure Mode Audit with Retry, Fallback, and Human Escalation Rules

Audits an existing multi-agent pipeline for failure modes and produces retry limits, fallback paths, escalation triggers, and a test plan. Supports the decision on what to harden before wider rollout.

Coding Agent Task Brief with Guardrails, Test Plan, and Rollback Steps

Creates a scoped brief for an AI coding agent that limits file changes, defines tests, and includes a rollback plan and review checklist.

Design an AI Agent With Tools, Guardrails and Eval Cases Before You Build It

Turns an agent idea into a build-ready design: the job, the tools with their inputs and permissions, guardrails, failure handling and a set of evaluation cases to test it.

Source Credibility Check with Claim-by-Claim Verification Plan

Lists every factual claim in a pasted article or claim, rates the evidence for each and says exactly what to verify and where.

Weighted Decision Matrix with Pre-Mortem on the Winner

Guides you through criteria and weights, scores each option in a table, then runs a pre-mortem on the top choice.

Characterization Tests Before a Refactor

Writes tests that lock in a module's current behavior, including edge cases and quirks, so you can refactor safely.