JAFSJosé Antonio Fuentes Santiago

The Good, the Bad and the Ugly: How to Get Three Agents Fighting So They Write Your Code for You

2026-08-02 · [ leer en español ]

Cartoon of the three agents at their desks: the Good writes the code, the Ugly reviews it and the Bad tries to break it (labels in Spanish)

I remember the afternoons at my parents' house when there was always a western on. Not a day went by without John Wayne strutting across the screen like he owned the place. Truth is, I was never much of a fan. But what if I told you that AI lets us stage a western of our own every time we build software?

On paper the idea is simple: three agents, each with a clearly defined role.

But before the opening credits finish rolling (those movies ran the credits at the start, not at the end like now), let's go back over where we came from, origin-of-species style.

Previously on...

In the previous article you saw how to write a spec as well as it can be written (José Luis, I know it's not the only way, I can hear you complaining from here). The lesson was that without a good spec you don't get very far, because AI isn't deterministic.

But let's be honest. A flawless spec is very reassuring, sure, but it's still the law of the town written on a piece of paper that somebody else has to interpret. And out in the Wild West of software, the law is worthless if nobody's around to play judge.

Until now, coding with AI has mostly been a sad little dialogue for one: you ask the model for something and it hands you back some nice, shiny code. At that point you have two options:

Look the other way
Meaning you trust it, you commit, and you let it ride into production like a fugitive slipping past the sheriff. Three minutes later the server is up in flames and the only thing moving in the office is a tumbleweed. AI likes to please. Ask it whether its own code is any good and it'll tell you it's a work of art that belongs in a museum, even if there's a null in there that can lock every user out of the login.

Review the code
The time AI saved you goes into reading every if it wrote, line by line. Whatever edge AI gave us, we lost by not trusting it. Not trusting it is the right call, mind you, but going over everything in detail isn't the point. You can also review the code at a high level, which always helps and costs you a lot less time, but then you're in for several rounds before the code is final.

Turns out there's a third way: stop riding solo and put together a posse of agents, so you only review the code once if you like and don't burn all that time.

The cast

The idea is simple. You stop trusting a single model to be architect, programmer and tester all at once. Instead you split the work among three clashing personalities who cordially hate each other but have to get along under the watchful eye of the spec. Here's the cast:

  • 🤠 The Good: The eager agent who takes the spec with a smile, designs the solution and writes the code, all with a piece of straw between his teeth and a promise that he's following best practices.
  • 👺 The Ugly: The sour reviewer. Your archetypal Senior Code Reviewer, brow furrowed and mouth twisted, who isn't impressed by pretty patterns and will go through every line looking for the spot where you strayed from the original document.
  • 🌵 The Bad: A ruthless saboteur, dressed in black for some reason, whose only purpose in life is to raise hell in Code City with absurd payloads, empty strings and concurrent requests until a 500 goes off like a stick of dynamite.

Duel at sundown

And where are you in this lineup? You're the sheriff. Your job is to see that the law is upheld. But thanks to your deputies, all you have to do is skim their reports from your office.

We could call the whole thing the Good, the Bad and the Ugly pattern. GBU for short.

Handing out roles at the saloon: a prompt for each agent

To keep this trio from turning into a circus of hallucinations, each agent needs a prompt that spells things out, with clear limits and a personality that's actually constructive.

🤠 The Good (the optimistic developer)

You've already met him: he's the agent in charge of the implementation, the sheriff's first deputy. He reads the spec you wrote in the previous phase and gets to work. He's constructive, he knows his design patterns and he always goes for the cleanest, most elegant solution.

  • Input: The functional/technical spec and the architecture contract.
  • Output: Production code, basic unit tests and a detailed execution plan.

His prompt could look something like this:

"You are a Senior developer focused on Clean Architecture (swap in whatever applies, or better, point it at your own markdown docs) and best practices. Your goal is to take the provided spec and write the cleanest, most readable and most testable code possible. Produce a step-by-step implementation plan and the corresponding code. Do not assume requirements that are not in the spec. If anything is ambiguous, document the assumption you made, staying within the limits of the document."

👺 The Ugly (the grumpy reviewer)

The Ugly is the sheriff's second deputy, and he resents the Good for being the favorite. His one job in life is to compare what the Good built against what the original spec asked for. He couldn't care less whether the code is "pretty" or "modern": if a requirement is missing or a method signature doesn't match, he rejects the change without mercy.

  • Input: The original spec plus the code the Good wrote.
  • Output: Approved, or a Deviation Report (with the specific points the Good has to fix).

His prompt:

"You are a strict, uncompromising code auditor and spec reviewer. Your only mission is to verify that the provided code complies 100% with the attached spec. Look for inconsistencies, missing requirements, unnecessary coupling and violations of the architecture rules. Be methodical and critical. If you find the slightest deviation from the original document, reject the code and explain exactly which requirement was not met and how to fix it."

🌵 The Bad (the agent of chaos)

If the Ugly checks the theory, the Bad tests the practice. He's an agent trained to break things. His whole outlook is to throw a wrench in the works and be the Good's main adversary: he doesn't care whether the architecture is clean, he just wants to find out if a malformed JSON, a negative ID or a string of weird characters will blow up the mine.

  • Input: The code the Ugly approved plus the exposed interfaces and entry points.
  • Output: Adversarial integration tests, edge cases, or a bug report with the stack traces from the wreckage.

And the prompt for this wicked creature:

"You are an adversarial QA engineer specializing in stress and security testing. Your only goal is to find holes, overflows, validation failures or inconsistent states in the provided code. Design extreme test cases (null values, memory limits, injections, race conditions). If you manage to make the system return an unhandled error or an uncaught exception, write a detailed report with the exploit or payload you used to break it."

Showdown at sundown

The magic of this pattern has less to do with how smart each agent supposedly is than with how they deal with each other.

                          ┌──────────────────────────┐
                          │           Spec           │
                          └─────────────┬────────────┘
                                        │
                                        ▼
    ┌───────────────────────────────────────────────────────────────────────┐
    │                                                                       │
    │   1. 🤠 THE GOOD ──► Writes plan and code                             │
    │           ▲                                                           │
    │           │ (Rejected: bring it back to the spec)                     │
    │           │                                                           │
    │   2. 👺 THE UGLY ─► Audits against the spec                           │
    │           │                                                           │
    │           │ (Approved)                                                │
    │           ▼                                                           │
    │   3. 🌵 THE BAD ──► Stress tests and edge cases                       │
    │           │                                                           │
    │           └─────────── (Broken: send back a fix) ───────────────┐     │
    │                                                                 │     │
    └─────────────────────────────────────────────────────────────────┼─────┘
                                                                      │
                                   (Survives all 3 phases)            │
                                              │                       │
                                              ▼                       ▼
                                   ┌────────────────────┐   ┌───────────────────┐
                                   │  ⭐ SHERIFF (you)  │   │     Retry loop    │
                                   │  (Merge to main)   │   │ (Max. N attempts) │
                                   └────────────────────┘   └───────────────────┘

That gives us the three phases of the idea:

  • Build: The Good reads the spec and gets the code ready.
  • Audit: The Ugly reviews the code. If it strays from the spec, he bounces the task back to the Good with specific comments. The loop repeats until the Ugly, tired of looking for excuses, stamps it approved.
  • Ambush: Code in hand, the Bad holds up the stagecoach the code is riding in. If he manages to stop it and make off with the jewels, he tells the Good how he did it, just to laugh in his face.

The sheriff's signature: Once the code has survived the Ugly's scrutiny and the Bad's attack, it lands on your desk. All that's left for you is a strategic, high-level review and, if it holds up, pulling the trigger on the merge.

Keeping the infinite loop on a leash

Leave three AIs arguing unsupervised and they may end up in an infinite loop, with the Good and the Ugly trading grievances forever over the length of a method or the name of a variable (you know some models love to ramble like a grandpa at a bus stop). If you don't want your tokens going up in smoke, especially now that they're priced like gold nuggets, here are three rules you can follow:

  • A hard cap on retries: If the Good and the Ugly haven't agreed after 3 rounds, the system stops and escalates the decision to the sheriff (the human).
  • The Ugly outranks the Good on design: If the Good tries to argue why he picked one abstraction over another, the Ugly always wins as long as the spec backs him up. No philosophical debates.
  • The Bad has to bring a failing test: The Bad can't reject code by saying "this looks unsafe to me." He has to hand over an automated test or a reproducible script that triggers the failure. If the test passes, his objection is thrown out.

Wrapping up: the sheriff works less and gets twice as much done

You used to have one deputy who did the job but, being young, had more drive and optimism than sense, which left you apologizing to everyone in town who got hurt by his mistakes. Now you run a gang of rival agents whose feuding does wonders for the quality of the software that comes out.

With this setup, AI stops being that eager partner who says yes to everything with a smile while the cattle wander off down the river. With an auditor (the Ugly) and a wrecker (the Bad) on the payroll, you force the Good to keep himself in check against the one thing that can't afford to be ambiguous: the original spec.

So next time you're about to ask an LLM for a whole module, don't just fire off loose orders. Set up a proper western, give each agent a part, pour yourself something cold and let them beat each other senseless while you take care of what really matters: making sure the law of the business rules is upheld.

One last thing: if you'd like to see a hands-on example of how to set all this up, say so on LinkedIn or Instagram and we'll do it.

UPDATE

You asked, and here it is: in The Brains, the Good, the Bad and the Ugly the pattern gets a fourth agent who does the planning, and you'll see the full implementation with Claude Code, command by command.

THIS ONE IS STILL BEING WRITTEN

All of this is turning into a book: AI-Assisted Development in a Zombie Apocalypse, the third in the series. I'm still writing the Spanish edition, and the English one comes after it. If you want to hear when it ships, follow the author on Amazon: the shelter doesn't send email, but Amazon does.