From Vibe Coding to Spec-Driven: A Practical Engineering Workflow for AI Coding
I. Introduction: When “Writing Code” Becomes Cheap
Generating hundreds of lines of code has gone from taking a full day to taking a few minutes; refactoring a module now takes tens of seconds; generating a suite of test cases takes a minute or two. AI coding tools are fundamentally changing the cost structure of software delivery.
But the cost of generating code approaching zero does not mean the cost of delivering good code approaches zero. A common predicament is this: AI can write code that “runs,” but not necessarily code that is “right”; a developer stares at a working system yet cannot explain why it works, where its boundaries are, or how they would notice if the next change broke it. Simon Willison calls this phenomenon cognitive debt — technical debt is when the code quality is poor; cognitive debt is when you yourself don’t understand it.
What this article discusses is how, under the new premise that “writing code is cheap,” to use a structured workflow to push AI’s output from “a demo that runs” to “a deliverable engineering artifact.” The core idea can be summed up in one sentence: constrain intent with a spec, lock in correctness with tests, and close the loop so that AI can fix itself.
II. First, Distinguish Two Modes: Vibe Coding vs. Spec Coding
Before laying out the workflow, it is worth distinguishing two fundamentally different ways of using AI coding.
2.1 Vibe Coding: Give Up Line-by-Line Control in Exchange for Speed
The defining characteristic of Vibe Coding is giving up line-by-line understanding and control of the code: describe the intent in natural language, let the large model generate a runnable version, and the human is only responsible for judging “is it right, is it usable.” Its loop is extremely short: describe the product → generate a version → run and experience it → keep chatting with feedback. For prototype validation, one-off scripts, and exploratory projects, this approach is highly efficient.
2.2 Spec Coding: Define the Contract First, Then Write Code
Spec Coding, by contrast, requires that before issuing any coding instruction, you first spell out “what to do, how to do it, and what the constraints are” in a structured document, and AI codes around that spec. Its three iron rules are: no spec, no code; when the spec and the code conflict, the code is always the one that is wrong; when a bug is found, fix the spec first, then fix the code.
2.3 Layered Strategy: Choose the Mode by Complexity
The two are not opposites. The sensible strategy is to layer by complexity: core business, cross-module changes, and features with long-term maintenance expectations go through the spec workflow; changing a field, fixing an obvious bug, or writing a one-off script does not need to bear the full cost of a specification. The workflow described below is mainly aimed at the former category.
III. The Core Workflow: A Six-Stage Pipeline with Gates
A production-ready AI coding workflow can be broken down into six stages, with human gates (gatekeepers) set at the key checkpoints. This structure has similar implementations in both the Kodro framework and the AI Engineering Framework.
┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐
│ Clarify │──▶│ Specify │──▶│ Plan │──▶│Implement│──▶│ Validate│──▶│ Deliver │
└────┬────┘ └────┬────┘ └─────────┘ └─────────┘ └────┬────┘ └─────────┘
│ │ │
▼ ▼ ▼
┌─────────┐ ┌─────────┐ ┌─────────┐
│ Gate 1 │ │ Gate 2 │ │ Self- │
│ Human │ │ Human │ │ heal │
│ Confirm │ │ Review │ │ ≤ 5× │
└─────────┘ └─────────┘ └─────────┘3.1 Stage One: Clarify Intent
Do not have AI write code straight away. The first thing to do is have it ask you questions. An effective approach is to ask AI to raise 3 to 5 clarifying questions covering: who the target users are, what the first version must include, what the explicit non-goals are, what the tech-stack constraints are, and whether there are existing modules that must be reused.
The value of this step lies in forcing vague requirements down to an executable granularity. Compare “build a login page” with “build a responsive login page in React + Tailwind, with email-format validation and a remember-me option, wired to Firebase Auth” — the latter is input that AI can produce reliably from.
Gate 1: Once clarification is complete, a human confirms the intent has been understood correctly before moving on to the spec stage.
3.2 Stage Two: Generate the Spec
The spec document should contain at least: a description of functional behavior (ideally BDD-style scenarios), explicit acceptance criteria, boundary conditions and error-handling conventions, and the interface contracts with existing systems. The Kodro framework uses a “Document Quality Index (DQI)” to quantify how complete a spec is, requiring at least 80% before the workflow is allowed to proceed to the next stage.
The form of the spec does not need to be heavy. A single spec.md file plus a handful of Given-When-Then scenarios is enough to give AI’s subsequent coding something to stand on. The key principle is: the spec is the source of truth. If the behavior later turns out to be wrong, change the spec first, then have AI change the code — not the other way around.
Gate 2: A human reviews the spec. Once the behavior definitions, boundary conditions, and non-functional constraints are confirmed to be clear, it is approved.
spec.md
├── Functional behavior (BDD scenarios)
│ ├── Given: precondition
│ ├── When: triggering action
│ └── Then: expected result
├── Acceptance criteria
├── Boundary conditions & error handling
└── Interface contracts (with existing systems)3.3 Stage Three: Break Down the Plan
AI generates a task list from the spec and annotates the dependencies between tasks. The output of this step is a directed graph or an ordered checklist: what to do first, what to do next, which tasks can run in parallel, and what the completion criterion is for each task.
The core principle of task breakdown is independently verifiable. “Implement a user authentication system” is not a good task; “Implement the user registration endpoint, including email verification and password hashing, such that the unit tests for that endpoint pass when it is done” is.
3.4 Stage Four: Implement
The first three stages wrote not a single line of code. Only at this step does AI begin generating the implementation under the constraints of the spec and the plan. Because the input quality is already high enough, the number of conversation rounds can be compressed from the twenty-odd rounds typical of Vibe Coding to three to five rounds, and the total token cost is actually lower.
A practice that is easily overlooked: keep the implementation session and the testing session separate. Let one AI session write the implementation and another session write tests from the spec — this avoids the bias of “setting the questions and answering them yourself.”
3.5 Stage Five: Validation and Self-Healing
Finishing the code is only the beginning. The real quality checkpoint is here.
Ideally, AI should be able to run the tests itself, read the failure logs, locate the problem, modify the code, and re-run — until the tests pass. This is the core of the “AI self-service loop”: give AI the ability to get immediate feedback, so that it goes from “write the code and wait for someone to test it” to “write the code and test it itself until it is right.”
Kodro allows AI to self-repair up to 5 times during the validation stage; only if it still fails is the problem escalated to a human. In AstrBot’s practice case, by building a CLI end-to-end testing platform, AI can work autonomously for more than 35 minutes, continuously producing plugins that pass all tests.
3.6 Stage Six: Delivery and Accumulation
The deliverable should not be just code. It should include at least: run instructions, a snapshot of the spec for this change, test coverage, and any “implicit rules” or pitfalls discovered during development. Depositing these into the project’s knowledge files (such as CLAUDE.md or a knowledge/ directory) raises the starting point for the next AI session.
Deliverables
├── Code implementation
├── Run instructions
├── Spec snapshot (this change)
├── Test coverage report
└── Knowledge deposits
├── Implicit rules
└── Pitfall log
│
▼
Write to CLAUDE.md / knowledge/
│
▼
A higher starting point for the next AI sessionIV. Three Key Mechanisms That Make the Workflow Actually Work
Workflow frameworks are easy to draw; the hard part is keeping them from degenerating into empty ritual when implemented. The following three mechanisms are what separate a “real process” from “going through the motions.”
4.1 Mechanism One: Red-Green TDD as AI’s Enforced Discipline
The biggest risk with AI is not that it cannot write code, but that it writes code that “runs but is not right,” or code that “is written but never called.” The reason test-driven development is especially effective for AI is that tests force AI to first define what “correct” means, and then implement correctness.
┌──────────────────────────────────────────────────────┐
│ │
│ ┌────────────────────┐ │
│ │ ① Write tests │ │
│ └──────────┬─────────┘ │
│ │ │
│ ▼ │
│ ┌────────────────────┐ │
│ │ ② Run → fail │ 🔴 Red │
│ └──────────┬─────────┘ │
│ │ │
│ ▼ │
│ ┌────────────────────┐ │
│ │ ③ Confirm failure │ │
│ │ cause is expected │ │
│ └──────────┬─────────┘ │
│ │ │
│ ▼ │
│ ┌────────────────────┐ │
│ │ ④ Write code │ │
│ └──────────┬─────────┘ │
│ │ │
│ ▼ │
│ ┌────────────────────┐ │
│ │ ⑤ Run → pass │ 🟢 Green │
│ └──────────┬─────────┘ │
│ │ │
│ └──────────▶ Back to ① (next cycle) │
│ │
│ Core: tests force AI to define "correct" first, │
│ then implement "correct". │
└──────────────────────────────────────────────────────┘In practice, condense the whole discipline into the four words “red-green TDD” as the prompt: write the test first (running it must fail — red), confirm the failure reason matches expectation, then write the implementation until the test passes (green). The Alibaba Cloud community’s practice goes one step further and requires that the first sentence of every new session be “run the tests first” — this sentence tells AI that the project has tests, the number of tests hints at the project’s scale, and it puts AI into a testing mindset.
4.2 Mechanism Two: An End-to-End Testing Platform Fills the “Business Correctness” Gap
Unit tests can protect the logic, but they cannot protect the business scenario. A function returning the right value does not mean the user sees the right thing after clicking the button.
To address this, it is essential to build AI an E2E testing environment that it can drive directly from the command line. Take bot-framework development as an example: the traditional approach is to deploy to the platform, send a message by hand, check whether the reply is right, copy the logs to AI, have AI make changes, and redeploy — an extremely slow loop that depends heavily on humans. A CLI mock testing platform, by contrast, lets AI directly “send a message, receive a reply, check the content, modify the code, and send again,” completing the entire loop inside the terminal.
For web applications, the equivalent is to give AI a toolchain that can run pages headlessly, click elements, and compare screenshots. The key point is: AI must be able to see for itself “what the user will see”, not just the true/false in the logs.
4.3 Mechanism Three: Non-Degrading Refactoring Driven by a Predefined Checklist
Once a feature works, the code may still be hard to read, contain security holes, or have muddled layering. Letting AI do the refactoring carries the risk that, under the banner of “optimization,” it quietly breaks the logic.
The solution is two layers of insurance. The first layer is the test safety net: existing unit tests and E2E tests must all pass, and any refactoring that breaks behavior is rejected immediately. The second layer is a predefined checklist: list in the project’s config file the dimensions AI should review item by item — deviations from SOLID, missing defensive programming, whether error handling is precise, whether there are hardcoded credentials, and so on. When AI reviews code against a concrete set of questions, it can identify real problems without mistaking normal code for problems; after several rounds, the code approaches “smoothness” along these dimensions.
┌───────────────────────────────────────────┐
│ AI Refactoring Request │
└─────────────────────┬─────────────────────┘
│
▼
┌───────────────────────┐
│ Layer 1: Test Net │
│ Unit + E2E all pass? │
└───────────┬───────────┘
│
┌───────┬───────────┐
▼ ▼
┌───────┐ ┌───────────┐
│ Pass │ │ Fail │
└───┬───┘ └─────┬─────┘
│ │
▼ ▼
┌───────────────────┐ ┌───────────────┐
│ Layer 2: Checklist│ │ Reject at once│
│ · SOLID drift │ └───────────────┘
│ · Defensive prog. │
│ · Error precision │
│ · Hardcoded creds │
└─────────┬─────────┘
│
▼
┌─────────────────┐
│ Refactor & merge│
└─────────────────┘V. Anti-Patterns and a Pitfall-Avoidance Checklist
5.1 Anti-Pattern One: Describing Code Location in Natural Language
“Add a check in that function” is vague to AI. The correct way is to give the file path, the function name, and the exact condition. “In the createOrder function of src/services/order.ts, add an amount-cap check: if amount > 100, throw AppError('amount exceeds limit').”
5.2 Anti-Pattern Two: One Session Doing Everything
Implement user registration, then implement articles, then fix bugs, then write documentation — the context gets polluted quickly. Start a new session when the topic changes, and give each sub-task its own conversation.
5.3 Anti-Pattern Three: Accepting Evidence-Free “Should Be Fine”
When AI says “this change should work,” demand verifiable evidence: test output, compilation results, an actual invocation screenshot. The core of the Verification iron rule is to prohibit evidence-free claims.
5.4 Anti-Pattern Four: Skipping the Spec and Letting AI “Figure It Out”
For requirements above five person-days of complexity, the price of skipping the spec is usually not saved time — it comes back later as more rework rounds.
Cost
│
│ ╱ Rework cost
│ ╱
│ ╱
│ ╱
│ ╱
│ ╱
│ ╱
│ ╱
│ ╱
│────────────╱────────────────────▶ Time
│ ▲
│ │
│ Time saved by
│ skipping the spec
│ (quickly eaten by rework)VI. Conclusion
Engineering AI coding is, in essence, about resolving a contradiction: the cost of generating code is falling steeply, but the cost of verifying that code is correct is not falling in proportion. If verification capability cannot keep up with generation capability, the more you produce, the heavier the cognitive debt, and the less controllable the system.
The workflow described above — Clarify, Specify, Plan, Implement, Validate, Deliver — together with TDD discipline, the end-to-end feedback loop, and non-degrading refactoring, all exist to embed verification inside AI’s working loop, so that the question “is it right?” has an answer at every step, rather than leaving the entire judgment burden to humans at the end.
Tools will keep evolving and models will keep getting stronger, but the value of this structure will not fade: clear expression of intent, an executable definition of correctness, and the ability for AI to discover its own mistakes are scarce under any generation of tools.