/pattern/loop/

02 · WorkflowControl LoopRetry LoopAgent Loop

Loop.
Repeat until good enough, or budget runs out.

One or more steps are repeated until a specific budget, quality bound, or exit condition is reached.

When to reach for it

  • The result can be iteratively improved.
  • External validation triggers a retry.
  • Tool results necessitate new iterations.

When it backfires

  • There is no stable exit condition.
  • Costs can spiral out of control.
  • Errors amplify through repetition.

The tradeoff

Adaptive improvement is achieved at the risk of endless or highly expensive execution loops.

The mental model

A loop you can draw on a napkin.

A body step produces output; a check step decides whether to exit or loop again.

BodyCheckEnd
Pitfalls

Two ways this pattern will hurt you.

No exit condition — the loop spins forever

The check step always returns 'retry' because the quality threshold is set impossibly high.

Fix · Combine quality gate with a hard iteration cap. Treat cap exhaustion as a normal outcome, not an error.

Errors amplify through repetition

Each loop appends to the same context. A bad intermediate gets baked in and the model compounds it rather than fixing it.

Fix · Keep only the latest iteration in context, or use a scratchpad that the model must explicitly update.

Framework support

Where Loop is native.

LangGraphNative
CrewAICrewAI FlowsNative
Microsoft Agent FrameworkNative

Search

Search patterns, frameworks, and pages.