AI Agents and Workflows

6 lessons

Module Progress...
AI Agents and Workflows

Self-Correcting Agent

Build a self-correcting ReAct agent that catches tool errors, feeds them back to the LLM, and retries automatically. Error recovery with LangGraph.

Your agent calls a tool with the wrong argument. The tool throws and your app crashes. This happens all the time in production, the LLM hallucinates a parameter and call tools incorrectly. Most ReAct implementations treat these as fatal errors but we can do a lot better.

We'll catch the exception, pack it into a ToolMessage, and feed it back to the model. The LLM reads the error, adjusts its arguments, retries. A well-formatted error message is just another piece of context the model can reason about.

Most LLMs can't self-correct reasoning on their own1, but they can when given external feedback2, and tool errors are exactly that. We'll rebuild the agent from the previous tutorial using LangGraph and add this error-recovery loop as the main feature.

What You'll Build

  • A stock screening agent built with LangGraph
  • Tool validation that raises errors with helpful messages
  • Exceptions formatted as ToolMessages for the LLM
  • Retry limits to prevent infinite loops

Setup

Footnotes

  1. Large Language Models Cannot Self-Correct Reasoning Yet (Huang et al., ICLR 2024)

  2. When Can LLMs Actually Correct Their Own Mistakes? (Kamoi et al., TACL 2024)

  3. LangGraph Error Handling and Retry Policies