Inferensys

Prompt

Action Failure Recovery Prompt for Autonomous Agents

A practical prompt playbook for recovering autonomous agents from tool execution failures with diagnostic summaries and corrected action attempts.
Developer demonstrating multi-agent tool use, agent tool selection interface on laptop, casual tech demo moment.
PROMPT PLAYBOOK

When to Use This Prompt

Defines the precise operational context for deploying the Action Failure Recovery Prompt within an autonomous agent harness.

This prompt is designed for a specific failure mode inside an autonomous agent's execution loop: a single tool call returns an error. The job-to-be-done is not to re-plan the entire task or question the agent's objective, but to produce a structured, machine-readable diagnostic of the failure and a single corrected action attempt. The ideal user is an AI reliability engineer or agent-framework builder who has already instrumented their harness to capture the original goal, the failed action's intent, the exact function name, the arguments sent, and the raw error payload (status code, exception message, or malformed response body). The prompt assumes this context is available and injectable via placeholders.

Use this prompt when the agent's tool execution fails with a clear error signal—such as a 4xx/5xx HTTP status, a ValidationError, a TimeoutException, or a response that fails a schema check. The harness should invoke this prompt immediately after catching the error and before checking the retry budget. The output is a diagnostic summary (error classification, root cause hypothesis) and a corrected action (revised arguments or a null action if the tool is fundamentally unusable). The harness then validates the corrected arguments against the tool's schema and, if valid, retries the call. Do not use this prompt for plan-level deviations, multi-step rollbacks, or cases where the agent has lost track of the overall objective. Those scenarios require re-anchoring or re-planning prompts instead, as they involve a broader context collapse that a single-step recovery cannot address.

A concrete implementation check: before wiring this prompt into your agent loop, ensure your harness can distinguish between a tool error and a plan error. A tool error means the agent knew what to do but the external system rejected the attempt. A plan error means the agent's chosen action no longer makes sense given new observations. Feeding a plan error into this prompt will produce a syntactically valid but semantically useless corrected action—the model will fix arguments for a call that shouldn't be made. To avoid this, gate the prompt behind an error-type classifier. If the error is a tool failure, proceed. If it's a plan deviation, route to a re-anchoring prompt instead. Additionally, always run the corrected arguments through a JSON Schema validator before retrying. If the model hallucinates a new parameter or omits a required field, the harness should treat that as a second failure and either re-invoke this prompt with the validation error or escalate to a human reviewer if the retry budget is exhausted.

Next, integrate this prompt into your retry budget logic. A common failure mode is an infinite retry loop where the model repeatedly suggests the same broken correction. To prevent this, the harness should track the hash of the corrected arguments and compare it against previous attempts. If a duplicate is detected, break the loop and escalate. Finally, log the full prompt input, model output, and validation result for every recovery attempt. These traces are essential for debugging why certain error patterns resist self-correction and for building a golden dataset to evaluate future prompt versions. Avoid using this prompt in high-stakes financial transactions or clinical actions without a mandatory human approval step after the first retry failure.

PRACTICAL GUARDRAILS

Use Case Fit

Where the Action Failure Recovery Prompt delivers value and where it introduces risk. Use this to decide if the prompt fits your agent architecture before integrating it into a harness.

01

Good Fit: Deterministic Tool Failures

Use when: the agent receives a structured error from a known tool (e.g., HTTP 400, schema validation failure, timeout code). The prompt excels at interpreting machine-readable error signals and generating a corrected action. Guardrail: Ensure the error payload is passed verbatim into the prompt context; do not summarize or redact error codes.

02

Bad Fit: Ambiguous or Silent Failures

Avoid when: the tool returns a success status but produces semantically wrong output, or when no error signal is generated. The prompt cannot diagnose failures it cannot see. Guardrail: Pair this prompt with an upstream output validator. If validation fails without a clear error, escalate to a human or a separate diagnostic prompt instead.

03

Required Input: Structured Error Context

What to watch: The prompt depends on a complete error record including the original action, the tool name, the error code, and the raw error message. Missing fields produce vague recovery attempts. Guardrail: Build a harness function that assembles a standardized error context object before invoking this prompt. Reject retries if required fields are null.

04

Operational Risk: Retry Loop Amplification

What to watch: An incorrect recovery suggestion can produce a new failure, triggering another retry, leading to a loop that burns compute and latency budget. Guardrail: Implement a retry budget counter in the harness. After N consecutive failures on the same action, bypass the recovery prompt and escalate to a human or a fallback static action.

05

Operational Risk: Argument Drift

What to watch: The recovery prompt may alter arguments that were correct while fixing the one that failed, subtly changing the agent's intent. Guardrail: Diff the original action arguments against the corrected arguments. If more than one field changed, flag for human review before execution. Log all argument changes for audit.

06

Good Fit: Agent Harnesses with Eval Gates

Use when: your agent architecture already includes evaluation checks for action correctness, schema compliance, and intent preservation. The recovery prompt's output can be validated before execution. Guardrail: Run the corrected action through the same eval harness used for initial actions. Reject corrections that fail eval and escalate immediately.

PROMPT PLAYBOOK

Copy-Ready Prompt Template

A copy-ready prompt template for diagnosing an autonomous agent's tool execution failure and generating a corrected action attempt.

This prompt template is designed to be injected into your agent harness immediately after a tool call returns an error. Its job is to produce a structured diagnostic summary and a corrected action attempt, preventing the agent from hallucinating a recovery or blindly retrying the same broken call. Replace the square-bracket placeholders with runtime values from your agent's execution context before sending the prompt to the model.

text
You are an agent recovery module. Your task is to analyze a failed tool execution and produce a corrected action attempt.

## ORIGINAL GOAL
[ORIGINAL_GOAL]

## FAILED ACTION
- Tool: [FAILED_TOOL_NAME]
- Arguments: [FAILED_TOOL_ARGUMENTS]
- Error Code: [ERROR_CODE]
- Error Message: [ERROR_MESSAGE]
- Timestamp: [TIMESTAMP]

## RECENT ACTION HISTORY
[ACTION_HISTORY]

## AVAILABLE TOOLS
[AVAILABLE_TOOLS_SCHEMA]

## CONSTRAINTS
- Do not retry the exact same call without modifying the arguments.
- If the error indicates a permanent failure (e.g., invalid resource ID, permission denied), do not retry. Escalate instead.
- Preserve the original goal. Do not change the objective.
- If the error is transient (e.g., timeout, rate limit), you may suggest a retry with backoff.

## OUTPUT SCHEMA
Return a JSON object with the following structure:
{
  "error_classification": "TRANSIENT" | "PERMANENT" | "UNCLEAR",
  "diagnosis": "A concise explanation of what went wrong, citing the error message.",
  "recovery_action": "RETRY" | "SUBSTITUTE_TOOL" | "REPLAN" | "ESCALATE",
  "corrected_action": {
    "tool_name": "string",
    "arguments": {},
    "retry_count": 0,
    "backoff_seconds": 0
  },
  "escalation_reason": "If recovery_action is ESCALATE, explain why. Otherwise null."
}

Before deploying this prompt, ensure your harness validates the output against the schema. If the model returns an invalid recovery_action or a corrected_action that duplicates the failed call, increment a retry counter and re-invoke the prompt with the validation error appended as additional context. After a configurable retry budget is exhausted (typically 3 attempts), the harness should escalate to a human operator or a higher-level replanning agent. Log every recovery attempt, including the model's diagnosis and the final outcome, to build a dataset for evaluating recovery accuracy over time.

IMPLEMENTATION TABLE

Prompt Variables

Inputs the prompt needs to work reliably. Validate each before assembly.

PlaceholderPurposeExampleValidation Notes

[FAILED_ACTION]

The action that failed, including tool name and arguments

tool: search_database, args: {query: 'SELECT * FROM users WHERE id=null'}

Parse check: must contain tool name and arguments. Null or empty triggers abort.

[ERROR_MESSAGE]

The raw error returned by the tool or execution harness

TypeError: Cannot read properties of null (reading 'id')

Parse check: must be non-empty string. If null, use 'Unknown error' and flag for human review.

[ERROR_CATEGORY]

Classified failure type for routing recovery strategy

INVALID_ARGUMENT

Enum check: must match one of [TIMEOUT, INVALID_ARGUMENT, PERMISSION_DENIED, RATE_LIMITED, TOOL_UNAVAILABLE, MALFORMED_RESPONSE, UNKNOWN]. Invalid category triggers retry with classification prompt.

[ORIGINAL_GOAL]

The agent's current objective from the plan

Retrieve user profile by account ID to verify subscription tier

Schema check: must be non-empty string. Must match the goal stored in agent state. Mismatch triggers re-anchoring.

[PLAN_STEP_NUMBER]

The step index in the current plan that failed

Step 3 of 7

Format check: must match pattern 'Step N of M' where N <= M. Invalid format triggers step reconstruction from plan state.

[RETRY_BUDGET_REMAINING]

Number of retries left before escalation is required

2

Range check: must be integer >= 0. If 0, skip recovery prompt and route to escalation handler.

[AGENT_STATE_SNAPSHOT]

Serialized agent state before the failed action for context

{completed_steps: [...], pending_steps: [...], observations: [...]}

Schema check: must deserialize to valid JSON with completed_steps, pending_steps, and observations arrays. Missing fields trigger state reconciliation.

[TOOL_SCHEMA]

The expected input schema for the tool that failed

{name: 'search_database', parameters: {query: {type: 'string', required: true}}}

Schema check: must be valid JSON Schema. If unavailable, use tool registry lookup. Null allowed only if tool is unregistered.

PROMPT PLAYBOOK

Implementation Harness Notes

How to wire the Action Failure Recovery Prompt into an agent execution loop with validation, retries, and escalation.

The Action Failure Recovery Prompt is designed to be invoked by an agent harness immediately after a tool execution returns an error or an unexpected result. The harness should intercept the tool response, classify the failure type (e.g., timeout, invalid argument, permission denied, empty result), and inject the error context into the prompt's [ERROR_CONTEXT] and [FAILED_ACTION] placeholders. This prompt is not a standalone chatbot instruction; it is a structured recovery step within a larger agent loop. The harness must preserve the original [GOAL], [PLAN_STATE], and [COMPLETED_STEPS] so the model can diagnose the failure without losing sight of the overall objective.

Wire the prompt into your agent's execution loop as a post-error hook. After a tool call fails, construct the prompt payload by merging the error payload, the original action arguments, and the agent's current plan state. Send the prompt to a model with strong instruction-following and structured output capabilities (e.g., GPT-4o, Claude 3.5 Sonnet). The harness must validate the model's output against the [OUTPUT_SCHEMA] before acting on it. At minimum, validate that diagnosis.error_type is a recognized failure class, corrected_action.tool_name is a valid tool in the agent's registry, and corrected_action.arguments conform to the tool's input schema. If validation fails, increment a retry counter and re-invoke the prompt with the validation error appended to [ERROR_CONTEXT]. Do not execute the corrected action until validation passes.

Implement a retry budget with a hard cap (e.g., 3 attempts per action failure). Track each recovery attempt in structured logs that include the original error, the model's diagnosis, the corrected action, and the validation result. If the retry budget is exhausted, the harness must escalate to a human-in-the-loop queue or invoke a fallback policy—do not loop indefinitely. For high-risk domains such as finance, healthcare, or infrastructure, require human approval before executing any corrected action that modifies state, transfers funds, or alters configurations. Log every recovery attempt with a correlation ID that ties the original action, the error, the recovery prompt invocation, and the final outcome together for observability and postmortem analysis.

When deploying this prompt in production, pair it with an evaluation harness that measures error interpretation accuracy and argument correction validity. Run offline evals using a golden dataset of known tool failures and expected corrections. In production, monitor the recovery success rate, the average retry count before success or escalation, and the rate of validation failures on the model's output. These metrics will surface when the prompt needs tuning or when the underlying tool contracts have drifted. If the recovery success rate drops below a defined threshold, trigger an alert and review recent failure patterns before updating the prompt or the tool schemas.

IMPLEMENTATION TABLE

Expected Output Contract

Defines the required fields, types, and validation rules for the diagnostic summary and corrected action attempt produced by the Action Failure Recovery Prompt. Use this contract to parse and validate the model's response before feeding it back into the agent execution loop.

Field or ElementType or FormatRequiredValidation Rule

failure_summary

string

Must be non-empty and contain a concise description of the error. Check length > 10 characters.

error_category

enum: [tool_timeout, invalid_argument, auth_error, rate_limit, partial_result, external_dependency, unknown]

Must match one of the defined enum values exactly. Reject on case mismatch or unknown value.

original_action

object

Must contain the original tool name and arguments. Validate against the agent's tool schema. Reject if tool name is not in the available tool list.

root_cause_analysis

string

Must reference specific error details from the provided [ERROR_CONTEXT]. Check for hallucinated error codes not present in the input.

corrected_action

object

Must contain a valid tool name and corrected arguments. Validate arguments against the tool's input schema. Reject on missing required parameters.

correction_rationale

string

Must explain what was changed and why. Check that the explanation references the root_cause_analysis and is not a generic placeholder.

retry_confidence

number (0.0-1.0)

Must be a float between 0.0 and 1.0. If below 0.7, the agent harness should log a warning and consider escalation.

escalation_recommended

boolean

Must be true if retry_confidence is below 0.5 or if the error_category is auth_error. Harness should route to human review when true.

PRACTICAL GUARDRAILS

Common Failure Modes

What breaks first when autonomous agents hit action failures and how to guard against it.

01

Error Message Misinterpretation

What to watch: The model misclassifies a tool timeout as an authorization error, or treats a partial result as a complete failure, leading to an incorrect recovery strategy. Guardrail: Include the raw error code, message, and tool name in the recovery prompt. Add a classification step before generating the corrected action. Validate that the recovery action matches the error type.

02

Argument Correction Drift

What to watch: The model corrects the invalid argument but silently changes other parameters, altering the original intent of the action. Guardrail: Diff the original failed arguments against the corrected arguments. Require the prompt to explicitly list only the fields that changed and why. Reject corrections that modify fields not mentioned in the error.

03

Retry Loop Without Progress

What to watch: The agent retries the same corrected action repeatedly without advancing the plan, often because the underlying state hasn't changed or the correction is superficial. Guardrail: Implement a retry budget with exponential backoff. After two identical corrections, force the agent to select an alternative tool or escalate. Log the action hash to detect duplicates.

04

Goal Context Loss Over Retries

What to watch: After multiple retries, the agent's context window fills with error-recovery turns, causing it to forget the original objective and produce a syntactically valid but semantically wrong action. Guardrail: Prepend a condensed, immutable goal summary to every recovery prompt. Evaluate the corrected action against the original goal, not just the immediate error.

05

Partial Completion Double-Counting

What to watch: A tool returns a partial success (e.g., 3 of 5 items processed) before failing. The recovery prompt re-executes the entire batch, duplicating the completed work. Guardrail: Require the recovery prompt to ingest the partial result payload. Instruct it to generate a continuation action scoped only to the failed or unprocessed items, using IDs from the partial output.

06

Escalation Threshold Bypass

What to watch: The model generates a confident-sounding but fabricated recovery action when the error is truly unrecoverable, bypassing the human escalation path. Guardrail: Define a clear set of non-retryable error codes (e.g., permanent permission denials, resource exhaustion). If the error matches, the prompt must output an escalation payload, not a retry. Validate the output type before execution.

IMPLEMENTATION TABLE

Evaluation Rubric

Use this rubric to test the Action Failure Recovery Prompt before deploying it in an agent harness. Each criterion targets a specific failure mode common in autonomous agent recovery loops.

CriterionPass StandardFailure SignalTest Method

Error Interpretation Accuracy

Diagnosis correctly identifies the root tool error class from the provided [ERROR_MESSAGE] and [TOOL_NAME]

Diagnosis misclassifies a timeout as an auth error, or attributes the failure to the wrong tool

Run 20 synthetic error cases with known root causes; require >=90% classification accuracy

Argument Correction Validity

Corrected arguments in [CORRECTED_TOOL_CALL] satisfy the tool's schema and preserve the original [ORIGINAL_GOAL] intent

Corrected call omits a required field, uses a wrong type, or changes the semantic intent of the original action

Validate [CORRECTED_TOOL_CALL] against the tool's JSON Schema; flag any schema violations or intent drift

Goal Re-Anchoring Presence

Output includes a [GOAL_REMINDER] that restates the original objective without introducing new sub-goals

[GOAL_REMINDER] is missing, rewrites the original goal, or adds unrequested tasks

Check for [GOAL_REMINDER] field presence; compare embedding similarity to the original goal; require cosine similarity >0.95

Retry Budget Awareness

Output respects [RETRY_BUDGET_REMAINING]; if budget is 0, output [ESCALATION_PAYLOAD] instead of another retry

Output proposes a retry when budget is 0, or fails to include [ESCALATION_PAYLOAD] when required

Parameterized test with budget=0,1,3; assert [ESCALATION_PAYLOAD] present when budget=0 and absent when budget>0

Escalation Payload Completeness

When escalation is triggered, [ESCALATION_PAYLOAD] contains failure summary, actions attempted, and a clear decision request

Escalation payload is empty, omits the failure summary, or asks an unanswerable question

Schema-validate [ESCALATION_PAYLOAD]; require non-null summary, non-empty attempted_actions list, and a question ending with '?'

No Hallucinated Tool State

Diagnosis references only the provided [ERROR_MESSAGE] and [TOOL_INPUT]; does not invent internal system state

Diagnosis claims 'database is down' or 'network partition' when only a timeout error was provided

Human review of 10 complex traces; flag any assertion not directly supported by the error input

Output Schema Compliance

Response is valid JSON matching the expected schema with all required fields present

Response is malformed JSON, missing [CORRECTED_TOOL_CALL], or includes extra top-level keys

Parse response with strict JSON schema validator; reject any response that fails validation

Latency Under Retry Budget

Prompt completes within [MAX_RETRY_LATENCY_MS] to avoid compounding agent loop delays

Response time exceeds the latency budget, causing the agent harness to time out on the retry

Benchmark 50 runs under load; p95 latency must be <= [MAX_RETRY_LATENCY_MS]

ADAPTATION OPTIONS

Adapt This Prompt

How to adapt

Use the base prompt with a single retry attempt and minimal validation. Focus on getting the diagnostic summary and corrected action attempt into a readable shape before adding harness constraints.

Strip the prompt down to:

  • The original action that failed
  • The error message or tool output
  • A simple instruction: "Diagnose the failure and produce a corrected action attempt."

Watch for

  • Overly broad diagnoses that don't pinpoint the failure cause
  • Corrected actions that repeat the same invalid arguments
  • Missing distinction between transient errors (timeout, rate limit) and permanent errors (invalid schema, missing permissions)
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.