Building a Critic-Agent Loop: Scores, Refinement, and Guardrails
Summary
The article details building a guarded critic-agent loop for LLMs, where a worker LLM's output is reviewed and refined by a separate critic LLM. This system addresses common failure modes like rubber-stamping, infinite loops, silent failures, and high costs. Key guardrails include a `MAX_ITERS = 3` limit to prevent endless iterations, an explicit escalation to a human for unverified answers, and a `PASS_THRESHOLD = 8.0` for critic scores, requiring two independent signals for a "pass". The system also incorporates a confidence gate to skip critic review for high-confidence, low-stakes tasks and emphasizes maintaining critic independence through different models or adversarial prompting. Python code examples illustrate the critic prompt, `Critique` dataclass, and `solve_with_critic` function, alongside discussions on tools like Ragas and LangGraph, and security considerations such as collusion and prompt injection.
Key takeaway
For AI Engineers building LLM applications requiring high accuracy and reliability, implementing a guarded critic-agent loop is crucial. This pattern ensures outputs are verified, prevents infinite refinement cycles with a `MAX_ITERS` cap, and explicitly escalates unverified results to humans. You should prioritize critic independence and gate its use to manage costs, thereby improving trust and reducing false positives in production systems.
Key insights
A guarded critic-agent loop uses a separate LLM to review, score, and refine worker outputs, ensuring quality, termination, and honest escalation.
Principles
- Critic prompts must judge, not redo the task.
- Cap refinement iterations to prevent infinite loops.
- Ensure critic independence to avoid shared blind spots.
Method
Implement a `solve_with_critic` function: worker produces, critic reviews (JSON output), if failed, worker revises with specific issues, capped at `MAX_ITERS = 3`. Escalate if unverified.
In practice
- Use `dataclass` for structured critic output.
- Gate critic calls based on worker confidence and task stakes.
- Log every draft, score, and issue for audit trails.
Topics
- Critic Agents
- LLM Guardrails
- Agent Orchestration
- Prompt Engineering
- AI Security
- LLM Evaluation
Code references
Best for: AI Engineer, Machine Learning Engineer, MLOps Engineer
Related on AIssential
Editorial summary, takeaway, and curation by AIssential. Original article published by Towards AI - Medium.