The 15-Phase Spec Framework: How I Structure AI Prompts for Zero Code Drift

Unstructured prompting is why AI-built codebases quietly rot. Here is the phased, spec-driven framework — persistent context files, scoped prompt libraries, and automated visual verification — that I use to build complex software with zero code drift.

Written by Sandeep Mundra
Published on Aug 04, 2026 • 7 min read min read
A developer at a multi-monitor desk reviewing a written specification document next to a Claude Code terminal and a Playwright visual test comparison
In short
Structured, phased AI prompting anchored by persistent context files, a scoped prompt library, and automated visual regression testing is what separates AI-built software that survives production from AI-built software that quietly rots.
  • Persistent context: a CLAUDE.md-style file stops the model from re-guessing your architecture in every new session.
  • Phased prompting: splitting a build into discovery, architecture, data modeling, implementation, and verification phases catches drift before it compounds.
  • Visual verification: Playwright-driven screenshot diffing catches UI regressions unit tests never see.
  • Discipline over cleverness: the framework matters more than which model you happen to be running.

More than half of the AI-assisted codebases I have personally reviewed this year needed a full manual audit within ninety days of shipping, not because the model wrote bad syntax, but because nobody could explain why a given function existed. That is the part people miss when they talk about AI coding tools. The failure is never really about intelligence. It is about memory, and about the absence of a spec the model was ever asked to hold itself accountable to.

I build software with Claude Code and similar agentic tools most weeks now, on codebases that are not toy projects. And the pattern is consistent enough that I no longer think of it as a bug in the tools. It is a bug in how most of us prompt them. Ad hoc prompting, the one-shot "build me a login flow" style of request, works beautifully for a weekend project and fails predictably at scale. Unstructured prompting does not just risk bugs. It erodes the very thing that makes a codebase maintainable: a shared, recoverable understanding of intent.

The Myths That Keep AI-Built Software Fragile

Three assumptions keep teams stuck, and I have believed all three at different points, so this is not a lecture from above.

Does AI understand my whole codebase from one prompt?

No. A large language model reasons over whatever context you hand it in that session, not over the accumulated intent of your architecture. Without a persistent file it can read every time, it reconstructs your system from partial evidence, and partial evidence produces confident, plausible, wrong answers.

Myth: A Longer, More Detailed Prompt Produces Better Code

This is the myth I fell for longest. I used to write single mega-prompts running to two thousand words, covering the entire feature end to end, convinced that more specification in one shot meant more control. It produced the opposite. A model asked to do discovery, data modeling, and implementation simultaneously will quietly optimize for the part of the prompt it read last, and the earlier constraints get diluted. Scope, not length, is the variable that matters.

Myth: Testing After the Fact Catches Drift

That is true, and it is the reason most teams start there. Unit tests are real protection, and I am not arguing against them. But unit tests verify logic you already thought to test. They say nothing about a button that silently moved eight pixels, a modal that no longer traps focus, or a layout that broke on a viewport nobody checked. Drift in AI-generated frontends is frequently visual before it is functional, and a green test suite will not tell you that.

The Principles That Actually Prevent Code Drift

Once you accept that the myths above are the default failure mode, the fix is less about better prompting technique and more about building infrastructure around the prompting.

Persistent Context Files Are Non-Negotiable

Every serious project I run now starts with a CLAUDE.md file, or its equivalent, checked into the root of the repository next to the code it governs. It states the architecture in plain language, the naming conventions, the folders the model must never touch without asking, the libraries we have standardized on, and the mistakes we have already made and do not want repeated. It is read at the start of every session, automatically, so the model is never operating on vibes.

Early last year, on a logistics-dashboard rebuild for a client running six regional warehouses, I watched an engineer hand an agent a two-line prompt asking it to "add filtering to the shipments table." Without any persistent context, the agent invented a new state-management pattern that conflicted with the three already in use elsewhere in the app. We caught it in review, but it cost a day. The CLAUDE.md file we wrote afterward, listing the one approved pattern and explicitly forbidding new ones, has prevented that exact failure from recurring since.

Phased Prompt Libraries Replace Ad Hoc Requests

Instead of one prompt per feature, I keep a library of prompt templates scoped to a single phase of the software development lifecycle: discovery, requirements, architecture, data modeling, security review, implementation broken into small increments, and automated visual verification. Each phase has its own prompt, its own expected output format, and its own exit criteria before the next phase is allowed to start. The model never gets to skip from "what should this do" straight to "write the code," because the prompt library will not let it. The same discipline that behavior-driven development brought to human teams two decades ago is what a prompt library brings to an AI agent now: a shared, checkable definition of done before anyone writes a line.

The 15-Phase Sequence I Actually Run

Fifteen phases sounds bureaucratic for something as fluid as writing code with an AI assistant, and I understand the objection. It felt that way to me too, the first time a colleague suggested formalizing it. What changed my mind was watching how much time the structure saved compared to the time it cost. The sequence is not rigid in the sense of always running in the same order for every project, but it always covers the same ground:

  1. Discovery and problem framing, in plain language, with no code discussed yet
  2. Stakeholder and user requirements, written as testable statements
  3. System architecture and boundaries, including what is explicitly out of scope
  4. Data modeling and schema design, reviewed before a single table is created
  5. Security and access-control review, mapped against known risk categories from OWASP
  6. API and interface contracts, defined before implementation begins
  7. Phased implementation, in increments small enough to review in one sitting
  8. Automated visual verification using a Playwright-driven screenshot harness against a baseline
  9. Functional and regression testing against the requirements from phase two
  10. Documentation sync, so the CLAUDE.md file reflects what was actually built
  11. Human review and sign-off before merge, wired through the same CI pipeline as every other change
  12. Deployment gate, staged rather than direct to production
Where teams actually lose money. Skipping the automated visual verification phase to save an afternoon is the single most expensive shortcut I see, because the regressions it would have caught surface two or three releases later, tangled up with unrelated changes, and cost a week to isolate instead of an hour.
DimensionAd hoc promptingPhased spec-driven prompting
Context memoryReconstructed each session, inconsistentlyPersisted in a CLAUDE.md file, read every run
Regression detectionManual, after a bug reportAutomated visual diffing on every build
Review scopeWhole feature at onceSmall, single-phase increments
Security postureAddressed if someone remembersA dedicated, mandatory review phase

Why Spec-Driven Engineering Becomes Mandatory, Not Optional

Regular readers will know where I stand on tooling hype generally: skeptical by default, persuaded only by what survives contact with a real deadline. This framework survived. I have never been persuaded that raw model capability alone gets a team to production-grade software, and nothing in the last two years of running Claude Code against live client work has changed that view.

Across the four engagements where we ran this full sequence this year, merge-request review time dropped by roughly a third, and not one of them shipped a production incident traced back to a visual regression we had not already caught ourselves. That is not a controlled study. It is a pattern I trust because I watched every one of those four projects up close, and the one variable that changed each time was the discipline, not the underlying model.

A model that cannot tell you why a line of code exists cannot be trusted to change the line next to it.

Here is the position I will state plainly: unstructured, single-shot prompting into a production repository is going to look reckless within a couple of years, the way committing straight to a shared main branch without review already does. Teams that treat the prompt itself as a disposable, unversioned artifact are building on sand, even when the code that comes out looks clean. The technical debt just moves upstream, into a place harder to see than the codebase itself, which is the intent nobody wrote down.

Spec-driven prompting is not slower once a team internalizes it. It is slower for the first project and faster for every one after, because the context file and the prompt library are reusable assets, not one-off overhead. That compounding is the actual argument for treating this as standard practice rather than a nice-to-have process improvement.

If your team is still prompting AI tools the way most people typed search queries in 2010, the place to start is not a new model. Start a CLAUDE.md file this week, write down the five decisions you keep re-explaining to every new engineer and every new agent session, and put a visual regression check in front of your next merge. The tooling around this is moving fast; the discipline is the part that will still matter in five years.

Frequently asked questions

What is a CLAUDE.md file used for?

A CLAUDE.md file is a persistent, plain-language document stored at the root of a codebase that tells an AI coding assistant the architecture, conventions, and constraints of the project before it writes any code, so context does not have to be re-explained every session.

Why does AI-generated code drift from the original spec over time?

Code drift happens because each AI prompting session reconstructs its understanding of the codebase from limited context, and without a persistent record of prior decisions, small reinterpretations accumulate across sessions until the implementation no longer matches the original intent.

Can automated visual testing replace manual QA for AI-built interfaces?

No. Automated visual testing with tools like Playwright catches layout and rendering regressions that unit tests miss, but it complements manual QA rather than replacing it, since it cannot judge whether a feature solves the right problem.

Sandeep Mundra

About Sandeep Mundra