Skip to content
Back to posts

I Had a 5,000-Line Index File: The Lean Framework for Building with AI Without the Debt

Notes by SDG ·

I recently learned about the concept of "spaghetti code"—that tangled web of intertwined logic where everything depends on everything else, making changes nearly impossible. Intrigued, I went looking for it in my own applications.

What I found was eye-opening: several core route and index files had quietly ballooned to 3,000 to 5,000 lines of code.

To be fair, my app was running completely fine. The AI coding assistant wasn’t messing anything up, and I wasn't facing any active production bugs. But looking at those massive 5,000-line monoliths, a realization hit me: if I don't take care of this now, it’s going to become an absolute nightmare later on.

If you’re building solo or with a small team, AI feels like a superpower. You type a prompt, and a full-stack feature streams onto your screen in seconds. But that rapid velocity hides a subtle trap.

When AI makes generating code essentially free, writing code stops being your bottleneck. Preserving your sanity—and keeping your app maintainable six months from now—becomes the actual challenge.

Here’s how to keep shipping at breakneck speed without letting your AI assistant turn your codebase into a tangled web.

Why AI Defaulting to "Spaghetti Code" Isn't Your Fault

AI tools aren't system architects; they're path-of-least-resistance engines.

When you ask an AI to solve a specific problem, it looks for the absolute fastest way to give you a working snippet right now. It doesn't step back to consider where that logic actually belongs long-term.

Left to its own devices, an AI assistant will almost always:

  • Append another 150 lines to a file that’s already way too big.
  • Dump heavy business logic right inside an HTTP route handler.
  • Copy-paste duplicate utility logic instead of importing a shared helper.
  • Mutate database state without updating the local state or clearing the cache.

In a enterprise environment, massive platforms and automated build tools act as guardrails to prevent this. But when you’re building lean, you don't have time to build complex corporate infrastructure. Speed is your only real edge.

So how do you keep the AI under control without slowing down?

How to Refactor a 5,000-Line File Without Breaking Everything

When your core entry files hit thousands of lines, touching anything feels like trying to cut the wire on a live bomb. If you ask an AI agent to "refactor this file into clean modules" in one giant prompt, it will ruin your day. It will lose context mid-way through, forget subtle edge cases, or hallucinate missing variables.

I learned this the hard way recently when my index file hit that 5k mark. The only way out was using Replit Agent like a junior engineer who needs precise supervision:

Step 1: Tell it NOT to code yet

Prompt the agent strictly for an outline first:

"Analyze index.ts. Do not edit any files yet. Group the logic inside this file into domains, and propose a plan to extract ONLY the Authentication endpoints into a separate module without changing their behavior."

Step 2: Actually read the plan

Look over what the agent proposes. If it tries to refactor auth and billing and user settings all in one go, tell it no. Keep the scope tiny.

Step 3: Chop it down one piece at a time

Let it extract /routes/auth.ts. Run your app, verify your login flow works, and commit the code. Only then do you hand it the next piece—like /routes/billing.ts.

Taking ten minutes to review plans and slice a refactor into four small commits saves you four hours of fixing broken production code later.

Three Practical Rules for AI-First Development

To keep your files from ballooning into 5,000-line monsters again, you only need three lightweight habits.

1. Group by Feature, Not by Technical Layer

Forget traditional enterprise patterns that force you to split a single feature across five separate folders (/controllers, /services, /models, /views, /interfaces).

When you bounce between twenty micro-files, the AI likely will drop context and start guessing imports.

Instead, try Feature Folders (Vertical Slices):

src/

└── features/

  ├── billing/

  │  ├── api.ts

  │  ├── components.tsx

  │  └── types.ts

  └── auth/

    ├── api.ts

    └── components.tsx

Keeping everything a feature needs right next to each other gives the AI immediate, high-density context. Fewer broken imports, less guessing, cleaner code.

2. Make Your Types Do the Heavy Lifting

Don't rely on self-discipline or release checklists—you’re going to skip them when you're shipping late at night anyway. Let your compiler do the nagging for you.

When you connect your database schema directly to your API and UI using TypeScript and validation libraries like Zod.

The AI physically cannot write a bad database query or pass the wrong props without your editor throwing a bright red underline instantly.

3. Put Hard Boundaries in Your Rules File

Don't write architecture guidelines in a README file nobody reads. Put them directly into your editor's instructions (like a .cursorrules file or your system prompt) where the AI sees them every time you hit enter.

Keep it simple and direct:

  • Thin Routes: Route files should only accept the request, validate the data, and hand it off. No raw database queries inside route handlers.
  • The 300-Line Check: If adding a feature makes a file pass 300 lines, stop and ask me if we should spin up a new file first.
  • Cache Revalidation: Any function that changes database data must explicitly call our cache refresh function before returning.

The Takeaway

AI made writing code dirt cheap. But as a solo builder, your most valuable asset isn't lines of code—it's mental bandwidth.

You don't need complex design patterns or corporate engineering rules to build great products. You just need clear boundaries, strict typing, and the patience to review AI refactoring plans one bite at a time.

Prompting gets your project off the ground. Clean boundaries will keep it flying :)