This prompt is designed for the specific moment inside an agent's execution loop when a tool call has returned an error. Its job is to interpret the raw error code and produce a structured, machine-readable recovery action. The ideal user is a platform engineer or AI reliability engineer building a harness that orchestrates tool use. The required context is the failed tool call's name, its arguments, the exact error code returned, and the available recovery strategies your system supports. Without this context, the prompt cannot make a safe decision between retrying, adjusting arguments, or escalating permanently.
Prompt
Tool Call Error Code Interpretation and Retry Prompt Template

When to Use This Prompt
A guide for platform teams and AI engineers on when to deploy the error-code-to-recovery-strategy prompt inside an agent harness.
Use this prompt when your system needs to distinguish between transient and permanent failures programmatically. For example, a 429 Rate Limit Exceeded error should map to a retry action with a backoff instruction, while a 403 Forbidden or 401 Unauthorized error should map to a permanent escalation, as no argument adjustment will resolve an authentication failure. The prompt is most effective when your harness has a defined taxonomy of error codes and a fixed set of recovery actions, such as RETRY_WITH_BACKOFF, ADJUST_ARGUMENTS_AND_RETRY, or ESCALATE_TO_HUMAN. It is not a general-purpose debugging assistant; it is a decision router for a known failure space.
Do not use this prompt when the error is a malformed JSON syntax error or a schema validation failure. Those failures require a different class of repair prompts that focus on structural correction of the payload itself, not on interpreting a semantic error code from an external service. Similarly, avoid this prompt when the tool call succeeded but returned an unexpected business-logic result; that is an output validation problem, not a tool-call error recovery scenario. After implementing this prompt, your next step should be to wire it into a retry loop with a strict budget and to add evaluation cases that verify the correct recovery action is chosen for each error code in your taxonomy.
Use Case Fit
Where this prompt works and where it does not. Use these cards to decide if the Tool Call Error Code Interpretation and Retry Prompt Template is the right fit for your recovery architecture.
Good Fit: Structured Error Taxonomies
Use when: Your tool execution layer returns well-defined error codes (e.g., HTTP status, gRPC codes, custom API error enums) that map cleanly to retryable vs. permanent failure categories. Guardrail: Maintain a machine-readable error taxonomy file that the prompt references, ensuring the model always has the latest code-to-action mapping.
Bad Fit: Unstructured Error Messages
Avoid when: Your tools return free-text error messages without consistent codes or categories. The prompt relies on code-based routing; parsing natural-language errors introduces ambiguity and misclassification risk. Guardrail: Use a schema repair or output contract violation prompt instead, or add an error normalization layer before this retry step.
Required Inputs
What you need: The original failed tool call payload, the exact error code returned, the tool's schema definition, and the current retry count within the budget. Guardrail: Validate that the error code is present and recognized before invoking the prompt. If the code is unknown, escalate immediately rather than guessing a recovery strategy.
Operational Risk: Retry Budget Exhaustion
Risk: The prompt may suggest retries for transient errors (e.g., 429, 503) but the underlying issue persists, burning through the retry budget without resolution. Guardrail: Pair this prompt with a retry budget exhaustion escalation prompt. Track retry counts per tool call and force escalation when the budget is consumed, preserving partial results and failure context.
Operational Risk: Misclassification of Permanent Errors
Risk: The model misclassifies a permanent error (e.g., 400 Bad Request, 401 Unauthorized) as retryable, causing repeated failures and wasted compute. Guardrail: Pre-define permanent error codes in the taxonomy and instruct the prompt to treat them as non-retryable. Add a validation step that blocks retries for codes on the permanent list.
Not a Replacement for Circuit Breakers
Risk: Teams treat the prompt as the sole retry mechanism, ignoring infrastructure-level circuit breakers and backoff strategies. Guardrail: Implement circuit breakers and exponential backoff at the harness level, independent of the prompt. The prompt should only decide what recovery action to take, not control retry timing or rate limiting.
Copy-Ready Prompt Template
A reusable prompt that maps tool error codes to specific recovery strategies and produces a targeted retry instruction.
This prompt template is designed to be injected into your agent harness immediately after a tool call returns an error code. Instead of blindly retrying with the same arguments or escalating every failure, the prompt forces the model to classify the error, determine if it is retryable, and produce a corrected tool call only when recovery is possible. The template uses square-bracket placeholders so you can wire it directly into your error-handling middleware without manual rewriting.
textYou are an agent error recovery specialist. A tool call has failed with an error code. Your job is to interpret the error, decide whether a retry is appropriate, and produce a corrected tool call if recovery is possible. ## Error Context - Tool Name: [TOOL_NAME] - Original Arguments: [ORIGINAL_ARGUMENTS] - Error Code: [ERROR_CODE] - Error Message: [ERROR_MESSAGE] - Attempt Number: [ATTEMPT_NUMBER] - Retry Budget Remaining: [RETRIES_REMAINING] ## Available Tool Schema [TOOL_SCHEMA] ## Conversation Context [CONVERSATION_CONTEXT] ## Error Code Taxonomy Use this taxonomy to classify the error and determine the recovery action: | Category | Error Codes | Retryable? | Recovery Strategy | |----------|-------------|------------|-------------------| | TRANSIENT | 429, 503, 504, TIMEOUT | Yes | Retry with exponential backoff; preserve original arguments; reuse idempotency key if present | | VALIDATION | 400, 422, INVALID_ARGUMENT, SCHEMA_MISMATCH, TYPE_ERROR, ENUM_VIOLATION, MISSING_REQUIRED | Yes | Correct the specific argument that failed validation; leave valid fields intact; do not invent new values | | AUTHENTICATION | 401, 403, UNAUTHORIZED, FORBIDDEN | No | Do not retry; escalate with auth failure summary | | NOT_FOUND | 404, TOOL_NOT_FOUND, ENDPOINT_NOT_FOUND | Conditional | If a similar tool exists in the available tool schema, substitute it; otherwise escalate | | PERMANENT | 500, 501, 502, INTERNAL, UNIMPLEMENTED, DEPRECATED | No | Do not retry; escalate with failure summary | | QUOTA | 429_QUOTA, RATE_LIMITED, RESOURCE_EXHAUSTED | Conditional | Retry only if retry budget >= 2 and a backoff duration is provided; otherwise escalate | | DEPENDENCY | DEPENDENCY_FAILURE, UPSTREAM_ERROR, CHAIN_FAILURE | Conditional | Identify the failed dependency; if it can be re-executed with corrected arguments, produce a dependency retry plan; otherwise escalate | | AMBIGUOUS | AMBIGUOUS_INPUT, MULTIPLE_MATCHES, DISAMBIGUATION_REQUIRED | Conditional | If conversation context resolves the ambiguity, produce disambiguated arguments; otherwise request clarification | | TIMEOUT | TOOL_TIMEOUT, EXECUTION_TIMEOUT | Conditional | Retry once with original arguments and a timeout-adjustment note; if partial results are available, include them | ## Output Schema Return a JSON object with this exact structure: { "error_category": "<one of: TRANSIENT, VALIDATION, AUTHENTICATION, NOT_FOUND, PERMANENT, QUOTA, DEPENDENCY, AMBIGUOUS, TIMEOUT>", "retryable": true or false, "action": "<RETRY | ESCALATE | REQUEST_CLARIFICATION | DEPENDENCY_RETRY | TOOL_SUBSTITUTION>", "corrected_tool_call": { "tool_name": "<string>", "arguments": {} } or null, "correction_summary": "<one-sentence explanation of what was changed and why>", "escalation_payload": { "failure_summary": "<string>", "original_error": "<string>", "attempts_exhausted": true or false, "recommended_human_action": "<string>" } or null, "clarification_question": "<string>" or null } ## Constraints - If the error is retryable and you can produce a corrected call, set `action` to RETRY and populate `corrected_tool_call`. - If the error is permanent or the retry budget is exhausted, set `action` to ESCALATE and populate `escalation_payload`. - If the error requires user clarification, set `action` to REQUEST_CLARIFICATION and populate `clarification_question`. - If a dependency failure can be repaired, set `action` to DEPENDENCY_RETRY and include the dependency repair plan in `correction_summary`. - If tool substitution is appropriate, set `action` to TOOL_SUBSTITUTION and populate `corrected_tool_call` with the alternative tool. - Never invent tool names, endpoints, or argument values that are not present in the available tool schema or conversation context. - Preserve all valid arguments from the original call; only modify the fields that caused the error. - If the error code does not match any taxonomy entry, classify it as PERMANENT and escalate.
To adapt this template for your harness, replace each square-bracket placeholder with live data from your error handler. [TOOL_SCHEMA] should contain the full JSON Schema or function definition for the tool that failed. [CONVERSATION_CONTEXT] should include the last N user and assistant messages so the model can infer missing parameters or resolve ambiguities. [RETRIES_REMAINING] is critical: when it reaches zero, the taxonomy rules force escalation regardless of error category, preventing infinite retry loops. If your system uses custom error codes beyond the taxonomy, extend the table before deploying. Always validate the model's output against the output schema before executing a retry; a malformed recovery response should itself trigger escalation.
Prompt Variables
Inputs required to map tool error codes to retry strategies. Each placeholder must be populated before the prompt is assembled and sent to the model.
| Placeholder | Purpose | Example | Validation Notes |
|---|---|---|---|
[ERROR_CODE] | The exact error code returned by the tool or API | INVALID_ARGUMENT | Must be a non-empty string. Validate against the known error taxonomy before injection. |
[ERROR_MESSAGE] | The raw error message body from the tool response | Field 'user_id' is required but was missing. | Must be a non-empty string. Truncate to 500 characters to avoid context pollution. |
[TOOL_NAME] | The name of the tool that returned the error | create_customer_record | Must match a tool name in the current tool catalog. Reject if the tool name is not recognized. |
[ORIGINAL_ARGUMENTS] | The full JSON arguments payload that caused the failure | {"email": "test@example.com"} | Must be valid JSON. Parse and validate before injection. Redact PII if present in logs. |
[TOOL_SCHEMA] | The complete JSON Schema for the tool's expected input | {"type": "object", "required": ["user_id", "email"]} | Must be a valid JSON Schema object. Inject the canonical schema from the tool registry, not a cached copy. |
[ERROR_TAXONOMY] | A mapping of error code categories to retry actions | {"INVALID_ARGUMENT": "retry_with_correction", "PERMISSION_DENIED": "escalate"} | Must be a valid JSON object. Each key must be an error code string. Values must be one of: retry_with_correction, escalate, retry_with_backoff, abort. |
[RETRY_BUDGET_REMAINING] | The number of retry attempts left before escalation is forced | 2 | Must be a non-negative integer. If 0, the prompt should produce an escalation decision regardless of error type. |
[CONVERSATION_CONTEXT] | The last 3 user-assistant turns for intent preservation | User: Create a new customer record... | Must be a string or array of turns. Omit if unavailable. Truncate to 1000 tokens to preserve budget for error details. |
Implementation Harness Notes
How to wire the error code interpretation prompt into a production retry loop with validation, routing, and escalation.
This prompt is designed to sit inside a tool-execution retry harness, not as a standalone chat interaction. When a tool call returns an error code, the harness should intercept the failure, classify the error category, and inject the relevant error context into this prompt before re-invoking the model. The harness is responsible for enforcing retry budgets, preventing infinite loops, and routing permanent failures to human review or dead-letter queues. Do not expose raw error traces directly to end users; the harness should sanitize sensitive details while preserving enough diagnostic information for the model to produce a useful recovery action.
The implementation flow follows a strict sequence: (1) execute the tool call and capture the response; (2) if the response contains an error code, classify it against your error taxonomy (retryable, permanent, auth, rate-limit, schema, timeout, dependency); (3) select the corresponding recovery strategy from your mapping table; (4) populate the prompt template with the original tool call, the error code, the error message, the recovery strategy, and any relevant tool schema; (5) invoke the model with the populated prompt; (6) validate the model's output against the expected recovery action schema before executing the retry; (7) increment the retry counter and check against the budget; (8) if the budget is exhausted, route to the escalation path instead of retrying. Each step should emit structured logs with trace IDs, retry counts, error categories, and recovery actions for observability.
For validation, the harness must check that the model's recovery output contains a valid action type (retry, modify_and_retry, switch_tool, clarify, escalate), preserves the original intent parameters where applicable, and does not hallucinate new tool names or endpoints not present in the available tool catalog. Implement a schema validator that rejects recovery actions referencing non-existent functions or proposing argument mutations that violate the target tool's parameter constraints. For high-risk domains such as financial transactions or healthcare operations, insert a human approval gate before executing any modify_and_retry or switch_tool action that alters the original call's semantic intent. Log every validation failure as a separate event to distinguish model errors from tool errors in your observability pipeline.
Model choice matters here: use a model with strong instruction-following and structured output capabilities, such as Claude 3.5 Sonnet or GPT-4o, because the recovery action must conform to a strict schema under error conditions. Lighter models often hallucinate tool names or produce recovery actions that don't match the error category. If latency is critical, consider routing permanent errors and escalation decisions to a faster model while reserving the stronger model for complex retry scenarios like dependency failures or schema mismatches. Always set a timeout on the model invocation itself; if the model fails to produce a valid recovery action within the timeout window, treat that as an escalation trigger rather than retrying the prompt.
The most common production failure mode is error category misclassification at step 2, which feeds the wrong recovery strategy into the prompt and produces an inappropriate retry action. Test your error taxonomy against real tool error responses before deploying, and add a periodic review of misclassifications from production traces. A secondary failure mode is argument drift across retries, where each recovery cycle subtly changes the original intent until the retried call no longer serves the user's goal. Mitigate this by storing the original validated arguments at the start of the retry loop and diffing each recovery action against them, flagging any semantic deviation above a configurable threshold for human review.
Expected Output Contract
Defines the required fields, types, and validation rules for the structured retry instruction payload produced by the Tool Call Error Code Interpretation and Retry Prompt Template.
| Field or Element | Type or Format | Required | Validation Rule |
|---|---|---|---|
error_code | string | Must match one of the predefined error codes in the taxonomy (e.g., INVALID_ARGUMENT, TOOL_NOT_FOUND, PERMISSION_DENIED). | |
error_category | string | Must be exactly RETRYABLE or PERMANENT. No other values allowed. | |
recovery_action | string | Must be a non-empty string from the allowed action set: CORRECT_AND_RETRY, SUBSTITUTE_TOOL, REQUEST_CLARIFICATION, or ESCALATE_TO_HUMAN. | |
corrected_tool_call | object | Required if recovery_action is CORRECT_AND_RETRY or SUBSTITUTE_TOOL. Must validate against the target tool's JSON Schema. Must be null otherwise. | |
clarification_question | string | Required if recovery_action is REQUEST_CLARIFICATION. Must be a single, unambiguous question addressed to the user. Must be null otherwise. | |
escalation_reason | string | Required if recovery_action is ESCALATE_TO_HUMAN. Must summarize the failure chain and why automatic recovery is impossible or unsafe. Must be null otherwise. | |
alternative_tool_name | string | Required if recovery_action is SUBSTITUTE_TOOL. Must be the exact name of an available tool from the provided tool catalog. Must be null otherwise. | |
retry_confidence | number | Must be a float between 0.0 and 1.0. Represents the model's confidence that the recovery action will succeed. Values below 0.5 should trigger a review log entry. |
Common Failure Modes
Tool call error code interpretation breaks when the model misclassifies the error, retries non-retryable failures, or ignores the recovery action map. These cards cover the most frequent failure patterns and how to prevent them in production harnesses.
Retrying Non-Retryable Errors
What to watch: The model treats permanent errors (invalid credentials, resource not found, schema mismatch) as transient and retries with the same payload. This wastes compute, increases latency, and never resolves. Guardrail: Inject a hard error code taxonomy into the prompt that explicitly labels each code as RETRYABLE or FATAL. Add a harness-level circuit breaker that counts FATAL classifications and blocks further retries after the first occurrence.
Error Code Misclassification
What to watch: The model conflates similar HTTP status codes or tool-specific error codes (e.g., treating a 503 Service Unavailable as a 400 Bad Request) and applies the wrong recovery strategy. Guardrail: Include a mapping table in the prompt with exact error codes, their categories, and the required recovery action. Validate the model's classification against this table before executing the retry. Log mismatches for taxonomy refinement.
Ignoring the Recovery Action Map
What to watch: The model acknowledges the error code correctly but generates a generic retry instead of following the specific recovery action prescribed for that code (e.g., re-authenticating on 401 instead of just retrying). Guardrail: Structure the prompt so the recovery action is a required output field. Use a validator to confirm the action matches the expected action for the classified error code. On mismatch, re-prompt with the specific action requirement emphasized.
Argument Drift Across Retries
What to watch: When the model modifies arguments to fix an error, it unintentionally changes semantically meaningful values, causing the retry to succeed but with incorrect business logic. Guardrail: Require the prompt to output a diff of changed arguments with justifications. Implement a harness check that flags any modified argument not directly related to the error's root cause. For high-risk parameters, lock them to their original values.
Retry Loop Exhaustion Without Escalation
What to watch: The retry budget is consumed by repeated attempts that vary slightly but never converge. The harness either loops indefinitely or fails silently without preserving context for human review. Guardrail: Define a maximum retry count in the harness. On exhaustion, capture the full error history, the last proposed recovery action, and all attempted payloads. Format this into an escalation payload and halt further automated retries.
Error Message Injection Failures
What to watch: The raw error message from the tool contains stack traces, internal IPs, or sensitive data. Injecting this directly into the retry prompt leaks information or confuses the model with noise. Guardrail: Sanitize error messages before injection. Extract only the error code, the failing field or parameter name, and a clean constraint description. Use a structured error context block in the prompt template to prevent raw trace leakage.
Evaluation Rubric
Criteria for evaluating whether the error-code interpretation and retry prompt produces correct, safe, and actionable recovery instructions before shipping to production.
| Criterion | Pass Standard | Failure Signal | Test Method |
|---|---|---|---|
Error Code Classification Accuracy | Prompt correctly maps each error code in [ERROR_CODE_TAXONOMY] to its retryable or permanent category | Output misclassifies a permanent error as retryable or vice versa, or invents error codes not present in the taxonomy | Run 20 labeled error-code inputs; require >=95% classification accuracy against ground-truth categories |
Recovery Action Correctness | For retryable errors, output specifies a concrete, non-destructive recovery action that addresses the specific error code | Output suggests retrying without modification for a parameter error, or proposes an action that would corrupt state or violate idempotency | Manual review of 10 recovery actions by an SRE; all must pass a safety and correctness checklist |
Permanent Error Escalation Format | For permanent errors, output includes a structured escalation payload with error summary, original intent, and recommended human action | Output attempts a retry for a permanent error, or escalation payload omits the original tool call intent or error evidence | Validate escalation payload against [ESCALATION_SCHEMA] for 15 permanent-error cases; 100% schema conformance required |
Original Intent Preservation | Retry instruction preserves the original [TOOL_CALL_INTENT] and [ORIGINAL_ARGUMENTS] without semantic drift | Output changes the tool, alters argument semantics, or drops required parameters that were present in the original call | Compare original and retry argument sets using semantic equivalence check; require >=90% intent preservation score |
Idempotency Key Handling | When [IDEMPOTENCY_KEY] is provided, retry instruction includes the same key; when absent, output warns about potential duplicate side effects | Output generates a new idempotency key, drops an existing one, or fails to warn when no key is present for a non-idempotent tool | Test 10 cases with and without keys; verify key preservation and warning presence per specification |
Retry Budget Awareness | Output respects [RETRY_COUNT] and [MAX_RETRIES]; when budget is exhausted, output escalates rather than suggesting another retry | Output proposes a retry when retry count equals or exceeds max retries, or fails to include remaining budget in the instruction | Run 8 budget-exhaustion scenarios; 100% must produce escalation, not retry |
Error Message Evidence Inclusion | Retry instruction includes the specific error message from [ERROR_MESSAGE] as grounding for the correction | Output proposes a fix without referencing the error, or hallucinates an error message not present in the input | Check 12 outputs for exact or paraphrased inclusion of the provided error message; require 100% evidence grounding |
Tool Schema Conformance After Repair | Corrected arguments in the retry instruction validate against [TOOL_SCHEMA] with zero schema violations | Output introduces new schema violations, removes required fields, or changes field types during repair | Validate 20 repaired argument sets against their tool schemas programmatically; require 100% schema conformance |
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Adapt This Prompt
How to adapt
Add structured error-code taxonomy with per-code recovery actions, schema validation of the corrected call, retry budget tracking, and logging. Inject the full tool schema as [TOOL_SCHEMA], the original arguments as [ORIGINAL_ARGS], and the validation error as [VALIDATION_ERROR]. Require the model to produce a corrected JSON payload that passes schema validation before retry. Include a retry_count field in the harness to enforce budget limits.
Prompt snippet
codeYou are a tool-call error recovery system. The tool call with arguments [ORIGINAL_ARGS] failed against schema [TOOL_SCHEMA] with error code [ERROR_CODE] and message [ERROR_MESSAGE]. Recovery rules by error category: - INVALID_ARGUMENT: Map invalid values to closest valid enum or type. If ambiguous, set should_retry to false and request clarification. - MISSING_REQUIRED: Infer from conversation context [CONVERSATION_CONTEXT] if confidence > 0.8. Otherwise escalate. - TIMEOUT: Retry with same arguments and idempotency key [IDEMPOTENCY_KEY]. Max 2 retries. - RATE_LIMITED: Apply exponential backoff. Retry after [RETRY_AFTER] seconds. - PERMISSION_DENIED: Do not retry. Escalate with failure summary. - SERVER_ERROR: Retry up to 3 times with jitter. Escalate on exhaustion. Return JSON: {"should_retry": boolean, "corrected_args": object, "escalation_reason": string | null, "retry_after_seconds": number | null}
Watch for
- Silent format drift in corrected_args that fails re-validation
- Retry loops on non-retryable errors due to missing category mapping
- Missing human review escalation when retry budget is exhausted

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.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us