Skip to content

flow: add Loop — agentic loops as a flow step - #3001

Merged
asim merged 1 commit into
masterfrom
claude/loving-meitner-3Etoi
Jun 23, 2026
Merged

asim merged 1 commit into
masterfrom
claude/loving-meitner-3Etoi

Conversation

@asim

@asim asim commented Jun 23, 2026

Copy link
Copy Markdown
Member

What

Adds the agentic loop to flows — run a step over and over, letting the agent keep working until the goal is met, instead of stopping after one pass (the pattern from "the AI world is getting loopy": swarms that improve code / fix builds / refine drafts on a loop). The hard part is cost and runaway risk — a non-deterministic "keep going until done" has no ceiling — so the loop ships with a guardrail.

flow.Loop(body, opts...) is a StepFunc, so it drops into a flow's ordered, checkpointed step list like any other step. It runs the body repeatedly, carrying State across passes, until a stop condition fires or the iteration cap is hit.

micro.FlowStep{Name: "improve", Run: micro.FlowLoop(
    micro.FlowDispatch("coder"),                                  // body: an agent does one pass
    micro.FlowUntilLLM("Is the refactor complete?"),             // stop: model judges it done
    micro.FlowLoopMax(5),                                        // ceiling: never more than 5 passes
)}

API

  • flow.Loop(body StepFunc, opts ...LoopOption) StepFunc
  • flow.Until(cond) — code-defined stop predicate
  • flow.UntilLLM(question) — the model judges the goal met after each pass (the supervised "Ralph" loop); requires a flow model. Either stop condition firing ends the loop.
  • flow.LoopMax(n) — the guardrail: the body never runs more than n times, so the loop always terminates and can't run up an unbounded bill. Hitting the cap returns the latest state rather than erroring.
  • flow.OnIteration(fn) — per-pass progress hook.
  • Exposed at the top level as micro.FlowLoop / FlowUntil / FlowUntilLLM / FlowLoopMax / FlowOnIteration, symmetric with the other Flow* helpers.

Design notes

  • A loop runs as a single flow step — the step engine checkpoints its outcome, and a resume re-enters the step, so loop bodies should be safe to repeat. Per-iteration durable resume is a possible follow-up; OnIteration covers progress persistence for now (documented honestly in the guide).
  • Ties into existing primitives: pair with agent guardrails and x402 paid tools to bound a background loop's spend.

Included

  • flow/loop.go + flow/loop_test.go (unit tests, no network)
  • examples/flow-loop/ — offline runnable (plain-func body + code-defined stop)
  • internal/website/docs/guides/agent-loops.md + nav entry
  • CHANGELOG entry, micro.go aliases

Verification

go build ./..., go test ./flow/..., and golangci-lint run all pass. The example runs offline (loops 3 passes to quality 90, then stops). Jekyll builds the new guide.

🤖 Generated with Claude Code


Generated by Claude Code

Adds the agentic 'loop' to flows: flow.Loop(body, opts...) is a StepFunc
that runs a body step repeatedly, carrying State across passes, until a
stop condition fires or a hard iteration cap is reached.

- Stop modes: flow.Until (code-defined predicate) and flow.UntilLLM (the
  model judges the goal met after each pass — the supervised 'Ralph'
  loop). Either firing stops the loop.
- flow.LoopMax is the guardrail: the body never runs more than n times, so
  the loop always terminates and can't run up an unbounded bill. Hitting
  the cap returns the latest state rather than erroring.
- flow.OnIteration reports per-pass progress.
- Composes as a normal flow step (checkpointed by the step engine).
- Exposed at the top level as micro.FlowLoop / FlowUntil / FlowUntilLLM /
  FlowLoopMax / FlowOnIteration, symmetric with the other Flow* helpers.

Includes tests, an offline runnable example (examples/flow-loop), an
'Agent Loops' guide, and a CHANGELOG entry.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CmdEY7pYmV5zzwCjNJ4ykL
@asim
asim merged commit a503628 into master Jun 23, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants