- Luh Sprwhk
- Posts
- The New Cost of Clean Code
The New Cost of Clean Code
Architecting for AI: The Patterns That Optimize Costs vs. the Ones That Kill Your Budget
I was taking a break from building my side project, "Rubber Duck Tarot," and when I picked it back up, I got a surprise email from Claude. It said I'd already blown through my $10 budget. I was floored. This wasn’t a big-ticket project, and I hadn’t been working on it consistently, so how could the money be gone? At first, I thought my account had been hacked. How could a few hours of development—even when I was in the zone, coding like there was no tomorrow—have run up a bill that fast?
While his advice is often debated, Uncle Bob's insights on Clean Code are proving more relevant than ever. What I figured out is that in the era of AI-powered development, bad code directly translates to big costs. It’s no longer just about maintainability or developer sanity. Your code architecture decisions literally show up on your infrastructure bills.
Nobody wants to be the developer whose messy patterns are burning through the API budget.

Why Your AI Assistant Cares About Code Patterns
The key to working with AI on code is recognizing that it's surprisingly picky about patterns. Once I started tracking my API costs, I noticed something interesting: clean, explicit code patterns consume far fewer tokens than clever, abstract solutions.
This is because when an AI model encounters a complex abstraction, it has to hold much more context in its working memory to trace through all the indirection. Every layer of abstraction forces the model to maintain more relationships and dependencies, which directly translates to token consumption. Explicit patterns, on the other hand, can be parsed in a single pass without juggling state.
The Human-AI Disconnect: Template Method vs. Strategy
This is where the difference between human-friendly and AI-friendly patterns becomes clear. Humans are much better at understanding abstraction.
Take the Template Method pattern, for example. Humans excel at mentally tracing through abstract flows and filling in the concrete details as needed. This is the core of object-oriented design: we understand that a general "transport" class can be a car or a boat, just as a "bird" can be a duck or a parakeet. Our brains are wired for this kind of high-level pattern matching and contextual reasoning.
// The abstract base class that defines the algorithm's skeleton
class BurgerMaker {
// This is the template method. It orchestrates the process.
makeBurger() {
this.prepareBun();
this.cookPatty();
this.addCondiments();
this.serve();
}
// Common step with a default implementation
prepareBun() {
console.log("Toasting the bun.");
}
// Abstract methods to be implemented by subclasses
// These are the "template" details that vary
cookPatty() {
throw new Error("You must implement the 'cookPatty' method.");
}
addCondiments() {
throw new Error("You must implement the 'addCondiments' method.");
}
// Another common step
serve() {
console.log("Your burger is ready!");
}
}
// A concrete subclass for a specific type of burger
class VeggieBurgerMaker extends BurgerMaker {
cookPatty() {
console.log("Frying a plant-based veggie patty.");
}
addCondiments() {
console.log("Adding lettuce, tomato, and avocado.");
}
}
// Another concrete subclass
class ClassicBeefBurgerMaker extends BurgerMaker {
cookPatty() {
console.log("Grilling a 1/4 lb beef patty.");
}
addCondiments() {
console.log("Adding ketchup, mustard, and pickles.");
}
}
// Usage
const veggieBurger = new VeggieBurgerMaker();
console.log("--- Making a Veggie Burger ---");
veggieBurger.makeBurger();
console.log("\n--- Making a Classic Beef Burger ---");
const beefBurger = new ClassicBeefBurgerMaker();
beefBurger.makeBurger();
AI struggles with this abstraction. It has to work overtime to figure out what’s actually going on with all that abstract flow and inheritance. This is where tokens can get really expensive.
By contrast, AI absolutely loves the Strategy pattern. It can see exactly what’s happening, which behavior goes where, with no mental gymnastics required. Each strategy is an explicit, self-contained unit.
// A set of "strategy" objects, each handling a specific behavior.
// These are not related by inheritance.
class VeggiePattyStrategy {
cookPatty() {
console.log("Frying a plant-based veggie patty.");
}
}
class BeefPattyStrategy {
cookPatty() {
console.log("Grilling a 1/4 lb beef patty.");
}
}
class VeggieCondimentsStrategy {
addCondiments() {
console.log("Adding lettuce, tomato, and avocado.");
}
}
class ClassicCondimentsStrategy {
addCondiments() {
console.log("Adding ketchup, mustard, and pickles.");
}
}
// The "Context" class that uses the strategies
// It doesn't know the specific implementation, just that it has a method to call.
class Burger {
constructor(pattyStrategy, condimentsStrategy) {
this.pattyStrategy = pattyStrategy;
this.condimentsStrategy = condimentsStrategy;
}
prepare() {
console.log("Toasting the bun.");
this.pattyStrategy.cookPatty();
this.condimentsStrategy.addCondiments();
console.log("Your burger is ready!");
}
}
// Usage: We compose the burger with the specific strategies we want.
console.log("--- Creating a Veggie Burger ---");
const veggieBurger = new Burger(new VeggiePattyStrategy(), new VeggieCondimentsStrategy());
veggieBurger.prepare();
console.log("\n--- Creating a Classic Beef Burger ---");
const beefBurger = new Burger(new BeefPattyStrategy(), new ClassicCondimentsStrategy());
beefBurger.prepare();
Intentional Architecture for the AI Era
This isn't about dumbing down our code. It's about being intentional. Use AI-friendly patterns where you're collaborating with AI. Save the human-friendly abstract patterns for the places where human intuition truly shines.
The new rule of clean code isn't just about maintainability anymore—it's about keeping your AI assistance costs under control.
Do you need help optimizing your codebase for AI efficiency? I help teams refactor their architecture to reduce API costs and improve performance. Contact me today for a consultation.