Spec-Driven Development: A Practical Guide from Requirements to Delivery
This article explains how Spec-Driven Development (SDD) turns executable specifications into the primary input for software delivery, with code treated as one implementation of the spec. It covers a four-layer spec structure (goal, capability, contract, and acceptance), the discipline needed to keep specs and code in sync, how to feed context to AI coding tools in small, constrained slices, and how to enforce quality gates through static, unit, and integration checks. It also outlines change control, common anti-patterns, and a four-step adoption path for teams working on long-cycle, multi-role projects.
1. Why Spec-Driven Development
In long-cycle projects, the most common cause of failure is not a lack of coding ability. It is that requirements are repeatedly “re-interpreted” as they move between people. A one-sentence requirement from a product manager often changes shape after it passes through design, frontend, backend, and QA.
Spec-Driven Development (SDD) takes a clear position: make an executable specification the primary input for development, and treat code as just one implementation artifact of that specification.
It addresses three specific pain points:
- Requirement ambiguity: natural language is inherently fuzzy, so a spec forces boundary conditions to be written down.
- High rework cost: defects are cheaper the earlier they are found, and spec review is the lowest-cost point to intercept them.
- Context loss: in long-cycle development, both developers and AI coding tools need a stable context anchor.
The key point is that SDD is not about “writing more documents.” It is about turning documents into engineering assets that are binding and verifiable.
2. The Layered Structure of a Spec
In practice, the most common anti-pattern is turning the spec into one long blob of text. It should be split into at least four layers:
- Goal layer (Why): business objectives, success metrics, and an explicit non-goal scope.
- Capability layer (What): a capability list describing the behavior the system promises externally.
- Contract layer (How): interface semantics, data structures, state machines, and error code semantics.
- Acceptance layer (Verify): acceptance conditions that can be executed automatically.
A spec skeleton can be written as structured text so that it is easy for both tools to parse and humans to review:
spec:id: EMP-012goal: Digital worker completes ticket classification and reply draft generationnon_goals:- No multi-tenant billing- No voice channelcapabilities:- Receive ticket events and normalize them- Generate reply drafts with a confidence scorecontract:input: {ticket_id: string, text: string, locale: string}output: {draft: string, confidence: number, need_human: boolean}errors:- {code: INVALID_INPUT, retryable: false}- {code: UPSTREAM_TIMEOUT, retryable: true}acceptance:- given input missing locale, when called, then return INVALID_INPUT- given confidence below threshold, then need_human is true
The key design choice here is that acceptance conditions use a Given-When-Then structure, which makes them naturally translatable into test cases.
3. Discipline for Mapping Specs to Implementation
The point where SDD most easily breaks down is when the spec and the code drift apart. Three rules are needed to keep the spec binding:
- One-way traceability: every capability item must map to an implementation location and a test case ID, and the reverse must also hold.
- Contract first: freeze inputs, outputs, and error semantics before parallel development starts, so teams do not build on separate assumptions.
- Change means review: any contract change must be written back into the spec, or it is treated as a defect.
In long-cycle development, keep spec files under version control and commit them together with the code changes. This lets reviewers see the “change in intent” and the “change in implementation” at the same time, so context is not lost.
4. Working with AI Coding Tools
The most common mistake when using AI coding tools is handing the entire repository to the model at once and expecting it to “understand everything.” A more reliable approach is to feed context in slices:
- First provide the goal layer and the non-goal scope to constrain the model’s divergence.
- Then provide the contract layer and require it to generate implementation only within that contract.
- Finally provide the acceptance layer and let it generate tests and run verification on its own.
A practical constrained prompt structure looks like this:
Role: Senior backend developerInput: Use only the spec fragment below; do not introduce undeclared fieldsTask: Implement the handler and unit testsConstraint: Error codes must come from spec.contract.errorsOutput: Code + tests + list of uncovered items
The “list of uncovered items” is important. It exposes the model’s hallucinations as a checkable diff instead of hiding them inside the code. If you use Baidu AI Cloud’s AI coding and model services, the same approach is recommended: keep calls small in context, strong in constraints, and verifiable. This usually works better than a single large-context input.
5. Acceptance Automation and Quality Gates
If a spec cannot be verified automatically, it degrades into a wish list. Three levels of gates are recommended:
- Static gate: check whether contract fields and error codes match the spec, using a structural comparison script.
- Unit gate: every acceptance item maps to at least one test case, with coverage measured per capability item rather than by global line count.
- Integration gate: verify timeout, retry, and degradation paths in an environment close to real dependencies.
For capabilities that involve external dependencies, the spec must explicitly declare failure semantics. For example, whether an upstream timeout is retryable, the maximum number of retries, and what a degraded response returns. If these are left blank, the implementation phase will inevitably produce different guesses from different people.
6. Change Control: The Real Difficulty of SDD
Requirement changes in the middle of a project are unavoidable. The value of SDD is not to forbid change, but to make the cost of change visible. The recommended process:
- Submit a change request that states the IDs of the affected spec items.
- Mark the impact surface: contract, data, tests, and downstream dependencies.
- After review approval, write the spec back once, then change the code.
- Keep the history of deprecated items so that newcomers do not re-litigate them.
A common mistake is “change the code first and backfill the spec later.” In long-cycle projects, this almost inevitably leads to a distorted spec, which eventually loses its status as the single source of truth.
7. Common Anti-Patterns and How to Avoid Them
- Spec bloat: a single file of thousands of lines that nobody reviews. Split it by capability domain.
- Inconsistent dual-writing of spec and implementation: constrain it with the same commit and automated comparison.
- Writing only the happy path: exceptions and boundaries are the main source of defects.
- Treating the spec as a deliverable rather than an engineering asset: it must be executable and verifiable.
- Over-engineering up front: writing all details at once actually slows down responsiveness, so allow progressive refinement.
8. A Suggested Adoption Path
If a team is ready to try, advance in four steps to avoid rebuilding the process all at once:
- Step 1: Pick a capability with medium complexity and clear boundaries as a pilot.
- Step 2: Establish a spec template and an acceptance mapping standard, and get one chain working end to end.
- Step 3: Add static and unit gates, and quantify the rework rate and the share of defects caught early.
- Step 4: Expand to multi-team collaboration, using spec IDs as a shared language for cross-team communication.
Conclusion
The essence of SDD is turning “understanding” into a checkable engineering object. Its payoff is not more documents, but less rework, no lost context, and lower cross-role communication cost. In projects that are long-cycle, multi-role, and now include AI coding tools, this kind of certainty is often more valuable than raw coding speed.
