Module 2 Lesson 5: Feedback Loops
Self-correcting AI. How agents learn from errors and retry failed attempts during execution.
Feedback Loops: Getting It Right the Second Time
A major difference between a "Script" and an "Agent" is how they handle failure.
- The Script: Tries to run a tool, gets an error (e.g., 404), and crashes.
- The Agent: Tries to run a tool, gets an error, reasons about why it failed, and tries a different approach.
This is a Feedback Loop.
1. Types of Feedback
A. Environment Feedback
This is the result of a tool call.
- "I tried to search for 'NVIDIA Earnings', but the search engine returned zero results. Maybe I should try searching for 'NVDA Investor Relations' instead."
B. Validation Feedback
This comes from a separate "Critic" or "Validator" node (which we will learn in Module 3).
- "The agent generated a report, but the Validator noticed it missing a bibliography. The agent must now go back and add sources."
2. Managing Retries
Infinite loops are the "Silent Killer" of Agentic AI. If an agent keeps trying the same failing tool over and over, you will run up a massive API bill and the task will never finish.
- Max Loops: Always set a hard limit (e.g., 5 retry attempts).
- Error Messages: When a tool fails, don't just say
Error 500. Give the LLM a helpful message:"The API is currently rate limited. Please try a different tool or wait 30 seconds."
3. The "Self-Correction" Prompt
You can explicitly program the agent to check its own work: "Examine your previous answer. Does it fulfill all the user requirements? If not, list the missing pieces and go back to step 1."
4. Visualizing a Corrective Loop
graph TD
Ask[User Query] --> Thought[Plan Step 1]
Thought --> Action[Execute Tool]
Action --> Error[Error: API Timeout]
Error --> Analyze[Reason: Why did it fail?]
Analyze --> Retry[Plan Step 1.2: Use Cached Data]
Retry --> Action
Action --> Success[Result: Success]
Success --> Done[Final Answer]
5. Code Example: Passing Errors back to the LLM
def call_weather_api(city):
if city == "Unknown":
# We don't crash; we provide FEEDBACK to the brain
return "Error: Please specify a valid city name. 'Unknown' is not recognized."
return "25 degrees"
# The LLM receives that 'Error' string as an 'Observation'
# and will likely ask the user for clarification.
Key Takeaways
- Feedback is the information that allows an agent to pivot.
- Self-Correction is the result of an agent reasoning about a failure.
- Helpful error messages are critical; the LLM uses them as "Tips" to improve its next guess.
- Always implement Max Iteration guards to prevent infinite usage loops.