I was building loops wrong... — Transcript
Full transcript
- 0:00It was 12:00 a.m. yesterday and a bunch
- 0:02of PRs still come into our codebase and
- 0:04it was not because of us working
- 0:05particularly hard, but we have agent
- 0:07wake up every 30 minutes, scan our
- 0:09codebase for any improvements and
- 0:11errors, check our server logs, and ship
- 0:13a PR. And every PR in Verify agent will
- 0:15test fully and attach your evidence for
- 0:16us to review. For some low-risk fix, it
- 0:18can even merge directly. And also, a CRM
- 0:20lifecycle agent will scan through our
- 0:22user base and find useful segments to
- 0:24outreach and nurture. And also, poor
- 0:26ticket is automatically handled, no
- 0:28matter which language users are
- 0:29speaking. And none of this is a demo.
- 0:31All this loop has been running our
- 0:32company Super Divine for the past
- 0:34months. And today I'm going to show you
- 0:35exactly what we learned from building
- 0:37this loops, what worked and what didn't
- 0:38work. So, hopefully by the end of this
- 0:39video, you understand loops than most
- 0:41people in your timeline. So, last week I
- 0:43wrote about Loop Engineer, the shift
- 0:44from you prompting the agent to do a
- 0:46task towards designing system where the
- 0:48agent can decide what to work on,
- 0:50execute, verifies, and improve over
- 0:52time. And a lot of you said, "Okay, I'm
- 0:53sold, but how do I actually
- 0:55[clears throat] build one that actually
- 0:56works?" Because honestly, everyone can
- 0:58just put together a loop using Copilot
- 1:00or Codex and code it down. But that's
- 1:02easy 5%. The real work is how do you
- 1:04design guardrails that let you walk away
- 1:06from it? The loop that can consistently
- 1:08delivering the work safely and improve
- 1:10themselves over time. So, every loop we
- 1:11build internally has this exact
- 1:13structure. We normally have one markdown
- 1:15file that is holding both what we call
- 1:16loop contract as well as state and lock
- 1:19of this loop. And this serving as a
- 1:20living documentations about the loop
- 1:22itself. Then there will be trigger layer
- 1:24that actually wake up agent to do the
- 1:25work. And there are different type of
- 1:27trigger, which I will talk through a bit
- 1:28more. Then the actual agent that's going
- 1:30to find a work, does the work, improves
- 1:32the work has been done properly, and
- 1:34keep updating the docs. So, the next
- 1:35round starts smarter. So, first part of
- 1:37the loop contract, this is crux of the
- 1:39MD file. It's like the constitution of
- 1:41loop. There's three things really matter
- 1:42and lives inside. One is the goal. So,
- 1:44what does the winning look like? Whether
- 1:46there's even a finish line. And also,
- 1:48the boundaries. You want to make it very
- 1:49clear what agent can just do by itself
- 1:52versus the ones that actually need to
- 1:53escalate human. And third is SOP. If you
- 1:55have a specific workflow or principles
- 1:57that you want agent follow every time.
- 1:59And after that, there's a state and
- 2:00logs. So, this is what it keep agent
- 2:02remember what are the things that they
- 2:03have tried before, what are the
- 2:05learnings. And I'm splitting into two
- 2:06parts. One is the state. So, state is
- 2:08like a durable picture. What's the
- 2:10current hypothesis agent has? What are
- 2:12the old backlogs? What are the things
- 2:13that agent has shipped but needs follow
- 2:15up? And this part generally you want to
- 2:17keep it very small and on purpose.
- 2:19Versus log is the append-only record of
- 2:22whatever happened run by run. And
- 2:24without this block, every morning in
- 2:25loop just rediscover the same noise
- 2:27arrow, waste token on chasing the scene
- 2:28that already tried. And for most of
- 2:30small loops, you can basically just put
- 2:32most loop contract and state and block
- 2:34into just one file. And let's take
- 2:35through one example. This is one loop we
- 2:37are running in our code base called
- 2:38react doctor checks. What it does is
- 2:40that we're utilizing this open source
- 2:42CRI tool called react doctor, which is
- 2:44CRI you can run in your phone and code
- 2:46base and it will automatically identify
- 2:48some of the critical issues, listing
- 2:50them and give you score. So, this loop
- 2:51is basically uh running this react
- 2:53doctor scan on our code base every day
- 2:56and pick up the one of the most critical
- 2:57issue to fix automatically. So, in this
- 2:59loop's contract doc, we have a scope
- 3:00folder that is talking about the purpose
- 3:02of this loop and also a boundary
- 3:04section. And we're talking about how it
- 3:06should spawn a sub-agent to do the fix
- 3:08in isolated work trees format and how it
- 3:10needs to run through the verification
- 3:12process as well, as well as what kind of
- 3:14change it can just merge by itself
- 3:16versus one that it needs human reviews.
- 3:18Below that, it lists out a SOP about the
- 3:21common workflow it should follow, as
- 3:22well as a state section that is
- 3:24mentioning its current understanding
- 3:25about this loop, what are the tickets
- 3:27and PR already open and shipped, what
- 3:29are the things that needs follow up, and
- 3:30then append-only logs after each round
- 3:33to log what are the things has been
- 3:34happening. So, every day we agent will
- 3:36be wake up with this documentation as a
- 3:38prompt to trigger the agent start the
- 3:39work. And we actually build an internal
- 3:41tool so that we have a dashboard to
- 3:42track what are the PR has been opened
- 3:44and merged, and how is the react doctor
- 3:47health score improve over time. And this
- 3:48loop contract documentation is not just
- 3:50applicable to engineering tasks. As I
- 3:52mentioned, we also have this CRM uh uh
- 3:54life cycle loop, where every day I would
- 3:56have the our agent to monitor all the
- 3:58daily active users, group them into
- 4:00different segmentations, like ones are
- 4:02small influencers that we should reach
- 4:03out for affiliates and distribution,
- 4:05users who are clearly frustrated based
- 4:07on the large language model logs we
- 4:08have, as well as users who are pretty
- 4:10engaged but haven't upgraded yet. And
- 4:11for each segment, depending on the
- 4:13priorities and risk level, agent can
- 4:15just either auto outreach or draft a
- 4:17message to waiting for me for approval.
- 4:19And this CRM daily run also has very
- 4:20similar structure. So, we have this
- 4:22documentation that is talking about the
- 4:23goal and SOP for this loop. Then we have
- 4:26a list of boundaries. What are personal
- 4:28pitfalls agent should be aware of when
- 4:30they were doing the research, and what
- 4:31kind of message agent can just send by
- 4:33themselves versus where need approval.
- 4:35Then it has state section that listing
- 4:37out its current understanding and
- 4:38hypothesis, as well as the timeline.
- 4:41Since So, since we have been running
- 4:42this loop for like almost a month, this
- 4:44documentation just keep getting updated
- 4:46after every single run. And the
- 4:47structure we have is basically have loop
- 4:49folder and under each loop, we can
- 4:51contain one readme file. And sometimes
- 4:53there might be other artifacts, which
- 4:55can also be saved in the folder and
- 4:56referenced in that readme file. So,
- 4:58these are first two parts, the loop
- 4:59contract as well as the state log. The
- 5:01next part is a trigger. So, every loop
- 5:03will have some sort of a trigger, but
- 5:05depending on type of loop, you might
- 5:06define trigger differently. And part of
- 5:08reason why current loop were feel a bit
- 5:10confusing is because of different
- 5:11trigger types. So, we all know Codex and
- 5:13Cloud Code has this go command, where
- 5:15you can define condition and give it go.
- 5:17Then you'll keep running for loops until
- 5:19task has completed. And this is the
- 5:20first type of trigger, is like a
- 5:22continuous for loop. You can imagine
- 5:24behind things is just a while loop.
- 5:26While the goal is not satisfied, or you
- 5:28sometimes can define max back and forth
- 5:30turns, or a token budget. And before
- 5:32those things are hit, it will just
- 5:33continuously doing the same, look at
- 5:35this contract, do next step, until the
- 5:37goal is satisfied. And this as I
- 5:39mentioned before is basically the goal
- 5:40command. It is particularly good for
- 5:42scenario where you can get immediate
- 5:43feedback loops, like bug fixing or
- 5:46implementing a piece of complex software
- 5:48that the specs is well defined. And
- 5:49meanwhile, second type of trigger is
- 5:50also type of cron job. This is like
- 5:52Codex automation or cloud codes loop or
- 5:54schedule comment. Fundamentally, it will
- 5:56just trigger and wake up the agent in
- 5:58certain interval. And the only
- 5:59difference between loop and schedule is
- 6:01that one is running in the cloud versus
- 6:03one is running in the same session. And
- 6:04these two type of trigger already handle
- 6:06a good amount of scenarios. But often I
- 6:08found also useful to have event-based
- 6:10loop as well, which means it's kind of
- 6:11reactive to certain events, like
- 6:13whenever there's a new email, you might
- 6:15want to wake up agent and let it handle
- 6:17the email. Or whenever your server get
- 6:19incident, then you want to wake up the
- 6:20agent and get it to fix the incident
- 6:22immediately. And those type of
- 6:23event-based trigger is particularly good
- 6:26for things that needs immediate
- 6:27handling. However, both cloud code and
- 6:29Codex don't natively support those type
- 6:31of triggers. So, what you want to do is
- 6:33actually set up a local daemon process
- 6:35that expose a URL for any webhook to
- 6:37send notifications. So, for example, you
- 6:39can create a webhook on Render and uh
- 6:41point to your local daemon URL to
- 6:43receive certain failed events. And
- 6:45fourth and probably the most useful type
- 6:46of trigger I found is what I call uh
- 6:49combo or workflow. So, we can have a
- 6:51ticker that's still running interval,
- 6:53but instead of trigger the agent
- 6:54immediately, ideally we just run a
- 6:56script to fetch from data source to
- 6:58check programmatically the where there
- 7:00are new works to handle. For example,
- 7:01for our support inbox triage loop, we
- 7:04actually define a JavaScript here. So,
- 7:06you can fetch from Intercom what are the
- 7:07recent updates from the past 30 minutes.
- 7:09If there are any real updates, then
- 7:12trigger the agent to handle. If not,
- 7:13just skip this this the run. And this
- 7:15one we found is particularly effective
- 7:17and useful because we can run the loop
- 7:19in much more cost and token efficient
- 7:21way by batching a good amount of work
- 7:23together for the agent to handle and
- 7:25only wake up agent when there's real
- 7:26work. And both first two triggers
- 7:28support out of box from cloud code and
- 7:29Codex, but the other two type of
- 7:31triggers you will need to set up your
- 7:33own local script and daemon service. And
- 7:35for us, we have this internal tool that
- 7:36we built to run these type of triggers,
- 7:39which we just open source and you can
- 7:40run locally. It talks directly to your
- 7:42local agent, but your team will have
- 7:43this kind of centralized place to manage
- 7:45all the loops, contracts, state, login,
- 7:47triggers. And I'll talk about this
- 7:48project a bit later. But this third part
- 7:50of the trigger, I think choosing the
- 7:52right trigger for the specific type of
- 7:53loop that you're running will really
- 7:55drive down the cost a lot. Third is the
- 7:56actual agent that's is going to be wake
- 7:58up and execute the tasks. And normally
- 8:00agent will go through the three stages
- 8:02of First, it gathers the signals and
- 8:04find and prioritize the work. Then start
- 8:06executing the task. And if it's a
- 8:08complex, high-stakes task like engineer
- 8:10tickets, you typically will have a
- 8:11verifier at the quality before it claim
- 8:13finish. For simpler tasks, you might
- 8:15have just one agent doing both three of
- 8:17these jobs. But for more complex type of
- 8:19tasks, you typically will want to break
- 8:21down into three different roles. And
- 8:23most common structure is this
- 8:24orchestrator agent which will receive
- 8:26the prompt doing the research and
- 8:28planning of the task. Then sponsor
- 8:30agents, each one working isolated work
- 8:32tree, so tasks can be run in parallel.
- 8:34And each executor should hand off task
- 8:36to a verifier who can test the result
- 8:39and also attach evidence to the PR, so
- 8:41it can become much easier for human to
- 8:43review the tasks. And all the updates
- 8:45will be sent back to the loop contract
- 8:47doc. So this kind of the core piece of
- 8:49every loop that we run. And I have
- 8:51talked about verifier quite a bit
- 8:52before, especially for engineer task.
- 8:54This is like the prerequisite to any
- 8:56loop that delivering high-stake work
- 8:58like real production code change or
- 9:00messaging real customers. Basically, you
- 9:02want to making sure the process is quite
- 9:04easy and produce evidence that human can
- 9:06easily review. Generally, that means
- 9:07that you want to give agent an
- 9:09environment that they can verify their
- 9:12work token efficiently. If you've been
- 9:13following my content, I've made a few
- 9:15articles around that already regarding
- 9:17how do you use things like Playwright
- 9:19CRI to let agent test and record video
- 9:21or image evidence about the work and
- 9:23utilizing things like Crabbox to set up
- 9:26remote sandbox environment to do the
- 9:28test, so you won't be restricted about
- 9:30how many dev server you can run in
- 9:32parallel on your local machine. And I
- 9:34have packaged this links into a skew
- 9:36called verifier setup, so you can just
- 9:38give to cloud code or codex in your
- 9:40actual code base. Then set up the proper
- 9:42verification systems. And I put the link
- 9:44in the description below for this GitHub
- 9:47repo with all the skills that we talk
- 9:48about here. So you can use it for free.
- 9:50So this is overall system and anatomy of
- 9:52what does a good loop look like in our
- 9:53case. And meanwhile, another really
- 9:55useful layer we found is this evolve
- 9:57loop. So the concept here is that most
- 9:58of loop when you just getting started
- 10:00might not be perfect. There could be a
- 10:02lot of room for improvements like how do
- 10:03you design the trigger? So it's more
- 10:05cost effective. How can you turn some of
- 10:07repetitive SOP into script that agent
- 10:09can run? And a lot of those improvement
- 10:11can actually be optimized by the large
- 10:12language model itself. If you give agent
- 10:14all the existing configuration of the
- 10:16agent, the past run state and logs, as
- 10:19well as the raw conversation history so
- 10:21that it can inspect further. And all we
- 10:22need to do is just after every five or
- 10:2510 loop runs, we trigger a dedicated
- 10:27evolve session where the agent will be
- 10:29given the existing configuration, past
- 10:31log histories, then prioritize some
- 10:33change. And change can be the the loop
- 10:36contract itself or some outdated state,
- 10:38as well as the trigger script that you
- 10:39can create for repetitive actions. And
- 10:41support loop here is actually a perfect
- 10:43example. It was actually during the
- 10:45evolve run, it starts setting up this
- 10:47programmatic trigger to only wake up
- 10:49agent when it's necessary. So this is
- 10:50overall system and anatomy of what does
- 10:53a good loop look like for us. And we can
- 10:55go through a few examples together to
- 10:57set this up. And one really good one to
- 10:59start with is this documentation
- 11:00maintainer loop. So I know most of you
- 11:02will have either documentations or some
- 11:05sort of cloud or MB and code base
- 11:06documentation file to help agent gain
- 11:08the full context. But that context can
- 11:10often get outdated. We can solve this by
- 11:12setting up a simple loop where every day
- 11:15it can wake up the agent to check what
- 11:17has been shipped past 24 hours and what
- 11:19are diffs. And compare those change with
- 11:21the read me, the setup guide, the
- 11:23examples and run books. And for each
- 11:25abnormal part, it can actually verify
- 11:27which one is actually true. And if
- 11:28there's nothing stored, then just finish
- 11:30the session. But if there is, then you
- 11:32can just make some small quick fix to
- 11:34making sure the documentation is up to
- 11:35date and open a PR for that. This loop
- 11:38is very small, but quite a useful to
- 11:40use. And the way we will set up is loop
- 11:42contract look like this. They clearly
- 11:44list out the goal and the boundary about
- 11:46what it can ship by itself and what are
- 11:48the things it is allowed to do. And you
- 11:50will notice this one rules here called
- 11:52never rewrite accurate doc to look busy
- 11:54cuz one default behavior we saw agent
- 11:57has is that it will tend to do something
- 11:59even though it's not necessary. So this
- 12:01kind of the rules is actually quite
- 12:02useful to making sure it produce useful
- 12:05results. And then in SOP and state and
- 12:07logs. So you can simply give this
- 12:09markdown file to your cloud code or code
- 12:10acts and ask it to set up loop based on
- 12:12this. But as I mentioned before, we
- 12:14actually build this internal tool that
- 12:16we have been using to coordinate and set
- 12:18up loops for us. It allow you to define
- 12:19those kind of programmatic triggers that
- 12:21we shared before. Also set up a contract
- 12:23based on the best practice that we
- 12:25learned. And also past raw wrong logs
- 12:27will also be stored so the agent can
- 12:29remember right itself. And it has this
- 12:31evolve loop behavior building
- 12:32automatically. So you can see after
- 12:33every few sessions there will be a blue
- 12:35dot popping up. And this blue dot is a
- 12:37dedicated evolve run. They will look at
- 12:39past run and then figure out
- 12:40improvements that can be done. Like in
- 12:42this specific evolve, it sharpen the
- 12:44specs and SOP, clean up some outdated
- 12:46state, and also update dashboard for us
- 12:48to easier track the performance, which
- 12:49we found is actually pretty useful.
- 12:51Because quite often you don't really
- 12:52know the output from the loop. But with
- 12:54this one we can easily track what are
- 12:55the open things that needs our attention
- 12:58and how the performance change over
- 13:00time. And if needed, can just click a
- 13:02button which will copy all the contacts
- 13:04over about this loop to my local
- 13:05computer so I can chat with agent to
- 13:07evolve the loop. And we have included a
- 13:09few templates here already. And doc
- 13:11maintainer is one of them. So I can just
- 13:13copy this prompt and go to my repo and
- 13:15paste it in. It will automatically
- 13:17include a list of instruction about how
- 13:20this loop should be built alongside some
- 13:22best practice that we encode here. Then
- 13:24it will automatically create this loop
- 13:25knee folder in your code base as well as
- 13:28this doc drift sweep loop. So this is a
- 13:30loop where it will talk about scope of
- 13:32things it should review which is readme,
- 13:34cloud MD, everything in the docs folder,
- 13:36the skill file, the scripts, and then it
- 13:38will talk through the SOP here and the
- 13:40state of current understanding and
- 13:41timeline items. And then we'll have this
- 13:43loop created on Loopery which will
- 13:44automatically trigger the agent on time
- 13:46and keep log of things ever shipped, all
- 13:48the artifacts and all the runs. And
- 13:49there are few other templates that we've
- 13:51been using and you can just copy paste
- 13:53like this react doctor loop. Other loops
- 13:55that periodically clean up the tech
- 13:57debt. And this is totally open source
- 13:58and free to use. So feel free to just go
- 14:00to GitHub and start using it. I've put a
- 14:02GitHub link in the description below so
- 14:04you can try. Thank you and I'll see you
- 14:05next time.
About this transcript
This page contains the full transcript of I was building loops wrong... by AI Jason, generated from the public captions YouTube serves with the video. The transcript has 3,244 words across 462 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.