# Loop Engineering from first principles

Kyle Mistele

AI Engineer World's Fair 2026

[Talk page](https://ai.engineer/talks/xIt_mTQp6mY-loop-engineering-from-first-principles) · [Video](https://www.youtube.com/watch?v=xIt_mTQp6mY)

## Loop Engineering from First Principles

Build coding-agent loops around measured errors, small changes and human review, using an incremental RPC-to-Effect migration to connect control theory with practical CI workflows.

Familiarity with pull requests, CI workflows and coding agents is helpful; no prior knowledge of control theory or Effect is required.

### What happens when nobody can review the PR?

[0:27](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=27s)

A coding agent can keep working, verifiers can keep checking, and review agents can keep reviewing—yet the result can still be a pull request nobody wants to read. Kyle Mistele opens with an illustrative failure: six review agents surrounding a 40,000-line PR. More automated scrutiny does not, by itself, make that change understandable to the team responsible for shipping it.

[Geoff Huntley’s Ralph](https://ghuntley.com/ralph/) is a useful tool for some workloads. Mistele sees its appeal especially in solo work and noncritical systems. The harder problem is using loops in a team maintaining software with customers, regulatory obligations and service-level agreements. Those constraints make the size and comprehensibility of each change part of correctness.

![Slide with a cartoon character at an oven and a diagram connecting Prompt to Agent to done, with a return arrow labeled while (true).](https://ai.engineer/api/talk-essay-media/6d5e9d21b92678762a22a7947b753ab51a818fdae452ea99ee20a96c48a22b4e)

Ralph Wiggum as a “software engineer,” alongside a repeating prompt-to-agent loop.

### Loops should make code easier to read

[1:55](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=115s)

The enthusiasm for loops began with a simple shift: instead of repeatedly prompting a coding agent yourself, design a loop that prompts it. Mistele traces the discussion from Ralph’s original post to Peter Steinberger’s advocacy of loop design. In his description of OpenClaw, loops build, review, merge and release code; other loops find bugs, including bugs in the loops themselves. He also cites Boris Cherny describing his engineering work as writing loops to prompt **Claude**.

Push that logic further and the volume of generated code becomes a reason to stop reading it. Verification and automated review take over, and code becomes effectively write-only. Mistele treats the accompanying claims of tenfold speed as an unsettled promise, not an established result.

His counterexamples concern both reliability and cost. Mistele says Claude Code’s terminal flicker took six months to fix, while OpenCode built a renderer in a fraction of that time; this is his historical anecdote, not a controlled development-speed comparison. The [terminal-flickering issue](https://github.com/anthropics/claude-code/issues/1913) documents the reported problem. He also points to OpenClaw stability problems and the expense of running these systems without a frontier lab’s token budget. Drawing on Matt Pocock’s argument, he adds that bad code becomes especially expensive when agents keep building on it.

**Design loops that improve the code while humans continue reading it.** A loop can remove bad patterns, make changes easier to review and solve difficult problems in a complex repository. The software factory can emerge incrementally, with engineering discipline applied to the loop itself.

### Measure, correct and measure again

[4:33](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=273s)

Control theory asks how to drive a changing system toward a desired state. For a coding loop, the system is the codebase. A **sensor** measures its current state, and a **set point** specifies the desired state. The difference is the measured error. A **controller** converts that error into an incremental correction, and an **actuator** applies the correction while other changes continue to disturb the system. Measuring again closes the loop.

![Control Theory diagram showing a set point entering an error junction, followed by Controller, Actuator and System, with a disturbance input and a Sensor feeding measured output back.](https://ai.engineer/api/talk-essay-media/f99de444d8f8d7ebef58f2227858b2d19d4382b45d54123fec6dbcde3e1a4002)

A control loop connects measured error, controller, actuator, system and sensor.

The same basic structure appears in a thermostat: measure temperature, compare it with the setting, act, then measure again. Software engineers already encounter related mechanisms in Kubernetes autoscaling and infrastructure-as-code reconciliation. Mistele also describes PostgreSQL autovacuum and React’s virtual DOM as using or approximating control loops. The common requirements are a system you can change, a problem you can measure and feedback about the result.

Incremental corrections reduce the chance of oversteering. Instead of attempting the entire desired end state in one change, the controller makes a bounded move and checks its effect. That is the useful distinction between a feedback loop and a blind loop. It is not a categorical rejection of Ralph: Mistele explicitly recognizes that the best Ralph implementations already apply control principles and that Ralph is a teaching device, not an instruction to abandon judgment.

### Separate the responsibilities, even when one agent performs them

[6:59](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=419s)

Start an agentic control loop by defining a property the codebase should satisfy, then choose how to detect deviations. Simply repeating a Bash command does not guarantee incremental progress; the sensing and selection policy must supply that discipline.

| Sensor approach | Suitable ingredients |
| --- | --- |
| Deterministic | ESLint rules, [ast-grep](https://ast-grep.github.io/reference/yaml), Packwerk |
| Agentic | An agent, a skill and natural-language rules |
| Hybrid | A pipeline combining deterministic checks and agent judgment |

These are alternatives for measuring the discrepancy, not requirements to make every part of the loop agentic.

The implementation boundaries can overlap. Mistele uses Aiden Bai’s [React Doctor](https://github.com/millionco/react-doctor) as an example of a hybrid sensor and controller: it identifies React problems, prioritizes fixes and explains how to make them. Likewise, one agent can select a change and implement it in the same context window. Combining components does not remove the controller’s responsibility to limit the size of a change and choose the right direction. Repeated oversized or incorrect corrections can quickly destabilize the codebase.

### Choose a measurable maintenance workload

[8:29](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=509s)

Pattern cleanup is only one application. A loop could check an API against someone else’s OpenAPI specification, maintain compliance with the particular MCP specification version a server targets, mirror a project between Python and TypeScript, or keep a Vite-based Next.js fork aligned with upstream. Each candidate needs the same three properties: a measurable discrepancy, incremental corrections and feedback on their quality.

HumanLayer’s example is an incremental migration of its RPC API to [Effect](https://www.effect.website/docs/v3/getting-started/introduction). The team first adopted Effect for race-prone code, liked the result and began extending it across the codebase. In the recording, Mistele places a small procedure beside its Effect rewrite to establish the transformation the loop will perform. The syntax is incidental to the loop design; this is a migration chosen for the team’s needs, not a recommendation that every codebase adopt Effect.

### Find old procedures—and stop adding new ones

[9:40](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=580s)

The first implementation task is a sensor that finds unmigrated RPC procedures. An agent or a text search could do this, but Mistele chooses **ast-grep** for structural matching. Its scanning rules can live outside the ordinary TypeScript and ESLint configuration, avoiding reliance on checks an agent might suppress with inline comments. Additional rules can target other unwanted patterns, with granular include and exclude paths across supported languages in a monorepo.

The raw results need normalization before they become a useful control input. In Mistele’s workflow, ast-grep produced roughly 50 keys per violation; the team retained four fields and sorted the results deterministically. That count describes this workflow, not a fixed output contract. The practical purpose of processing [ast-grep’s JSON output](https://ast-grep.github.io/guide/tools/json) is to turn a verbose scan into a stable list that can be compared between revisions.

Before migrating the backlog, prevent it from growing:

1. Run a full scan on `main`.
2. Sort the violations deterministically and commit the baseline to version control.
3. Scan each pull request and check whether it introduces any new unmigrated procedures.
4. Require new procedures to use Effect while existing procedures are migrated incrementally.

This lets the repository carry known migration debt without treating new debt as acceptable.

Other developers are still changing the system while the loop works. Without the regression check, their changes could undo its progress. Mistele loosely calls the baseline check a disturbance dampener: it is a practical safeguard around the control loop rather than a perfect one-to-one mapping to a control-theory component.

### Choose a small correction with a useful purpose

[11:21](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=681s)

A minimal controller can use Bash and `jq` to select the first violation from the sorted list. A more selective controller can use structural analysis to choose the smallest unmigrated procedure, limiting the scope of the next change. An agent could make the selection, or select and implement together, but Mistele recommends keeping deterministic decisions in deterministic code.

The migration also has an operational purpose: better error handling and instrumentation. A more ambitious controller could use telemetry to prioritize procedures with frequent errors, weak instrumentation or gaps in application performance monitoring. The control signal would then contain both the selected procedure and the evidence explaining what should improve. That gives the actuator a reason to improve the implementation rather than mechanically translate its syntax. The accompanying diagram makes the handoff concrete: Sentry traces and errors inform selection, and sample trace data travels with the chosen procedure to the agent.

![Data-Driven Controller diagram with AST-Grep Rules, steps to fetch Sentry traces and errors by procedure and pick the most errors or worst performance, and an Actuator (Agent) receiving the selected procedure and sample trace data.](https://ai.engineer/api/talk-essay-media/d841915fcfef1c8f2a3f28b9753a02a8e842c80641fe6395cac4353266267163)

A data-driven controller selects a procedure using Sentry traces and errors, then passes trace data to the actuator.

### Give the actuator local patterns to follow

[12:26](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=746s)

The actuator is a CLI coding agent plus a skill. The skill deserves substantial attention, but it should evolve through observed results rather than being treated as a specification that must be perfected before the first run. HumanLayer writes **golden patterns** by hand: idiomatic examples that show the agent how this repository expects a change to look. Without those examples, the agent falls back on documentation and patterns learned elsewhere.

Pass the skill and the controller’s selected target into the actuator together. Include a response template in the skill so the agent’s final message can serve as the pull-request description. Once the agent finishes, deterministic workflow steps commit the change, push the branch and create the PR. The agent handles implementation; ordinary automation handles publication of its proposed change.

### Use existing CI, then fix the correction friction

[13:22](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=802s)

Existing CI is a practical place to run the loop. GitHub Actions, GitLab or CircleCI already provides repository access, secrets, dispatch and scheduling. A workflow can perform one sense–control–actuate iteration, create a PR and run on a daily schedule. There is no need for a separate cluster merely to orchestrate this sequence. The intended result is a small change waiting for review each morning.

That initial design was frustrating enough that HumanLayer turned it off. Correcting an output meant checking out the branch, changing the skill, changing the code, committing and pushing again. The migration itself might be small, but steering the automation demanded too much manual work. A useful loop therefore needs a low-friction way for a human to correct both the current proposal and the instructions that will shape future proposals.

### Turn review comments into persistent steering

[14:13](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=853s)

Add a Markdown feedback file to version control and load it deterministically into the actuator’s context on every run, after the controller has selected the work. Then label every generated PR with an identifier for its loop. The label matters when several workflows operate in the same repository: each must recognize its own PRs and respond only to feedback intended for it.

The correction path starts with a reviewer leaving an `/iterate` comment:

1. The corresponding loop workflow receives the comment trigger.
2. It loads the PR diff, comments, review comments and description into the agent’s context alongside the skill.
3. It instructs the agent to fix the proposed code and update the feedback file.

The review now produces both a repair to the current PR and reusable guidance for subsequent actuator runs.

Keeping that guidance in version control makes changes to the loop’s behavior inspectable. The team can see how its instructions evolved and revert them when necessary. Human steering becomes part of the repository’s reviewable history rather than a series of manual skill edits that are difficult to reconstruct.

### Stop producing changes when review is blocked

[15:10](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=910s)

A scheduled loop can outpace its reviewers even when each individual change is small. HumanLayer encountered this during travel, customer visits and other work: PRs accumulated, duplicated effort and conflicted. Useful maintenance became another backlog demanding attention.

The existing PR label supplies a simple **backpressure** mechanism. Before checking out code, installing dependencies or invoking an agent, query for open PRs carrying the loop’s label. If one exists, stop the new run. The gate permits at most one open PR per loop.

An outstanding PR is a conservative signal that the previous output has not cleared the human-review boundary. Producing another change does not resolve that bottleneck. Waiting prevents a stack of new proposals from building up behind it, reduces duplicate work and, as Mistele qualifies it, hopefully avoids conflicts.

### Increase throughput as confidence and review capacity grow

[16:17](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=977s)

Once the loop produces changes the team trusts, the next question is how to accelerate it. Mistele cites a backlog of 150 RPC procedures and estimates roughly six months at the preceding pace of one procedure per daily run. That is a planning estimate, not a measured completion rate; review delays also affect how quickly the backlog clears.

He proposes two ways to increase the work selected in an iteration:

- **Larger batches:** Have the controller select three or five procedures instead of one.
- **Separate implementation contexts:** Select a batch, then migrate each procedure in its own implementation phase and context window. Mistele expects this separation to be cheaper and more reliable; he presents no measured comparison.

The distinction is between how much work the controller chooses and how much context each implementation receives.

Another proposed extension distributes review ownership: run the workflow four times and give one PR to each of four teammates. This would require adjusting the earlier single-open-PR policy; the talk does not specify that concurrency design. The underlying constraint remains human capacity to read the resulting changes.

The resulting operating model improves the code incrementally, slows down when review cannot keep up and accepts corrections through the same PR interface the team already uses. Mistele closes by inviting viewers to try the offered skill and share what they build. The useful endpoint is a loop whose output remains readable and whose behavior the team can steer as it learns.

![Closing slide with a skill installation command above five bullets: Build control loops; Read the code; Make the code better; Manage risk with incremental change; Use adaptive flow control & steering.](https://ai.engineer/api/talk-essay-media/06543c94f57963b421946db4dd3306ecab4a4dc285aff499913fda6deb8ad4ab)

Closing recommendations pair control loops and code review with incremental change, adaptive flow control and steering.

## Resources

- [Ralph Wiggum as a software engineer](https://ghuntley.com/ralph/) — Geoffrey Huntley explains Ralph, including prompt tuning and limiting each loop to one task.
- [Effect v3 introduction](https://www.effect.website/docs/v3/getting-started/introduction) — Introduction to Effect’s concurrency, error handling, resource management and observability capabilities.
- [ast-grep JSON output](https://ast-grep.github.io/guide/tools/json) — Match-object structure and JSON output modes for processing structural scan results with other tools.
- [ast-grep rule configuration](https://ast-grep.github.io/reference/yaml) — Reference for defining structural rules, parsing languages and matching constraints.
- [HumanLayer skills](https://github.com/humanlayer/skills) — HumanLayer’s public repository of coding-agent skills.
- [React Doctor](https://github.com/millionco/react-doctor) — A tool for detecting problems in agent-written React code.

## Slides

### We’re all building loops wrong

Kyle Mistele calls loops powerful, but says the hype around repeatedly prompting coding agents is often unhelpful.

[0:39](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=39s) [Slide stating that we are all building loops wrong, with “wrong” in bold.](https://assets.ai.engineer/sites/aiecode2025/sha256/5b146d45296bdec0299389c4a38f8d86d9b06e00662725e71b7b03d11b3721af.webp)

We’re all building loops wrong

Loop Engineering from First Principles | Kyle Mistele | @0xblacklight
AI Engineer
World’s Fair

Interface text:
Chrome File Edit View History Bookmarks Profiles Tab Window Help
Tue Jun 30 3:47 PM

### Ralph Wiggum as a software engineer

The diagram loops a prompt through an agent repeatedly. Kyle Mistele calls Ralph useful for certain problems, but warns that this approach can produce 40,000-line PRs that nobody wants to read.

[0:46](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=46s) [An illustration of Ralph Wiggum beside a diagram looping from a prompt through an agent and back.](https://assets.ai.engineer/sites/aiecode2025/sha256/8c21a557340f7c72929cb31fa39599f4ce43284c2d7c47ebf66e520a37b62a73.webp)

BY GEOFFREY HUNTLEY IN AI — 14 JUL 2025

Ralph Wiggum as a "software engineer"

Prompt → Agent → done
while (true)

How Ralph Wiggum went from 'The Simpsons' to the biggest name in AI right now -
Venture Beat

Loop Engineering from First Principles | Kyle Mistele | @0xblacklight
AI Engineer
World’s Fair

Interface text:
Chrome  File  Edit  View  History  Bookmarks  Profiles  Tab  Window  Help
Tue Jun 30 3:48 PM

### Loops for the real world

Kyle Mistele sets out to build loops for large, complex codebases serving real customers, with regulatory obligations and service-level agreements.

[1:32](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=92s) [Slide proposing a discussion of how to build loops for the real world.](https://assets.ai.engineer/sites/aiecode2025/sha256/078d89fbaba656aa2e58e9d0395fef137ac4e508f3136f140bede697595b712b.webp)

Let’s talk about how to build loops
For the real world

Loop Engineering from First Principles | Kyle Mistele | @0xblacklight
AI Engineer
World’s Fair

Interface text:
Chrome  File  Edit  View  History  Bookmarks  Profiles  Tab  Window  Help
Tue Jun 30 3:48 PM

### Designing loops to prompt agents

Peter Steinberger calls for designing loops that prompt coding agents. Mistele uses the post to introduce loops for real-world codebases.

[2:02](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=122s) [Peter Steinberger’s post recommends designing loops that prompt coding agents.](https://assets.ai.engineer/sites/aiecode2025/sha256/a6e99d16d87f2dcdd936c3305a6d86dd9828654a06e62c4212e1fa778267e163.webp)

Peter Steinberger
@steipete

Here’s your monthly reminder that you shouldn’t be prompting coding agents anymore.

You should be designing loops that prompt your agents.

11:58 AM · Jun 7, 2026 · 8.3M Views

Loop Engineering from First Principles | Kyle Mistele | @0xblacklight
AI Engineer
World’s Fair

Interface text:
Chrome  File  Edit  View  History  Bookmarks  Profiles  Tab  Window  Help
Tue Jun 30 3:48 PM

### Automated review and repair loops

Codex reviews each landed commit and opens a fix PR if an issue is still relevant. Review and repair agents then cycle through issues up to five times.

[2:13](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=133s) [Peter Steinberger describes agents reviewing commits and fixing issues, alongside an example pull request.](https://assets.ai.engineer/sites/aiecode2025/sha256/b805722e08cee342eb3c88197a7a2a3b610a3f922b2d60d955d8380f36842638.webp)

Peter Steinberger 🦞 @steipete · Apr 28

codex now runs on each commit we land, reviews it - and if a booboo is found, a new codex spins up and (if still relevant) makes a PR for the fix.

Then a review agent spins up. If an issue is found, another agent will fix the issues. (up to 5 loops)

openclaw/openclaw
#74065 fix: Found one low-severity block-reply edge case in the new...
4 comments · 0 reviews · 5 files · +108 −8
openclaw-clownfish · April 29, 2026 · 1 commit
fix: Found one low-severity block-reply edge case in the new split-tag by open...
From github.com

Loop Engineering from First Principles | Kyle Mistele | @0xblacklight
AI Engineer World's Fair

Interface text:
Chrome · File · Edit · View · History · Bookmarks · Profiles · Tab · Window · Help
Tue Jun 30 3:49 PM

### Boris Cherny on writing loops

Boris Cherny says he writes loops that prompt Claude and do the work, describing loop-writing as his job.

[2:31](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=151s) [Boris Cherny holds a microphone beside a quote about writing loops that prompt Claude.](https://assets.ai.engineer/sites/aiecode2025/sha256/19fec8c71d1dab07336d52ab0244565d0d2cc16875576c5fa5e96f96488ac10b.webp)

“I don't prompt Claude anymore. I write loops — and the loops do the work. My job is to write loops.”

Boris Cherny, Creator of Claude Code

Name badge: Boris Cherny

Interface text: Chrome  File  Edit  View  History  Bookmarks  Profiles  Tab  Window  Help  Tue Jun 30 3:49 PM

### Fleets that design loops

Peter Steinberger predicts that in 3 months we’ll be talking about fleets that design loops. Mistele imagines swarms of agents designing loops to prompt other agents.

[2:39](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=159s) [A post by Peter Steinberger predicts fleets that design loops.](https://assets.ai.engineer/sites/aiecode2025/sha256/1ae008a6053e1aa74fd8ed9570e405f3bde06ce224a4771dfdd2f823d0225e16.webp)

Peter Steinberger 🦞 @steipete · Jun 7
Replying to @gauthampai

Don’t worry it’ll take 3 months until it’s there.

We’ll be talking about fleets that design your loops then.

Replies: 51
Reposts: 30
Likes: 1.2K
Views: 252K

Loop Engineering from First Principles | Kyle Mistele | @0xblacklight
AI Engineer
World’s Fair

Interface text:
Chrome  File  Edit  View  History  Bookmarks  Profiles  Tab  Window  Help
Tue Jun 30 3:49 PM

### Write-Only Code Summit

Mistele uses DevGuild’s summit to illustrate write-only code: generated software can outpace human attention while customers still expect quality, performance, and trust.

[2:55](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=175s) [DevGuild Write-Only Code Summit page discussing AI-generated code and software quality.](https://assets.ai.engineer/sites/aiecode2025/sha256/fd9040b1c602c8e183195cb0998a3e386ee4253fb9d71b03584b6494617774e8.webp)

MAY 7, 2026

DevGuild

WRITE-ONLY CODE SUMMIT

WRITE-ONLY CODE SUMMIT

AI code generation, multi-agent workflows, and shifting release practices have ushered in an era of write-only code, one where software outpaces human attention. But for those with real customers, velocity cuts both ways. Enterprises still expect a high bar for quality, performance, and trust. As engineering decision-makers, our work has never been more valuable.

DevGuild gathers technical founders and infrastructure leaders redefining the software development lifecycle. If you're building the culture, processes, and tools for a write-only future, join us.

Interface text:
Chrome  File  Edit  View  History  Bookmarks  Profiles  Tab  Window  Help
Tue Jun 30 3:49 PM

### Build loops

Mistele describes a vision of building loops, spending tokens and leaving code unread. He questions how well it works and points to the cost of tokens.

[3:13](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=193s) [Slide reads: Build loops; spend as many tokens as possible; don’t read the code.](https://assets.ai.engineer/sites/aiecode2025/sha256/76210c91fc65001a585e5cc610daef55943d6f119fda72d0e24941fbaf9b6908.webp)

Build loops

Spend as many tokens as possible

Don’t read the code

Loop Engineering from First Principles | Kyle Mistele | @0xblacklight

AI Engineer
World’s Fair

Interface text:
Chrome  File  Edit  View  History  Bookmarks  Profiles  Tab  Window  Help
Tue Jun 30 3:49 PM

### OpenClaw Install Confidence

On a 0–10 scale, OpenClaw’s install confidence falls to 0.5, then reaches 10.0 before ending at 9.1. Mistele points to stability issues.

[3:35](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=215s) [Line chart showing OpenClaw install confidence scores falling and then rising to near 10.](https://assets.ai.engineer/sites/aiecode2025/sha256/3ebf5e52e42134b1ae6d68599f6ce4bc3668c61f0627ed4b83488612e85289b2.webp)

openclaw
Source: openclaw/openclaw · Install Confidence 0–10

GLOBAL SCORE HISTORY ?
Y-axis ticks: 0, 5, 10
Score labels, left to right: 5.6, 5.5, 7.6, 2.3, 3.4, 1.5, 0.5, 4.6, 4.4, 4.4, 3.4, 4.2, 6.4, 8.8, 7.8, 8.3, 10.0, 10.0, 9.8, 9.1

Loop Engineering from First Principles | Kyle Mistele | @0xblacklight
AI Engineer
World’s Fair

Interface text: Chrome File Edit View History Bookmarks Profiles Tab Window Help; Tue Jun 30 3:50 PM

### Monthly Token Spend

The slide puts Peter Steinberger’s monthly token spend at $1,305,088.81 as Mistele warns that token costs are high without a frontier lab’s unlimited budget.

[3:43](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=223s) [A large dollar figure labeled as monthly token spend.](https://assets.ai.engineer/sites/aiecode2025/sha256/748087200e53e8cf3f020be37aa193453b15eeb9414f39f301db0b424058ea72.webp)

$1,305,088.81
Peter Steinberger’s Monthly Token Spend

Loop Engineering from First Principles | Kyle Mistele | @0xblacklight
AI Engineer
World’s Fair

Interface text:
Chrome  File  Edit  View  History  Bookmarks  Profiles  Tab  Window  Help
Tue Jun 30 3:50 PM

### The Cost of Bad Code

Mistele cites Matt Pocock’s argument that bad code is more expensive in the age of agents than ever before.

[3:48](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=228s) [A conference speaker appears beside a slide stating that bad code is the most expensive it has ever been.](https://assets.ai.engineer/sites/aiecode2025/sha256/be761f1bb9b1372625cf526c317b6258b6abe03c6d821ff2c9d0302a55a6ced0.webp)

Bad code is the most expensive
it’s ever been

[illegible]ATTER MORE THAN EVER
IT AIN'T BROKE: W[illegible]

AI Engineer EUROPE
PRESENTED BY
Google DeepMind
Braintrust
WorkOS
OpenAI

Interface text:
Chrome  File  Edit  View  History  Bookmarks  Profiles  Tab  Window  Help
Tue Jun 30 3:50 PM

### What Actually Works?

Kyle Mistele turns to what he thinks works in the real world, drawing on practices at HumanLayer that still include building loops.

[4:03](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=243s) [A dark slide asks, “What Actually Works?”](https://assets.ai.engineer/sites/aiecode2025/sha256/a81bbc9ec7a34f7a12248b91442cc27760b639c1e65f092facb1bd805558055d.webp)

What Actually Works?

Loop Engineering from First Principles | Kyle Mistele | @0xblacklight

AI Engineer
World’s Fair

Interface text:
Chrome  File  Edit  View  History  Bookmarks  Profiles  Tab  Window  Help
Tue Jun 30 3:50 PM

### Practices for Building Loops

Mistele argues that loops can improve code quality and make code easier to read while preserving human ownership and tackling hard problems in complex codebases.

[4:07](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=247s) [A list calls for building loops, reading code, preserving human ownership, improving code quality, and solving hard problems in complex codebases.](https://assets.ai.engineer/sites/aiecode2025/sha256/4bc30deeff47a2f11449762f9a47d12cc96fa443c689ec14f1b2f121ea9116e2.webp)

Build Loops
Read the Code
Preserve Human Ownership
Maintain + Improve Code Quality
Solve Hard Problems in Complex Codebases

Loop Engineering from First Principles | Kyle Mistele | @0xblacklight
AI Engineer
World’s Fair

Interface text:
Chrome  File  Edit  View  History  Bookmarks  Profiles  Tab  Window  Help
Tue Jun 30 3:51 PM

### Control Theory

A sensor measures the system’s output, which is compared with the set point to produce an error. The controller directs an actuator to change the system, and the loop measures again as disturbances occur.

[4:32](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=272s) [A feedback loop connects a set point, controller, actuator, system and sensor, with a disturbance entering the system.](https://assets.ai.engineer/sites/aiecode2025/sha256/16bb4abc2995773d8628cdfd8eac518aed138d798016c5359682a96fa9539237.webp)

Control Theory

Set Point: w(t)
Measured Error: e(t)
Controller
Controller Output: u(t)
Actuator
System Input: uₛ(t)
Disturbance: d(t)
System
System Output: y(t)
Sensor
Measured Output: yₘ(t)

+ −

Loop Engineering from First Principles | Kyle Mistele | @0xblacklight
AI Engineer
World’s Fair

Interface text: Chrome File Edit View History Bookmarks Profiles Tab Window Help; Tue Jun 30 3:51 PM

### Thermostat Example

A thermostat illustrates a control loop: its indoor reading and set point are both 72, with cooling mode selected.

[5:26](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=326s) [A thermostat displays an indoor temperature and set point of 72.](https://assets.ai.engineer/sites/aiecode2025/sha256/79f8884f2b7d3d53279c5404374610ad6ae7f81a6928012a793c11d06b9b78c9.webp)

MODE
Cool

12:30 PM
Indoor
72

Set To
72
Following Schedule

MENU

FAN
Auto

[illegible]
Honeywell Home

Loop Engineering from First Principles | Kyle Mistele | @0xblacklight
AI Engineer
World’s Fair

Interface text:
Chrome  File  Edit  View  History  Bookmarks  Profiles  Tab  Window  Help
Tue Jun 30 3:52 PM

### Control Loop Examples

Kubernetes autoscaling uses control loops, while infrastructure as code iterates toward a desired state. PostgreSQL autovacuum and React’s virtual DOM use or approximate the same pattern.

[5:44](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=344s) [The slide lists Kubernetes, Terraform, React and Postgres.](https://assets.ai.engineer/sites/aiecode2025/sha256/90cf7ef0e3cc746a1c1706f4b3092f99762bf63c3c82356be9e2afa5af1c911b.webp)

Kubernetes
Terraform
React
Postgres

Loop Engineering from First Principles | Kyle Mistele | @0xblacklight
AI Engineer
World’s Fair

Interface text:
Chrome  File  Edit  View  History  Bookmarks  Profiles  Tab  Window  Help

### Why Use Control Loops?

Control loops use feedback to steer incremental changes, reducing the risk of oversteering and destabilizing a system.

[6:02](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=362s) [Control loops drive incremental change amid disturbances, maintain stability, reduce risk and support feedback-driven steering.](https://assets.ai.engineer/sites/aiecode2025/sha256/7d2c06b7621cb8d6100be841087d25f7efe5da643f518d35602aa02191b67590.webp)

Why Use Control Loops?

• Drive incremental change amid disturbances
• Maintain system stability
• Incremental and low-risk
• Feedback-driven and steerable

Loop Engineering from First Principles | Kyle Mistele | @0xblacklight
AI Engineer
World’s Fair

Interface text:
Chrome  File  Edit  View  History  Bookmarks  Profiles  Tab  Window  Help
Tue Jun 30 3:52 PM

### Large Test Runner Migration

Kyle Mistele points to a 498-file Vitest migration with 17,073 additions and 19,195 deletions as the kind of PR control loops can help avoid.

[6:27](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=387s) [A merged test runner migration pull request highlights 17,073 additions and 19,195 deletions.](https://assets.ai.engineer/sites/aiecode2025/sha256/193a66a56fe14fd3d98e2ef35ca09098df8d7c6c1fe8bfcb70072f365c8cda04.webp)

Migrate test runner to Vitest #1530

+17,073 −19,195

[illegible] accidentally running on Bun because --bun in the script chain shims node to Bun for child processes. That made CI less predictable, hid Node-runtime incompatibilities until late, and kept packages carrying one-off runner conventions.

This PR moves the repo to a single Vitest-on-Node test runner while preserving Bun as the package manager and script runner. It standardizes Vitest configuration, migrates package test scripts and imports, makes shared test infrastructure runtime-agnostic, and adds a canary so future --bun regressions fail loudly.

Loop Engineering from First Principles | Kyle Mistele | @0xblacklight
AI Engineer World’s Fair

Interface text:
Merged
K-Mistele merged 15 commits into main from plan-migration-from-bun-to-vitest-test-runner 2 weeks ago
Code
Conversation 7
Commits 15
Checks 11
Files changed 498
+17,073 −19,195
K-Mistele commented 2 weeks ago · edited
Reviewers
dexhorthy
Request
Projects
None yet
Milestone

### Running a Prompt in a Loop

The shell loop repeatedly pipes PROMPT.md to Claude. Mistele points out that this loop is not incremental.

[7:00](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=420s) [A shell command repeatedly runs a prompt through an agent, overlaid on the earlier Venn diagram.](https://assets.ai.engineer/sites/aiecode2025/sha256/123efcd64cc04491cad363307789eeb27d9485ca768fbd807d703353a25f2706.webp)

# run a prompt in a loop forever
while :; do cat PROMPT.md | claude; done

Loop Engineering from First Principles | Kyle Mistele | @0xblacklight
AI Engineer
World’s Fair

Interface text:
Chrome  File  Edit  View  History  Bookmarks  Profiles  Tab  Window  Help
Tue Jun 30  3:53 PM

## Full transcript

[0:00](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=0s) [upbeat music]

[0:27](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=27s) Hey, everybody. My name's Kyle. I'm co-founder of a company called HumanLayer, and I'm here to talk about loops. I think we've all been building loops lately, and I've realized recently I think we're all doing it wrong.

[0:41](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=41s) Loops are really powerful, don't get me wrong. But so much of the discourse around them is hype driven and just really not helpful, right? We, I think we have this idea, just kind of as an industry somehow, that we can, like, pipe a prompt in a loop to a coding agent and that we can build software this

[0:57](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=57s) way, right? Maybe we're investing a lot of time in verifiers. Maybe you have six different code review agents. But at the end of the day, if we're doing this, we're still building forty thousand line PRs that just nobody wants to read, right?

[1:12](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=72s) And this isn't to throw shade to Geoff Huntley, right? This is... Ralph is an innovative, uh, it's a sharp tool that works very well for certain types of problems.

[1:22](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=82s) It works very well if you're not building on a team, and it works very well if you're not working on critical systems. But most of us are working on teams, and we don't fit in that box.

[1:33](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=93s) So today I wanna talk about how to build loops that work in large, complex code bases for systems that have real customers, real users, real regulatory obligations and service level agreements, and everything else that keeps us from shipping YOLO forty thousand line PRs straight to production.

[1:51](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=111s) In other words, I wanna talk about how to build loops for the real world.

[1:55](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=115s) If you're not aware, uh, this post actually dates back to July. It went viral this past January, which is when a lot of us, I think, started building loops.

[2:03](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=123s) And of course, much more recently, I'm sure y'all are gonna see this slide a lot this week. Uh, but Peter Steinberger said that we shouldn't be prompting coding agents anymore, right?

[2:12](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=132s) We should just be designing loops that prompt our agents. Of course, OpenClaw's notoriously built on loops. Loops build the code. Loops review the code. They merge and release the code.

[2:22](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=142s) They find and fix the bugs. There's even loops for finding and fixing bugs in the loops that are merging the things, right? It's loops all the way down. It's, uh, Boris Cherny, also the creator of Claude Code, recently said that this is his entire job as an engineer now is just writing loops to prompt Claude.

[2:41](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=161s) And eventually, we might not even need loops, right? We're just gonna have, like, swarms of agents designing loops to prompt agents building swarms for loops and, like, I don't know, somewhere we're, like, writing production code, I assume.

[2:56](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=176s) And in fact, all of our loops that we're building are producing so much code that we can't possibly read all of it, right? So we might as well just not read any of it, right?

[3:06](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=186s) We're, we're investing in verification and in code review, but all this code is write-only. This was the thesis of a conference that, uh, was here in town last month.

[3:14](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=194s) So a lot of smart people at the frontier labs think that this is the future of software development, and if you're doing this, you're moving ten X faster and everybody else is getting left behind.

[3:26](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=206s) Now, it's not clear how well this works yet. Uh, took six months to fix the Claude Code terminal flicker. The OpenCode team wrote a renderer in a fraction of that time.

[3:36](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=216s) And OpenClaw, of course, also notoriously has stability issues. What is abundantly clear, however, is that this shit is really expensive if you don't work at a frontier lab and have an unlimited token budget.

[3:50](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=230s) And all this code that we're writing is actually really expensive, right? Matt Pocock talked about this recently. Uh, bad code is much more expensive in the age of agents than it, it has ever been at any point in the past.

[4:03](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=243s) So today I wanna talk about what I think works in the real world and what we've started doing at HumanLayer, which, to be clear, is still building loops, right?

[4:12](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=252s) I think loops are super powerful, but we can design loops and still read the code. In fact, we can design loops that make it easier to read the code because the loops are making the code better.

[4:23](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=263s) We can solve hard problems in complex code bases with loops, and we can build our software factory incrementally. But, uh, to do this is gonna take some real engineering, y'all.

[4:33](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=273s) So let's talk about control theory. Control theory is all about how we drive a dynamic system, which would be your code base, towards some desired, stable or optimal end state, right?

[4:43](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=283s) You have a sensor that measures the current state of the world. You have your set point, right? The desired state of the world. And the difference between those two things is your measured error.

[4:53](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=293s) You have a controller that reads that measured error and turns it into a control signal about an incremental change to apply to the system. We have an actuator that applies that change to the system, which is undergoing disturbances in the meantime.

[5:07](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=307s) And then we re-measure, recompute our measured error, and we're back where we started. Now, this sounds really complicated, and it can be. I have a twin brother, actually, who's an aerospace engineer.

[5:18](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=318s) This is how they keep fighter jets from falling out of the sky. Uh, but, uh, it's probably a little bit simpler than most of y'all think. Does anyone have one of these?

[5:28](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=328s) Uh, a thermostat uses a control loop, right? Uh, for, for any of our European friends in the audience, it's part of something we have, uh, here in the States.

[5:37](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=337s) It's called air conditioning. [audience applauding] And, uh, most of us probably actually use control loops on a daily basis, right? Kubernetes auto-scaling systems are built on control loops. Infrastructure as code uses a desired state, current state, iterative change, like control loop pattern.

[5:58](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=358s) PostgreSQL's, uh, autovacuum and React's virtual DOM both use or approximate control loops. Control loops are ideal when we have a system that we wanna change, a problem we can measure, and a way to get feedback on the result of that change.

[6:12](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=372s) Like good software engineers have always been taught to do, control loops change a system incrementally instead of just trying to get straight to the end state immediately all at once and risk blowing everything up, right?

[6:23](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=383s) They help us to avoid oversteering and destabilizing the system, and it minimizes risk.

[6:30](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=390s) So, control loops are the opposite of what I'm gonna call a blind Ralph loop. They're how we avoid PRs that look like this, 'cause nobody wants to review this, right?

[6:39](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=399s) Which is not to say that all Ralph loops are blind loops. The best Ralphs are actually applying control theory. I know Geoff Huntley is out in the hall somewhere wandering around.

[6:47](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=407s) If you go talk to him, he's gonna tell you the same thing, right? That Ralph is a- a teaching device, and I think some of us read it a little too literally, but this is how we should have always been building loops.

[6:59](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=419s) But the other issue with Ralph loops is they're not incremental, right? It's just a bash loop. So we have to build agentic control loops. And to do that, we start by defining a set point, which is the desired end state of our code base with respect to some property of it, and we add a sensor.

[7:15](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=435s) There's a lot of ways to build a sensor. It can be strictly deterministic, your ESLint rules, your ast-grep, your Packwerk, or it can be non-deterministic. You can have an agent and a skill and a bunch of natural language rules, and you could also just have a pipeline, like a combination of the two.

[7:33](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=453s) So how do we build agentic con- whoops. There we go. Now, uh, this is all theory, right? Practically speaking, and because we're using agents, we can blur the lines a little bit between system components.

[7:45](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=465s) So Aiden Bai's React Doctor, for example, is fantastic. It is, uh, it's a great way to catch all of the React slop that Claude snuck into your code base last week.

[7:56](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=476s) But, uh, it's a hybrid sensor and controller. It tells you what are all the problems with your React code, and also, by the way, what are the top three things you should fix and how do you fix them?

[8:06](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=486s) Similarly, our controller and actuator might actually just be a single agent deciding on an incremental change to make and then applying it in the same context window. But I wanna zoom in on the controller a little bit because without one, or without a well-tuned one, we might make too large of a change all at once, or we

[8:23](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=503s) might make the wrong change entirely. And if you put that in a loop, you're in trouble pretty quickly.

[8:29](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=509s) So we can use control loops to root out bad patterns and to clean up our code, but we can actually use them for all sorts of things, right? We could make sure that our API is compliant with someone else's OpenAPI spec.

[8:40](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=520s) We can make sure that our MCP server is compliant with whatever version of the, uh, the MCP specification that we're currently on. Haven't checked. You could mirror a project from Python into TypeScript or vice versa.

[8:54](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=534s) You could even maintain your, uh, Vite-based slop fork of Next.js against the upstream. The key questions are, can we find something we can measure? Can we apply changes incrementally?

[9:05](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=545s) And can we get feedback on the quality of those changes? To illustrate that, I'm gonna walk through a control loop that we use internally at HumanLayer. Uh, for our loop, we are incrementally migrating our RPC API to Effect.

[9:18](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=558s) We adopted it for some of our race-prone code. We like it, so we're adopting it across the rest of our code base. If you've never seen Effect code before, the code on the right is just the kind of trivial procedure on the left rewritten in Effect.

[9:29](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=569s) Uh, the syntax is really weird. We're psychos. We really like it. It's not for everybody. That's okay. Uh, this isn't a talk about Effect, so we'll keep moving. Ooh, clicker's not working.

[9:40](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=580s) Cool. So step one, we have to build our sensor to find unmigrated procedures. We can have an agent do this, or we could use grep or ripgrep. But instead, we're gonna use ast-grep 'cause it's really powerful.

[9:52](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=592s) It's a great tool to have in your toolbox for building loops. It's language agnostic. It's out of band from your TypeScript config or ESLint rules, which if you're a TypeScript developer, you have watched Claude disable those with inline comments.

[10:05](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=605s) Uh, but so we can just write a simple rule that finds unmigrated procedures, uh, based on the pattern above. And we, over time, we can even layer on more rules that describe other patterns we wanna get rid of with granular include and exclude paths.

[10:17](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=617s) If you have a multilingual monorepo like we do, uh, it'll work for any language you could possibly imagine. And we can just scan our code base, and it'll produce a long list of violations.

[10:27](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=627s) Uh, way too long, in fact. It'll give you about 50 keys per violation, so we're just gonna filter it down to four, and we're gonna sort it deterministically. Why are we doing that?

[10:36](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=636s) At the beginning, I said this was gonna be practical, and so we're gonna step outside of our control loop paradigm for a second because before we start incrementally migrating procedures one at a time, we need to enforce that all new procedures are using Effect, right?

[10:48](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=648s) So we're gonna run a full scan once on main, sort all the violations deterministically, and track it in our version control. And then on every new PR, we can see if a- the branch added any unmigrated procedures, right?

[11:01](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=661s) So this is our control loop, and our system is undergoing disturbances. In this case, uh, all of our teammates shipping Claude slop, and this is how we make sure that they're not undoing our loop's work.

[11:11](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=671s) This doesn't map directly to a part of the control loop, but we can kind of, like, squint at it a little bit and call it a disturbance dampener. So now that we've stopped the bleeding, we can actually design our controller.

[11:21](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=681s) For a simple controller, we could just deterministically pick the first violation from the list. You can use Bash and jq. Or we could get a little cleverer and use ast-grep to find the smallest unmigrated procedure and always pick the smallest one to reduce the risk.

[11:38](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=698s) Uh, we could have an agent make the decision if we really want to. I don't think you should ever send an agent to do deterministic code's job, but you certainly can.

[11:45](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=705s) In fact, depending on the complexity, we could have the agent pick the procedure to migrate and just do it at the same time, like we just talked about. But we can make this even more powerful, right?

[11:54](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=714s) Because we're not just migrating to Effect for the sake of it. We're doing it because it's helpful for handling errors and for helping us instrument our code better. And so what we could do if we wanna get really clever is we can look at our telemetry and figure out which procedures have the most errors or the least

[12:08](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=728s) instrumentation or has a gap in our APM, right? And when we send a control signal to our actuator agent, we can include not just the procedure to migrate, but also all the data about the things that we're trying to fix with this migration so that the actuator agent can actually make the code better instead of just doing

[12:26](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=746s) a one-to-one migration. Oh, man. There we go. So next is building our actuator. Our actuator is just an agent plus a skill. Um, bring your CLI coding agent of choice.

[12:41](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=761s) You should spend a lot of time on the skill. Not all of that should be up front. You'll want to iterate on it over time based on what works.

[12:47](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=767s) At HumanLayer, we like to build out what we call golden patterns by hand before setting the agent loose. These are just, like, idiomatic handwritten examples for the agent to follow because they're just pattern replicators, and otherwise you're getting what's in the docs or what the agent knows from the internet.

[13:00](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=780s) And so we pipe the skill plus our control signal into our actuator agent. And the skill, of course, should include a response template, and the agent's gonna work, and work, and work, and it'll produce a final response.

[13:13](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=793s) And then we're going to deterministically commit, and push, and create a PR using the final message as our PR description. Now all we have to do is actually run the loop, right?

[13:22](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=802s) Uh, my recommendation is to use GitHub Actions, or your GitLab, or your CircleCI, or whatever else you're using because it has access to your code, it has access to your secrets, and it has great dispatch and scheduling primitives, right?

[13:36](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=816s) We don't need a new cluster for this. So we can write a workflow that runs a single iteration of the loop, sense, control, actuate, and creates a PR. And then we can schedule this to run once a day.

[13:46](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=826s) And every morning, we walk into the office to a small incremental PR that's low risk. And when we first did this, it was actually really frustrating, and we turned the loop off, and it, uh ...

[13:57](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=837s) 'Cause we had to constantly update the skill. We had to constantly check out the branch, change the skill, change the code, commit and push, and our loop was actually really high friction, right?

[14:06](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=846s) But there's a better way to do this, uh, where we can put a human on the loop in a really low friction way to resteer it when it goes wrong.

[14:13](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=853s) And the way to do this is to just create a feedback file that's tracked in version control, just as a markdown file, right? We can deterministically load it into our actuator agent's context every time that it runs after we run the controller.

[14:24](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=864s) Then we can add a label to the PR, right? Each workflow needs to be able to identify PRs that it created since there might be a bunch of different loops running, and we only want workflows to respond to feedback from, uh, comments on their PRs.

[14:36](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=876s) And we're going to add a comment trigger to each loop workflow, so that when a user leaves a /iterate comment on the PR, uh, the loop workflow is gonna pick that up.

[14:44](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=884s) It's going to deterministically load all of the PR context, the diff, the comments, the review comments, the description into the agent's context along with the skill, and it's going to instruct the agent to fix the code, but also to update that feedback file, right?

[14:58](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=898s) It looks kind of like this. And the benefit of doing this way is that now that feedback file with instructions is tracked in your version control. You can see how you've changed it over time.

[15:07](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=907s) You can revert it if you need to.

[15:10](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=910s) So the next thing we're gonna do is add flow control because the other problem that we had when we did this was that if we were at a customer site for a week, or if we were traveling, or spent six days working on slides instead of writing code, uh, the PRs from all of our loops would just

[15:25](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=925s) stack up. They'd duplicate work, they'd conflict, and we wouldn't get around to doing it. And, like, the loop's work is important, but it's not that important. And so now we just had all this, like, junk we had to deal with that wasn't important.

[15:37](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=937s) So this is actually a really easy problem to fix, uh, because each loop and its workflow has a label that gets attached to PRs. When the workflow first runs, uh, before we check out the code, and install the dependencies, and run our sense actu- or sense, control, actuate steps, we can just check and see if the last

[15:54](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=954s) PR that we created or any PR with the loop's label on it is open. And if so, we just shut down, right? Because this means that the last time that a human, uh, reviewed the code from this loop was before the loop ran, right?

[16:08](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=968s) No human reviewed the last output, so there's no reason to stack up even more work for humans to review. This way we have exactly one PR at most open per loop at a time.

[16:17](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=977s) No stacking, no duplication, hopefully no conflicts. And of course, once you're feeling confident in the loop, we're gonna wanna speed it up, right? I have 150 RPC procedures to migrate.

[16:27](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=987s) If I do one at a time, it's gonna take six months, which is way longer than I wanna wait. Fortunately, there's a lot of ways to pick up the, the velocity of our loop.

[16:34](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=994s) Uh, we could have our controller pick three procedures to migrate instead of one at a time or five. Uh, we could have our controller pick three or five and then do each of those in a separate implementation phase, which will be both cheaper and more reliable since each migration gets its own context window.

[16:50](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=1010s) Or we could just run the workflow four times and give one PR to each of four people on the team. So let's put it all together. We built a control loop that improves our code incrementally, and we're actually reading the code.

[17:03](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=1023s) It has adaptive flow control, so we're not creating a bunch of loo- or a bunch of work that nobody wants to review, and we can resteer it on the fly in a super low friction way.

[17:12](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=1032s) If you want to try this yourself, uh, we built a skill. Please try it out. My Twitter handle is down there on the bottom. Please share it. I would love to see what you build.

[17:20](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=1040s) And if you get excited by this, uh, at HumanLayer, we're hiring here in San Francisco. And if you're working on mission critical systems and want to figure out how to get more out of AI, we'd love to chat.

[17:31](https://www.youtube.com/watch?v=xIt_mTQp6mY&t=1051s) Thank you so much. [audience applauding] [upbeat music]
