Quality-control layer for agent workflows: a generator produces output, a critic checks hard constraints, loop retries until result passes or safety cap is hit.
Stop Shipping AI Hallucinations: The Loop & Critique Pattern · 7 minsTranscript11 entries
Instructor:Your AI agent just shipped a hotel recommendation 90 minutes from the venue, and there's no way to catch the mistake. The loop and critique pattern stops that. Over the next five minutes, you'll see how it works, how to wire it up, and why exit condition design decides whether the iteration helps or just burns tokens. Single-pass agents have a blind spot: they generate once and then they ship. No validation, no feedback, no second chance. When your output has to satisfy hard constraints, such as, like in our case,
Instructor:a hotel within the 30-minute limit and the thousand-bucks budget restriction, maybe one pass is not enough. You need generate, validate, and retry in a loop. Let's understand it using a very simple example. You ask for a trip to Santa Fe, a hotel within 30 minutes of the venue. That time is a specific constraint, and that's exactly why this pattern exists. A single-pass agent will miss it more often than you expect. The generator agent produces the initial itinerary.
Instructor:Here's the thing: the first pass is expected to be imperfect, and that's the whole point. The loop exists because the first draft sometimes doesn't meet all the constraints at all. The critic agent then checks that draft against the specific criteria: hotel proximity, dining quality, and transport feasibility. If anything fails, it sends back specific, actionable feedback. And that's critical here: actionable feedback, not a vague response to try again. The critique becomes the context for the next iteration call,
Instructor:and the generator instruction uses a template marker to inject the feedback only when it exists. That's the pattern in action. And that's the wiring detail that makes the loop actually work. Once the criteria pass, the critic will approve the plan, and it will break out of the agent loop. Without that exit, the loop may run forever, so your safety net is max_iterations. In a lot of systems, you'll also put a max token cap, but it's a sensible default to have either of them, or both of them.
Instructor:Before we get into trade-offs, let's run it using a real-world example. So I'm going into VS Code now. In the background, my server is running here, which has all the agents as I anticipated. And what we've got there is a client we can call. So I call into A2A Validate to run the client, and it actually works. You know, the first response itself was correct. Now let's try our luck and see if we get it on the first response again. This time it might take two or three tries.
Instructor:For this example, we are running local Ollama with the variance level at 0.25. So the quality of the response is not really the assessment here. We want to validate the pattern itself. So here we can see the first iteration is going, and if we're lucky, this time it passes on the first iteration. And we run again and see. It may take two or three iterations randomly because I have added random failures. So let's see if we are lucky enough to get one failure now.
Instructor:We've been lucky again, or unlucky, because it again passed on the first iteration. So I'm just going to do one more run and see if it works. It's surprising that we are wishing it to fail so we can demonstrate here. If it doesn't, then let's just move on. Well, here it fails, so you can see the critic response here. This is just a brief view of the critic response, but internally it provides the retry response and the critique-requested improvement in the loop. And now we've got a second iteration running on it.
Instructor:Let's see if the second iteration passes and we get the feedback that fixes everything. So this is how it works. Let's go back to the trade-off. So here we are on the trade-off. Each iteration costs one generator call plus one critic call. Two iterations double the tokens. Three roughly triple them. The quality gain is real with this. For some very important business processes, this is a lifesaver pattern. Most improvements happen in the first iteration.
Instructor:Some complex environments may take more iterations as needed. After that, you will hit diminishing returns. So it's very important to find the point where those diminishing returns start, and that's likely your best candidate for the maximum iterations. So make sure you collect quality metrics to justify cost as well, because this can become one of the higher-cost patterns, certainly. Loop and critique gives your agent pipeline the ability to self-correct.
Instructor:Your exit condition decides whether you get the quality improvement or just cost. So design it carefully, cap your iterations, and measure the gain. We are good with now six patterns that we already covered in the last six videos. In the next videos, we will try to implement some real examples and see how we can actually put these methods together, either one at a time or in combinations, and see what can be done with them. Most likely, in my experience, by understanding these six patterns and mastering them,
Instructor:in most enterprise cases, you won't need many more advanced patterns. However, there are some other advanced patterns that can still be useful. So I'll see you in the next video. Make sure you subscribe and like the channel so the next videos land in your feed. inbox or a notification list. Thank you, and I'll see you again in the next video.
Learning Objectives4
Explain the loop & critique pattern and identify when iterative refinement is necessary
Implement a generator-critic feedback loop using Google ADK's LoopAgent
Design robust exit conditions using escalation and max_iterations
Evaluate the cost-quality trade-off of iterative agent refinement
Q&A
Q & A
Q
How does the loop know when to stop?
Two mechanisms: (1) the critic agent calls exit_loop which sets escalate=True, signaling the LoopAgent to terminate, or (2) the LoopAgent hits its max_iterations ceiling. The first is quality-driven exit; the second is a safety bound to prevent runaway cost.
Q
Does the generator see feedback from previous iterations?
Yes. The critic writes feedback to session state via output_key. The generator reads that feedback through template variables like {critique_feedback?} in its instruction. The ? suffix avoids errors on the first iteration when no feedback exists yet.
Q
Is loop & critique the same as retry logic?
No. Retry logic repeats the same call hoping for a different result. Loop & critique provides directed feedback — each iteration receives specific information about what failed and what to fix. It is a directed search, not random retry.