Inferensys

Prompt

Agent Plan Patching Prompt for Partial Completion

A practical prompt playbook for generating a recovery plan when an agent step partially succeeds, identifying completed work, required retries, and compensation actions.
Developer doing prompt engineering on laptop, prompt variations visible on screen, casual coding session.
PROMPT PLAYBOOK

When to Use This Prompt

Defines the precise conditions for using the partial completion recovery prompt and distinguishes it from other failure modes.

This prompt is designed for agent runtime developers who need to recover from a step that partially succeeded in a long-running workflow. A partial completion occurs when a single step applies some side effects while failing to apply others—for example, a database write succeeds but a notification fails, a file is created but permissions are not set, or a multi-resource deployment provisions compute but fails on storage. In these cases, the agent cannot simply retry the step because repeating the successful side effects could cause duplication, inconsistency, or resource conflicts. Instead, the agent must produce a structured recovery plan that identifies what completed, what must be retried or compensated, and whether a rollback is required.

Use this prompt inside an agent execution loop after detecting a partial completion state. The detection should come from postcondition validation or step verification logic that compares expected side effects against actual state. The prompt expects a detailed description of the step that was attempted, the intended side effects, and the observed partial state. It produces a recovery plan with three sections: completed side effects that should be preserved, failed side effects that need retry or compensation, and a rollback decision with justification. The output should be machine-readable so the agent runtime can execute the recovery steps programmatically.

Do not use this prompt for total step failures where no side effects were applied. For those cases, use the Agent Plan Patching Prompt for Tool Failure, which handles clean retry or tool substitution without compensation logic. Do not use this prompt when the failure mode is an unexpected output that doesn't match the expected schema—that scenario requires the Agent Plan Patching Prompt for Unexpected Output. Do not use this prompt when new evidence invalidates prior assumptions rather than a specific step failure—that requires the Agent Plan Patching Prompt for New Evidence. Misclassifying the failure mode leads to recovery plans that either over-compensate by rolling back work that didn't need undoing, or under-compensate by retrying steps that already left side effects. Always run a failure classification step before selecting which patching prompt to invoke.

PRACTICAL GUARDRAILS

Use Case Fit

Where the Agent Plan Patching Prompt for Partial Completion works and where it introduces unacceptable risk. Use these cards to decide whether this prompt fits your agent architecture before wiring it into a production loop.

01

Good Fit: Long-Running Workflows with Side Effects

Use when: your agent executes multi-step plans against mutable resources (databases, APIs, file systems) where a step partially succeeds—some writes committed, others failed. Guardrail: require the prompt to output an explicit completed/failed/pending inventory before proposing retry or compensation steps.

02

Bad Fit: Stateless or Idempotent-Only Pipelines

Avoid when: every step is idempotent and stateless, or your execution engine already handles partial failures through simple retry with no side-effect tracking. Guardrail: if you don't need compensation logic, use a simpler retry prompt instead—this prompt adds latency and token cost for state reconciliation you don't require.

03

Required Inputs: State Inventory and Failure Evidence

What to watch: the prompt cannot patch a plan without knowing what completed, what failed, and what error evidence exists. Guardrail: always provide a structured state snapshot (completed step IDs, outputs, side effects) and the raw failure output or error message. Never feed only the original plan and a vague 'it broke' signal.

04

Operational Risk: Compensation Incompleteness

What to watch: the model may propose a recovery plan that misses a side effect requiring rollback, leaving the system in an inconsistent state. Guardrail: run a postcondition check on the patched plan—verify every completed step with a side effect is either preserved, compensated, or explicitly marked as irreversible with a human approval flag.

05

Operational Risk: Patch Amplification

What to watch: a small partial failure can trigger an overbroad patch that discards more completed work than necessary, wasting compute and increasing latency. Guardrail: include a constraint in the prompt to minimize rework—prefer retry of the failed sub-step over full branch replanning unless evidence shows upstream steps are invalidated.

06

Not a Replacement for Transaction Rollback

What to watch: the prompt produces a textual recovery plan, not an atomic rollback mechanism. If your system requires ACID guarantees, the prompt cannot provide them. Guardrail: use this prompt for workflow-level recovery guidance only. Actual compensation execution must happen in your application layer with proper error handling and idempotency keys.

PROMPT PLAYBOOK

Copy-Ready Prompt Template

A copy-ready prompt template for generating a recovery plan when a long-running agent step partially succeeds.

This prompt instructs the model to act as a plan-patching agent. Its job is to analyze a partially completed step, identify what succeeded and what failed, and produce a structured recovery plan. The recovery plan must account for applied side effects, pending retries, and any required compensation or rollback actions. Use this template as the core instruction block in your agent's replanning module, wiring it to execute after a step returns a partial-success status.

code
You are an agent plan patching specialist. Your task is to generate a recovery plan when a step in a long-running workflow partially succeeds. Some side effects have been applied, while others have failed.

## INPUT
- Original Plan Step: [ORIGINAL_STEP]
- Execution Result: [EXECUTION_RESULT]
- Completed Side Effects: [COMPLETED_SIDE_EFFECTS]
- Failed Side Effects: [FAILED_SIDE_EFFECTS]
- Remaining Plan Steps: [REMAINING_PLAN_STEPS]
- Available Tools: [AVAILABLE_TOOLS]
- System Constraints: [CONSTRAINTS]

## OUTPUT SCHEMA
Return a JSON object with the following structure:
{
  "state_assessment": {
    "step_status": "partially_completed",
    "completed_actions": ["list of actions that succeeded"],
    "failed_actions": ["list of actions that failed"],
    "applied_side_effects": ["list of side effects already applied to the system"],
    "rollback_required": true_or_false,
    "rollback_reason": "explanation if rollback is required, otherwise null"
  },
  "recovery_plan": {
    "strategy": "retry_and_continue | rollback_and_retry | compensate_and_skip | escalate",
    "compensation_steps": [
      {
        "step_id": "unique identifier",
        "action": "description of the compensation action",
        "tool": "tool name or null",
        "target_side_effect": "which side effect this compensates for",
        "idempotency_key": "key to prevent duplicate execution"
      }
    ],
    "retry_steps": [
      {
        "step_id": "unique identifier",
        "action": "description of the retry action",
        "tool": "tool name",
        "modified_parameters": {},
        "precondition_checks": ["checks to run before retry"]
      }
    ],
    "updated_remaining_plan": ["adjusted list of remaining steps"],
    "checkpoint_instruction": "what state to save before executing recovery"
  },
  "risk_assessment": {
    "data_loss_risk": "low | medium | high",
    "duplicate_action_risk": "low | medium | high",
    "requires_human_approval": true_or_false,
    "approval_reason": "explanation if human approval is needed"
  }
}

## CONSTRAINTS
- Do not propose actions that would duplicate already-applied side effects.
- If a side effect is irreversible, mark rollback_required as false and explain why compensation is the only path.
- Prefer idempotent retry steps whenever possible.
- If the failure mode is unclear, request human approval before proceeding.
- Preserve all successfully completed work unless rollback is strictly required.
- If the estimated recovery cost exceeds [MAX_RECOVERY_COST], escalate instead of patching.

## EXAMPLES
[EXAMPLES]

Generate the recovery plan.

To adapt this template for your runtime, replace each square-bracket placeholder with live data from your agent's execution context. [ORIGINAL_STEP] should contain the full step definition that was attempted. [EXECUTION_RESULT] must include the raw output, error messages, and status codes. [COMPLETED_SIDE_EFFECTS] and [FAILED_SIDE_EFFECTS] require accurate state tracking—your agent must maintain a side-effect ledger to populate these correctly. [EXAMPLES] should include at least two few-shot demonstrations: one showing a safe retry-and-continue patch and one showing a rollback-and-compensate scenario. If your domain involves irreversible operations (e.g., sending emails, charging payments), add explicit constraints to the [CONSTRAINTS] block that forbid retries without human approval. After pasting this prompt, validate the output against the JSON schema before passing it to your execution engine. If the model returns a strategy of "escalate," route to a human operator immediately rather than attempting automated recovery.

IMPLEMENTATION TABLE

Prompt Variables

Each placeholder must be populated from the agent runtime before calling the model. Missing or stale values cause incorrect recovery plans.

PlaceholderPurposeExampleValidation Notes

[ORIGINAL_PLAN]

The full multi-step plan that was being executed before partial completion occurred

{"steps": [{"id": "1", "tool": "create_pr", "status": "completed"}, {"id": "2", "tool": "add_reviewers", "status": "failed"}]}

Must be valid JSON with step IDs, tool names, and status fields. Reject if plan structure is missing or unparseable

[COMPLETED_STEPS]

List of steps that succeeded with their outputs and side effects applied

[{"step_id": "1", "output": {"pr_url": "https://..."}, "side_effects": ["branch_created", "pr_opened"]}]

Each entry must include step_id matching ORIGINAL_PLAN. Side effects list must be non-empty for stateful operations. Null allowed for read-only steps

[FAILED_STEPS]

List of steps that partially or fully failed with error details and partial side effects

[{"step_id": "2", "error": "permission_denied", "partial_effects": ["reviewer_list_fetched"], "failure_point": "write_operation"}]

Error field required. Partial effects must be enumerated even if empty array. Failure point must indicate where in the step execution the break occurred

[CURRENT_STATE]

Snapshot of system state after partial completion including resource states and data artifacts

{"repository": "org/repo", "branch": "feature/x", "pr_exists": true, "reviewers_added": false}

Must be a flat key-value map of state variables. Boolean fields must use true or false. Reject if state contradicts COMPLETED_STEPS side effects

[AVAILABLE_TOOLS]

Full tool manifest with schemas, permissions, and current availability status for recovery planning

[{"name": "add_reviewers", "available": false, "reason": "rate_limited"}, {"name": "request_review_manually", "available": true}]

Each tool must declare availability as boolean. Unavailable tools must include reason. Schema must match runtime tool registry. Reject if tool list is stale relative to execution environment

[CONSTRAINTS]

Active constraints including time budgets, cost limits, permission boundaries, and policy rules

{"max_retries": 3, "deadline_remaining_seconds": 120, "disallowed_actions": ["force_push"], "require_human_approval_for": ["delete_branch"]}

Deadline must be numeric seconds remaining. Disallowed actions list must be explicit. Human approval triggers must reference specific action names matching tool manifest

[ROLLBACK_CAPABILITIES]

Available compensation operations for undoing side effects with their current feasibility

[{"action": "close_pr", "feasible": true, "requires_approval": false}, {"action": "delete_branch", "feasible": true, "requires_approval": true}]

Each capability must declare feasibility as boolean. Approval requirements must match CONSTRAINTS. Reject if capability list includes operations not present in COMPLETED_STEPS side effects

[EXECUTION_LOG]

Ordered log of all runtime events including tool calls, responses, errors, and state changes during the partial execution

[{"timestamp": "...", "event": "tool_call", "step_id": "1", "tool": "create_pr", "args": {...}}, {"timestamp": "...", "event": "tool_error", "step_id": "2", "error": "permission_denied"}]

Must be chronologically ordered. Each event must reference step_id from ORIGINAL_PLAN. Error events must include error message. Reject if log has gaps between step start and completion or error events

PROMPT PLAYBOOK

Implementation Harness Notes

How to wire the plan-patching prompt into an agent execution loop with validation, retries, and safe state management.

The plan-patching prompt is not a standalone utility; it is a decision node inside an agent's execution loop. When a step returns a partial completion signal—some side effects applied, others failed—the runtime must pause execution, capture the current state, and invoke this prompt to produce a recovery plan. The harness is responsible for providing the prompt with an accurate snapshot of completed work, failed operations, and the original plan context. Without this harness discipline, the model receives stale or incomplete information and generates patches that either duplicate completed work or skip necessary compensation steps.

Integration pattern. Wire the prompt as a synchronous call within the agent's step-completion handler. After each step, the runtime evaluates the step's output against its expected postconditions. If the step reports partial completion, the handler constructs the prompt's input payload: the original plan with step IDs, the completed sub-steps with their side-effect descriptions, the failed sub-steps with error details, and any relevant tool schemas for compensation actions. The model's output should conform to a strict schema—typically a JSON object containing a recovery_plan array of patched steps, a compensation_required boolean, and a state_summary string. Validate this output before acting on it: check that every step ID in the patch references either an existing plan step or a new compensation step with a defined tool, that no completed-and-verified step is marked for re-execution unless explicitly justified, and that the compensation steps are ordered correctly (undo before redo, dependent rollbacks sequenced properly).

Retry and escalation logic. If the model's output fails schema validation, retry once with the validation errors appended to the prompt as additional constraints. If the retry also fails, do not execute a best-guess patch—escalate to a human operator with the partial state, the failed validation details, and the original plan. For high-risk domains (finance, healthcare, infrastructure), require human approval before executing any compensation step that modifies external state, even when the model's output passes validation. Log every patch invocation with the input state, the model's raw output, the validation result, and the human approval decision. This audit trail is essential for debugging plan drift and for demonstrating governance compliance. Model choice matters: use a model with strong instruction-following and structured output capabilities (such as GPT-4o or Claude 3.5 Sonnet) for plan-patching decisions, and set temperature low (0.0–0.2) to reduce variance in recovery plan generation.

State management and idempotency. Before executing any patched step, the harness must check whether the step's intended side effect has already been applied. Partial completions often leave ambiguous state—a database write may have succeeded while the confirmation message failed. Implement idempotency keys or state-verification checks before re-running any operation. If the patch includes a rollback step, verify that the resource to be rolled back still exists and is in the expected state. The harness should also update the agent's progress tracker immediately after each patched step completes, maintaining a clear distinction between original-plan steps and patch-injected steps. This prevents the agent from losing track of what was part of the initial plan versus what was added during recovery, which is a common source of infinite patching loops.

IMPLEMENTATION TABLE

Expected Output Contract

The model must return a JSON object matching this schema. Validate before executing any recovery action.

Field or ElementType or FormatRequiredValidation Rule

recovery_plan_id

string (UUID v4)

Must be a valid UUID v4 string. Reject if missing or malformed.

plan_summary

string

Must be 1-3 sentences. Reject if empty or >500 characters.

completed_steps

array of objects

Each object must have 'step_id' (string) and 'status' (enum: 'completed', 'partially_completed'). Array must not be empty if any step succeeded.

completed_steps[].side_effects_applied

array of strings

Each string must describe a confirmed side effect. Reject if 'partially_completed' and array is empty.

failed_steps

array of objects

Each object must have 'step_id' (string), 'failure_mode' (string), and 'failure_timestamp' (ISO 8601). Array can be empty if no failures.

compensation_required

boolean

Must be true if any completed step has irreversible side effects that conflict with the remaining plan. Reject if null.

compensation_steps

array of objects

Required if compensation_required is true. Each object must have 'step_id' (string), 'action' (string), and 'target_side_effect' (string). Reject if missing when compensation_required is true.

retry_steps

array of objects

Each object must have 'original_step_id' (string), 'retry_strategy' (enum: 'retry', 'retry_with_backoff', 'alternative_approach', 'skip'), and 'modified_parameters' (object or null). Reject if retry_strategy is 'alternative_approach' and modified_parameters is null.

PRACTICAL GUARDRAILS

Common Failure Modes

Partial completion is one of the hardest agent states to recover from. These failures break the recovery prompt before it can produce a safe plan.

01

Incomplete Side-Effect Inventory

What to watch: The model lists only the most recent or most visible side effects and misses earlier mutations—database writes, file creations, or state changes from steps 1–3 that already succeeded. The recovery plan then proposes retries that duplicate work or skip required compensation. Guardrail: Require the prompt to enumerate every step in the original plan with an explicit status (completed, partial, untouched) before generating the patch. Validate that the count matches the original plan length.

02

Compensation Blindness

What to watch: The model correctly identifies what failed but proposes only retry logic without recognizing that partial side effects require compensation—an undo, a reversal API call, or a compensating transaction. This leaves the system in an inconsistent state even after the patch completes. Guardrail: Include a hard constraint in the prompt: for every step marked partial, require a compensation action or an explicit justification for why compensation is unnecessary. Validate output with a schema field compensation_required: boolean per step.

03

Rollback Scope Creep

What to watch: The model overcorrects by proposing a full rollback of all completed steps when only one step partially failed. This wastes completed work, increases latency, and may trigger unnecessary side effects from the rollback itself. Guardrail: Add a cost-of-rollback scoring heuristic in the prompt: ask the model to estimate the number of steps that would be undone and prefer patches that preserve completed work unless data consistency demands otherwise. Test with a case where 4 of 5 steps succeeded cleanly.

04

Idempotency Assumption Without Verification

What to watch: The model assumes retrying a partially completed step is safe because it guesses the operation is idempotent. If the step created a resource with a unique constraint or incremented a counter, the retry fails with a different error or creates duplicates. Guardrail: Prompt must ask: 'Is this step safe to retry without side effects? If unknown, flag for human review.' Include a required output field retry_safe: boolean | null and treat null as unsafe by default in the harness.

05

Ordering Violations in Recovery

What to watch: The recovery plan reorders compensation or retry steps in a way that violates the original dependency graph—undoing a resource before the step that depends on it has been unwound, or retrying a downstream step before its prerequisite is restored. Guardrail: Include the original dependency graph in the prompt context and require the patch to respect all edges. Validate the proposed patch order against the graph programmatically before execution.

06

Silent State Drift After Patch

What to watch: The patch completes without errors but the system state no longer matches what the plan assumes. A retried step succeeded with different parameters, or a compensation left a resource in an unexpected intermediate state. Downstream steps then operate on stale assumptions. Guardrail: Add a post-patch state assertion step: after the recovery plan executes, re-run the precondition checks for all remaining steps. If any precondition fails, escalate rather than continuing.

IMPLEMENTATION TABLE

Evaluation Rubric

Score recovery plan quality before shipping. Run these checks programmatically after model output validation.

CriterionPass StandardFailure SignalTest Method

State Accuracy

Completed steps match ground-truth execution log; no phantom completions or omissions

Recovery plan claims [STEP_ID] completed when execution log shows failure or not started

Diff [COMPLETED_STEPS] against execution log; assert Jaccard similarity >= 0.95

Compensation Completeness

Every irreversible side effect from failed step has a corresponding compensation action

Failed step produced side effect [SIDE_EFFECT_ID] with no matching compensation step in plan

Extract side effects from [FAILED_STEP_OUTPUT]; verify each has compensation step with matching resource ID

Retry Safety

Retry steps include idempotency check or precondition guard before re-execution

Retry step for [STEP_ID] lacks idempotency key or state check in preconditions

Scan retry steps for idempotency marker or precondition block; flag any retry without guard

Rollback Necessity

Rollback is proposed only when irreversible side effects exist; no rollback for read-only or idempotent steps

Plan proposes rollback for [STEP_ID] but step had no write side effects

Classify each failed step as read-only, idempotent-write, or irreversible-write; assert rollback only for irreversible

Dependency Preservation

Recovery plan respects original dependency graph; no step scheduled before its prerequisites complete

Step [STEP_ID] scheduled before prerequisite [PREREQ_ID] marked complete

Build DAG from [DEPENDENCY_GRAPH]; topological sort recovery plan; assert no ordering violations

Resource Conflict Avoidance

No two concurrent steps in recovery plan operate on same mutable resource

Steps [STEP_A] and [STEP_B] both write to [RESOURCE_ID] without sequencing

Extract resource locks from step definitions; detect concurrent write conflicts on same resource

Escalation Appropriateness

Human escalation triggered for irreversible actions, policy violations, or confidence below threshold

Plan auto-executes irreversible compensation without human approval gate

Check [IRREVERSIBLE_ACTIONS] against escalation policy; assert approval gate present or plan rejected

Plan Coherence

Recovery plan is syntactically valid, all step references resolve, no orphaned or dangling steps

Plan references [MISSING_STEP_ID] not defined in step list

Validate plan schema; resolve all step references; assert no undefined references or unreachable steps

ADAPTATION OPTIONS

Adapt This Prompt

How to adapt

Start with the base prompt and a lightweight harness that logs the recovery plan JSON. Use a single model call without retries. Accept any well-formed JSON output during early testing.

Simplify the prompt by removing compensation ordering rules and rollback safety checks. Focus on getting the three core fields right: completed_steps, failed_steps, and recovery_actions.

Watch for

  • Recovery plans that list steps without specifying whether they succeeded or failed
  • Missing compensation_required boolean when side effects exist
  • Overly broad recovery actions like "retry everything" instead of targeted patches
Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.