If you're using Cursor, Claude Code, or similar tools to build a real product, you've hit this: AI agents write good code but don't maintain your architecture. After a few months of agent-assisted development on Center AI (a voice agent platform), our backend had a 3,742-line legacy route file, 174 try/catch blocks with six different error response formats, infrastructure we built but never adopted, and 753 console.log calls because that's what agents default to.
The agents did exactly what we asked. The problem was how we asked.
Plan Before You Implement. Always.
We started every refactoring session the same way. Spawn 5 to 10 sub-agents to explore different parts of the codebase in parallel. One checks route structure. One checks error handling patterns. One counts console.log usage. One reads existing utilities. One looks at how modular files are already organized.
Then synthesize into a plan with exact file paths, line counts, and a proposed sequence. Review it. Only then move to implementation.
Practical tip: Build a reusable command that says: "Explore the codebase first. Spawn sub-agents to investigate different areas. Identify what already exists that can be reused. Do not write code yet. Produce a plan."
Meet America’s Newest $1B Unicorn
A US startup just hit a $1 billion private valuation, joining billion-dollar private companies like SpaceX, OpenAI, and ByteDance. Unlike those other unicorns, you can invest.
Over 40,000 people already have. So have industry giants like General Motors and POSCO.
Why all the interest? EnergyX’s patented tech can recover up to 3X more lithium than traditional methods. That's a big deal, as demand for lithium is expected to 5X current production levels by 2040. Today, they’re moving toward commercial production, tapping into 100,000+ acres of lithium deposits in Chile, a potential $1.1B annual revenue opportunity at projected market prices.
Right now, you can invest at this pivotal growth stage for $11/share. But only through February 26. Become an early-stage EnergyX shareholder before the deadline.
This is a paid advertisement for EnergyX Regulation A offering. Please read the offering circular at invest.energyx.com. Under Regulation A, a company may change its share price by up to 20% without requalifying the offering with the Securities and Exchange Commission.
Give Agents Explicit Patterns to Follow
When we asked an agent to "standardize error handling," it created a brand new error utility from scratch. We already had AppError, asyncHandler, and a global error middleware. All built, tested, sitting in /lib/.
The fix: point the agent at the existing pattern. Say "read routes/agents/crud.ts and match that exact pattern." Give it a before/after example.
We migrated 45 route handlers across 7 files in one session with this instruction: "Wrap handlers in asyncHandler. Replace res.status(4xx).json({ error }) with throw badRequest/notFound('ERROR_CODE', 'message'). Import from lib/errors.ts. Follow the same pattern as routes/users.ts."
Every file came out consistent. -2,031 lines removed.
Practical tip: Keep one "reference file" per pattern in your codebase. When instructing agents, always point them at it. Don't describe the pattern in words. Show them the file.
Verify Agent Analysis Before Acting
An agent analyzed our folder structure and concluded that aibpo-backend/src/ was the "outdated copy" and root /src/ was the "real code." It recommended deleting the backend folder. It was the opposite. The root was an accidental copy. The backend repo had 599 commits of real history.
In another session, an agent reported 5 endpoints "missing null guards." We checked. All 5 already had null checks. The grep was wrong.
Practical tip: When an agent says "I found X issues in Y files," verify at least 2 to 3 manually before batch-applying fixes. Agents are confident even when they're wrong.
Building a platform like this and want help with the architecture? I work with teams shipping agent and voice agent products. If you're navigating this kind of complexity, reach out: [email protected]
Sequence Refactoring Like a Pipeline
We broke backend hardening into five phases, each deployable independently:
Type safety. Enable strict TypeScript. Catches problems in every subsequent phase.
Dead code removal. Delete unused files. Shrinks the surface area.
Error handling. Adopt existing utilities across all routes.
Structured logging. Replace console.log with logger. Mechanical.
File splitting. Break large files into modules. Highest risk, do last.
The order matters. Each phase made the next one easier. Tell the agent which phase you're in. Don't let it jump ahead.
Practical tip: Put your phase plan in the system prompt or a pinned file. At the start of each session, tell the agent: "We are in Phase 3. The goal is X. Do not do Y."
Clean Up After Every Agent Session
AI agents leave fingerprints. 15-line JSDoc comments on 3-line functions. "SECURITY NOTICE" and "CHANGES MADE" blocks that read like a changelog. Nested try/catch where Prisma's deleteMany already handles zero matches. Comments like // Get JWT token from Authorization header above const authHeader = req.headers.authorization.
We ran a cleanup pass after every session with a reusable command: scan for extra comments inconsistent with the rest of the file, unnecessary try/catch blocks, as any casts, and style inconsistencies.
Practical tip: Build a "deslop" command you run after every agent session. The cleanup pass is as important as the implementation pass.
Test Against Production, Not Just the Build
Build passing means nothing if endpoints don't work. After every phase: compile check, deploy to Heroku, hit live endpoints with curl, run browser automation against the deployed frontend.
We caught a real issue this way. The agent enabled noImplicitAny: true, local build seemed fine, Heroku build failed with 45 type errors. Had to revert, fix the actual issues, and redeploy. Happened twice before it stuck.
Practical tip: Don't trust "build succeeded" from the agent. Deploy and verify.
Results
All phases done with AI coding agents. All while the platform stayed live and we shipped features in parallel. 15,000+ lines removed. Strict TypeScript across the board. One consistent error format. Zero console.log in production code. No file over 500 lines.
The agents are the tool. You're the architect.
I help teams build and harden agent platforms, from voice AI to chat to Shopify integrations. If you're building something similar and want to move faster without the architectural debt, let's talk: [email protected]


