好文集ChatGPT Plus¥119

Loops & Graphs: how to stop being the slowest node in your agent system and approve only one step

文章目录

Earlier this year Bun was ported from Zig to Rust with Claude Code. Reportedly about 750,000 lines of Rust, 11 days from first commit to merge.

The number people quoted afterwards wasn't the line count. It was 99.8%, the share of Bun's existing test suite that still passed.

That number explains the whole project. Thousands of agent runs could be trusted because something was waiting at the end of each one that could say no. And the work was cut into pieces that didn't have to wait for each other.

Those are the two parts that let you stop watching agents work. A loop makes one piece of work correct without you. A graph decides which pieces exist, in what order, and where a person signs off.

Most people build the first and skip the second. Then they sit and approve every step, because nothing else would catch a mistake.

文章配图


The loop is only as good as its check

The usual setup is a second model reading the first model's output. That's a model grading its own homework with a friend. It feels like review and catches very little.

A real check is one a program can fail. Rank what you accept as evidence:

STRONG   tests exit 0
STRONG   output matches the schema
STRONG   diff stays inside the plan
MEDIUM   the cited source actually supports the claim
MEDIUM   an independent path reaches the same answer
WEAK     a reviewer agent approves
NOISE    "looks good to me"
NOISE    the model says it's confident
NOISE    no errors were thrown

The last line is the one that hurts experienced people. An empty error log means nothing was reported, not that nothing went wrong. A loop that treats silence as success will repeat one mistake until the budget is gone, and every log line will look clean.

Write the check before you write the task. Bun had its test suite years before any agent touched it, which is why the port could run without someone reading every diff.

文章配图


Every loop needs a stop condition

"Keep going until it's good" has no end. Agents will happily spend another hour polishing.

Count something instead. For open-ended work like bug hunting or research, track how many new findings each round produces, deduplicated against everything seen so far, and stop after two rounds that find nothing:

seen, dry = set(), 0
for round in range(6):
    fresh = [f for f in discover() if f.key not in seen]
    seen.update(f.key for f in fresh)
    dry = dry + 1 if not fresh else 0
    if dry == 2:
        break

seen must include rejected findings too. If the loop forgets what it already threw away, it will rediscover the same dead ends and you'll pay for them every round.

Five things every cycle needs: a test that says done, a round limit, a budget, memory of past attempts, and a person who gets paged if it stalls.

文章配图

Where a perfect loop stops helping

A loop improves one unit of work. It can't tell you that the unit shouldn't exist, or that three of your five steps could have run at the same time.

So you can have every step passing and still get a slow run that produces the wrong thing. No amount of loop tuning fixes that, because the problem sits between the steps, not inside them.

That's the graph's job.


The graph: cut the arrows that carry nothing

A graph has nodes (one job each, one input, one output) and edges (this node's output is that node's input). Nothing else.

Most agent pipelines are drawn as straight lines because that's how people write instructions: research, then check the repo, then pull pricing, then write. But the pricing step never reads the repo. The order came from the sentence, not from the work.

Test every arrow: name the variable that travels along it. If you can't, delete the arrow and let both steps run in parallel.

In the example below, the same four steps drop from 31 minutes to 17. No extra tokens, just fewer pointless waits. Only wait for everything to finish when the next step actually needs the full set, like deduplicating across sources or ranking all candidates.

文章配图

Give every node a contract, and don't use a model where code will do

If you can't describe a node in four lines, you can't test it or swap it out:

NODE       source_researcher
JOB        find one primary source per claim
IN         { topic, source_type }
OUT        { claim, url, quote }
FAILS AS   no_primary_source

With a fixed contract you can change the model, the prompt or the tool and nothing downstream breaks.

Then ask of each node: does it judge, decide, assess or summarize? If yes, it needs a model. Pick the size by the job, small for extraction and strong for synthesis. If no, it's code. Merging lists, removing duplicates, sorting and joining records have one correct answer. Running them through a model makes them slower, pricier and occasionally wrong.

文章配图

Four shapes cover nearly everything

A chain, when each step really reads the previous one. A fan-out and join, for research, reviews and audits. A router, where a cheap classifier sends simple tasks down a short path and risky ones down a long one. A bounded cycle, with a hard limit.

One rule across all of them: parallel workers need separate contexts. Put four reviewers in one shared window and the first comment shapes the other three. You pay for four reviews and get one, copied three times.

文章配图

The loop lives inside a node, the graph lives between them

Checking and fixing happen inside a single unit. Splitting, merging, sending work back and deciding who approves happen between units.

You need both. Graph without loops gives you lots of unchecked output fast. Loop without graph gives you one excellent step in a badly designed process.


You don't have to draw the graph by hand anymore

This changed a lot this year. Claude Code's dynamic workflows have the model write an orchestration script for each run and fan out to as many as 1,000 subagents, 16 at a time. Codex lets you define agent roles in config files, each with its own model and sandbox. LangGraph 1.0 covers the opposite case: one graph, written once, replayable from checkpoints.

So the question becomes who should draw the graph. Two things decide it: how often the job runs, and how hard a mistake is to undo.

A one-off refactor or audit? Let the model generate the workflow and throw it away afterwards. A one-off migration on production data? Let the model draft the plan, and read only the plan. A weekly low-risk job? Keep a fixed graph in the repo and let it ship. A weekly job that touches money or users? Fixed graph, and you sign the last step.

One trap: letting the model regenerate a weekly workflow from scratch each time. It will split the work differently every run, so nothing it learns carries over.

文章配图


When a node fails, send back the unit, not the batch

Decide in advance what happens on failure:

tool or network blip      → RETRY
model or source down      → FALLBACK
optional branch failed    → SKIP
output failed its check   → REPAIR that unit only
3 repairs failed          → ESCALATE, the plan is wrong
budget or permission hit  → STOP, wake a human

The costly mistake is returning the whole batch. Four slices of a port, one fails its tests, all four go back. Three correct slices get rewritten into different versions, each of which can now fail for new reasons. The run stops converging, and from the outside it looks like the model is getting worse.

Return one unit with the failing test, the evidence and a scope: "this file only." Without the scope line, the agent fixes the bug plus two things it noticed nearby, and a one-line fix turns into a four-file diff.

After a fix passes, write the cause down as a rule for whoever splits the work next time. "Adapters must keep keyword arguments exactly" stops that bug from coming back in every later run. Almost nobody builds this step, which is why most agent setups are fast but never improve.

文章配图


Open the gate based on what a mistake would cost

The common approach is a confidence score with a threshold. The problem is that confidence is the one number the model produces about itself.

Sort changes by how hard they are to undo instead. A copy fix, a new test or an isolated function costs one revert if it's wrong, so let it merge. A shared utility or schema change needs hard evidence first. Migrations, deletions, production writes and payments stay closed regardless of any score.

A threshold is a number someone can lower on a busy Friday. A closed lane has no number to lower.

Reviewer agents like Codex's auto-review help, but they're one more opinion. Check evidence in order: tests, the run's history, how often this node's work has been rolled back, the reviewer agent, and the model's own claim last.

文章配图


Where you end up

Every layer you skip comes back to you as manual work. Without a harness, you click "allow." Without a loop, you read every output. Without a graph, you approve every step.

Build the check first, then the graph, then the rule-writing step. What's left for you is one decision per run, at the point where a mistake can't be undone: approving the merge, choosing which fixes ship, signing off the migration.

Everything before that point runs without you, and it's built to catch its own mistakes.

文章配图

I write about how agent systems actually work in production. Follow if that's useful to you.

全文完
查看原文