As AI agents take on more sophisticated tasks, their ability to operate autonomously and recover from mistakes becomes increasingly important. While previous sections focused on directing agent behavior and thought processes, building truly dependable agents requires them to handle unexpected situations and errors gracefully. This involves designing prompts that not only guide successful execution but also equip agents with mechanisms for self-correction and error handling. By baking these capabilities into the prompt, we reduce the need for constant human oversight and improve the overall resilience of agentic workflows.
The core idea is to move beyond simply instructing an agent on what to do, and also guide it on what to do when things go wrong. This often involves prompting the agent to reflect on its actions, outputs, or the information it has processed, and then take corrective steps if discrepancies or failures are identified.
Several prompting strategies can encourage agents to identify and address errors. These techniques often involve providing the agent with a framework for self-assessment or explicit instructions for handling common failure modes.
One straightforward method is to provide the agent with conditional logic within the prompt itself. This is akin to writing if-then-else
statements that the agent can follow.
For example, when an agent needs to interact with an external tool or API, you can specify how it should react to different error codes or responses:
System: You are an assistant that retrieves stock prices using the `getStockPrice` tool.
Tool available: `getStockPrice(ticker_symbol)`
Instructions:
1. When asked for a stock price, use the `getStockPrice` tool with the provided ticker symbol.
2. If `getStockPrice` returns a "Symbol Not Found" error, inform the user that the ticker symbol is invalid and ask for a correct one.
3. If `getStockPrice` returns a "Service Unavailable" error, wait 5 seconds and try calling the tool one more time.
4. If the retry also fails, or any other error occurs, inform the user that you are unable to retrieve the price at this moment and state the error encountered.
User: What's the price of GOGL?
In this scenario, the agent is explicitly guided on how to interpret tool outputs and what fallback actions to take. This reduces the chance of the agent getting stuck or providing an unhelpful generic error message.
You can prompt an agent to review its own generated content or action plan before finalizing it. This encourages a form of self-critique, where the agent assesses its work against given criteria.
Consider an agent tasked with summarizing a document:
System: You are an AI assistant that summarizes articles. After generating a summary, review it against the following checklist:
- Does the summary capture the main arguments of the article?
- Is the summary approximately 10% of the original article's length?
- Is the summary free of personal opinions or interpretations not present in the original text?
- Are there any repetitive phrases or sentences?
If you find issues, provide a revised summary. Otherwise, confirm the summary meets all criteria.
User: Here is the article: [long article text...] Please summarize it.
The agent first generates a summary and then, guided by the prompt, performs a self-evaluation. This reflective step can catch omissions, stylistic issues, or deviations from the requirements.
Simply retrying an action might not be effective if the underlying cause of failure persists. A more advanced approach is to prompt the agent to modify its approach upon failure.
For instance, if an agent's search query yields no results:
System: You are a research assistant. When searching for information, if your initial query returns no relevant results, follow these steps:
1. Analyze your initial query. Was it too specific or too narrow?
2. Broaden your search terms or try synonyms. For example, if "nanoparticle drug delivery systems" failed, try "nanomedicine drug transport" or "nanocarriers for therapeutics".
3. Perform the search again with the modified query.
4. If still unsuccessful after 2 modified attempts, report that you could not find the information.
User: Find research papers on "quantum photonic entanglement stabilizers".
This guides the agent to not just give up, but to problem-solve by iteratively refining its approach based on the outcome of previous attempts.
Agents can be prompted to use other tools (or even a separate LLM call with a specific "checker" persona) to verify their work or intermediate results. For instance, an agent writing code could be prompted to use a linting tool or a code execution environment to check for errors before submitting the code.
System: You are a Python programming assistant.
1. Write the Python code to solve the user's request.
2. Before presenting the code, you MUST verify it by mentally running it or, if available, using a `python_code_checker` tool.
3. If you detect errors, correct them and re-verify.
4. Present only the verified and corrected code. Explain any corrections you made.
User: Write a Python function that takes a list of numbers and returns the sum of even numbers.
When an agent cannot resolve an error itself, it's important that it reports the failure in a clear, structured way. This allows the broader system (or a human supervisor) to understand what went wrong and potentially intervene.
You can specify the format for error reports in the prompt:
System: ...
If an unrecoverable error occurs while processing the request, provide your response in the following JSON format:
{
"status": "error",
"error_type": "<type_of_error>",
"error_message": "<detailed_message_about_the_error>",
"last_action_attempted": "<description_of_what_you_were_doing>"
}
Do not use this format for successful responses.
User: Process the attached financial report.
This structured reporting aids in logging, debugging, and potentially automating higher-level recovery processes.
The process of an agent performing an action, checking its work, and then correcting if necessary can be visualized as a loop. The prompt engineer's role is to define the conditions and guidelines for each step in this loop.
An agent's operational cycle incorporating prompted self-correction. The agent performs an action, critiques its output based on prompt guidance, and attempts corrective measures if issues are detected before proceeding.
Implementing self-correction capabilities requires careful prompt design:
By thoughtfully incorporating self-correction and error-handling strategies into your prompts, you can build more autonomous, dependable, and intelligent agents. These techniques are fundamental for creating systems that can navigate the complexities of real-world tasks with greater resilience. The ability to manage and recover from errors is a significant step towards more sophisticated agentic behavior, which you'll have the opportunity to implement in practical exercises.
Was this section helpful?
© 2025 ApX Machine Learning