β€’

tech

AI code with loops and subagents

The new trend in AI coding is called loops. Well, in reality those are just subagents. Yes, subagents that you don't manage, but another AI does for you. And you just watch, and review.


Sandro Maglione

Sandro Maglione

Software

We reached the next milestone of AI coding: loops πŸͺ„

But I would argue the naming is bad, and it doesn't sell that well πŸ™Œ

It has not much to do with older "Ralph loops", and no manual configuration.

It's mostly a prompting "trick", that will change how you structure tasks πŸ‘‡


History of AI coding (until today)

It was a long time ago (like a few months, maybe 1 year?) when AI was mostly okay-ish only for "smart" autosuggestions.

No major change, you write your code, sometimes the AI predicted what you were about to write.

Check "Walking your code with AI" for my first newsletter about this (8 months ago).

Then, fast as lighting, a few weeks later AI started to take over code ⚑️

Tweet not found

The embedded tweet could not be found…

"How to write maintainable code with AI" was the first instance of this in the newsletter.

Since then the newsletter shifted a lot into AI-related topics, as new trends came and went:

  • Maintain code with AI
  • Ralph loops
  • Review AI code

However, all these practices relied on a shared assumption:

You know what to do and prompt an AI for it (1 or 20 at the times), the AI does it, and you review πŸ“

Well, the new "loops" iteration looks different πŸ‘‡

Loops, aka subagents

Here is the new game:

Instead of prompting 20 AIs yourself, you prompt 1, and tell it to spawn and orchestrate the work of 20 others πŸ—οΈ

With Codex, this is a only a matter of changing your prompts (and your mental model).

This is how it looks like: say Hi to Ampere, Nash, Plato, Jason, and Kant πŸ‘‹
This is how it looks like: say Hi to Ampere, Nash, Plato, Jason, and Kant πŸ‘‹

How to trigger subagents

Here is my most recent concrete example of how to rewire your prompting:

I had an app working on web, and I wanted to implement it them same on mobile (Expo) πŸ€”

What I used to do was:

  • Prompt the AI to scaffold the mobile project
  • Prompt another AI to implement the routes
  • Prompt another AI to implement the styles

Basically, I would spin up one or more AI at the time myself, each prompted by me, and manually manage parallelisms and dependencies.

This was a decent workflow, reliable, but slow πŸ‘€

Now the game changes:

You prompt 1 AI, plan the full migration with it, define how many subagents are needed and in which order, and let that single AI manage it all the way to the end πŸ™Œ

How subagents look like

An single image tells all the whole story:

On the left, the orchestrator agents planning the subagents to spawn. On the right, the full list of agents that were used to complete the full migration.
On the left, the orchestrator agents planning the subagents to spawn. On the right, the full list of agents that were used to complete the full migration.

The mental shift:

Instead of thinking about how to manage multiple agents, teach 1 agents the plan, and let it manage the full platoon 🫑

In practice: add "spawn subagents to manage each separate part of the plan" to your prompt. Codex knows, and the AI will just do it πŸ’πŸΌβ€β™‚οΈ

Review is still on ya

This is great and all (and fun to watch πŸ’πŸΌβ€β™‚οΈ).

But code review awaits you on the other side of all those subagents 😬

But wait! Also here you may consider another huge mental shift:

Manual coding required you to gradually build the app, AI coding instead is more like a book: write the full draft, and then refine πŸ“’

In my case: let the AI(s) implement the full migration, and then review and refine it (remove and refactor).

Note: not all tasks benefit from this constraint. "Loops" are just another tool, not required πŸ’πŸΌβ€β™‚οΈ


Important caveat (trap): AI is making easier to build all the projects that you didn't have time to do before.

But, if those were not priorities, probably it meant that they were not that important. Don't prompt AI all the time just for the sake of it πŸ’πŸΌβ€β™‚οΈ

See you next πŸ‘‹

Start here.

Every week I dive headfirst into a topic, uncovering every hidden nook and shadow, to deliver you the most interesting insights

Not convinced? Well, let me tell you more about it