Repository navigation
flow: add Loop — agentic loops as a flow step - #3001
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 aStepFunc, so it drops into a flow's ordered, checkpointed step list like any other step. It runs the body repeatedly, carryingStateacross passes, until a stop condition fires or the iteration cap is hit.API
flow.Loop(body StepFunc, opts ...LoopOption) StepFuncflow.Until(cond)— code-defined stop predicateflow.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 thanntimes, 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.micro.FlowLoop/FlowUntil/FlowUntilLLM/FlowLoopMax/FlowOnIteration, symmetric with the otherFlow*helpers.Design notes
OnIterationcovers progress persistence for now (documented honestly in the guide).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 entrymicro.goaliasesVerification
go build ./...,go test ./flow/..., andgolangci-lint runall 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