Skip to content

Agentic Loops

Patterns advanced 12 min
Sources verified Dec 22

Iterative refinement pattern where AI generates output, evaluates it against criteria, and improves through multiple cycles until quality thresholds are met.

Agentic loops are a specific pattern within agentic workflows focused on iterative refinement. The agent generates an initial output, evaluates it against quality criteria, identifies shortcomings, and then regenerates an improved version. This cycle repeats until the output meets standards or a maximum iteration limit is reached.

The core loop has three steps: Generate (produce initial output), Evaluate (score quality, identify issues), and Improve (regenerate with feedback). This is sometimes called 'self-correction' or 'reflexion,' and is particularly effective for tasks where quality can be programmatically measured—like code generation (run tests), data extraction (validate against schema), or creative writing (check for specific criteria).

For example, a code generation loop might: (1) generate Python function, (2) run unit tests and capture failures, (3) regenerate with error messages as context, (4) repeat until tests pass or max iterations reached. Research shows this can improve success rates from 60% to 85%+ for complex coding tasks.

The evaluation step is critical. It can be: Rule-based (regex checks, schema validation), Tool-based (linters, test suites, simulators), or LLM-based (another model scores quality). LLM-as-judge is flexible but adds cost and latency. Many systems combine approaches: fast rule checks first, then LLM scoring for nuanced criteria.

Common failure modes include: Infinite loops (evaluation never satisfied), oscillation (alternating between two flawed versions), degradation (later attempts worse than earlier ones), and runaway costs (too many expensive iterations). Mitigation strategies include: max iteration limits, convergence detection, caching previous attempts to avoid repetition, and exponential backoff for retries.

Use agentic loops when: (1) output quality can be measured, (2) initial attempts are often imperfect, (3) feedback can guide improvement, or (4) cost of extra iterations is acceptable. Avoid when: (1) one-shot prompting works, (2) latency is critical, (3) evaluation criteria are subjective, or (4) you cannot detect convergence.

Key Takeaways

  • Agentic loops iteratively refine outputs through generate-evaluate-improve cycles
  • Evaluation can be rule-based, tool-based (tests/linters), or LLM-based (model-as-judge)
  • Particularly effective when quality is measurable and feedback can guide improvement
  • Common failure modes: infinite loops, oscillation, degradation, runaway costs
  • Mitigation requires max iterations, convergence detection, and caching of previous attempts
  • Use when initial attempts are imperfect and iteration improves quality; avoid when one-shot suffices

In This Platform

This platform uses an agentic loop pattern in content validation: it scans for missing source references, generates a report of issues, and developers iterate on fixes until validation passes. The 'validate' command acts as the evaluator, providing specific feedback that guides the next refinement cycle.

Relevant Files:
  • build.js

Prerequisites

Sources

Tempered AI Forged Through Practice, Not Hype

Keyboard Shortcuts

j
Next page
k
Previous page
h
Section home
/
Search
?
Show shortcuts
m
Toggle sidebar
Esc
Close modal
Shift+R
Reset all progress
? Keyboard shortcuts