Spec-Driven Multi-Agent AI Programming: Delivering a Cross-Border Insurance Product from Scratch

    As AI-assisted programming evolves, relying on a single large language model for end-to-end delivery reveals structural flaws, including limited capabilities, lack of process constraints, and inadequate code review. This article explores a methodology combining Spec-Driven Development (SDD) with multi-agent collaboration to address these challenges. Using a cross-border insurance product as a practical case study, it details how to define business specifications, orchestrate multiple AI agents for development and review, and maintain engineering quality through strict workflow constraints and explicit knowledge archiving.

    I. Background and Problem Definition: Why Single-Model AI Programming Is Not Enough

    AI-assisted programming has evolved from “code completion” to the “end-to-end delivery” stage. However, in real-world projects, workflows driven by a single model still suffer from three structural defects:

    1. Single Capability Boundary: Different models have varying strengths in long-context understanding, architectural reasoning, code precision, and multi-language or multimodal processing. A single model struggles to maintain high-quality output across all stages.
    2. Lack of Process Constraints: Without standard constraints, AI-generated code tends to “freestyle” — inconsistent naming, missed boundary conditions, and missing exception paths lead to high rework costs.
    3. Absence of Review: Allowing the same model to both write and review code is like “grading its own exam,” which significantly decreases the defect detection rate.

    This is especially critical in cross-border insurance scenarios with strict compliance and consistency requirements, where logic for currency conversion, policy versions, and regulatory reporting leaves no room for error. Therefore, we need an AI programming methodology that is spec-first, multi-model complementary, and workflow-closed. This is the core of our practice: SDD (Spec-Driven Development) + multi-agent collaboration.

    II. Core Concept of SDD: The Spec Is the Single Source of Truth

    The core philosophy of SDD is: Write the spec first, then write the code; code is the compiled product of the spec. In AI programming scenarios, its value is further amplified — the spec is both a precise expression of human intent and a “guardrail” constraining model behavior.

    A complete set of SDD engineering assets typically includes:

    1. /specs
    2. ├── requirements.md # Business requirements: user stories, acceptance criteria
    3. ├── design.md # Technical design: architecture, data models, API contracts
    4. ├── tasks.md # Task decomposition: independently deliverable work items
    5. /rules
    6. ├── coding-style.md # Code style and naming conventions
    7. ├── architecture.md # Layered architecture and dependency rules
    8. └── testing.md # Test strategies and coverage requirements
    9. /memory
    10. └── decisions.md # Architecture Decision Records (ADR)

    There are three key practices in implementation:

    • Acceptance criteria must be machine-verifiable. For example, in the cross-border insurance product, “premium calculation supports multiple currencies” should be written as “input USD coverage and daily exchange rate, output CNY premium, with an error not exceeding one cent.”
    • Task granularity should be controlled to be completable in a single session. Each task should be executed end-to-end (including tests) within a single model interaction to avoid context drift.
    • Continuous accumulation of decision records. Both models and humans need “memory.” Write key trade-offs into the ADR so that subsequent tasks reference the spec rather than relying on verbal communication.

    III. Multi-Agent Collaboration System: Division of Labor, Complementarity, and Mechanism

    We use three types of intelligent programming tools working collaboratively, divided by their respective capabilities:

    3.1 Capability Division Model

    • Main Developer Agent (Type A): Excels in long-context task execution and tool invocation. Responsible for scaffolding, business code implementation, refactoring, and large-scale file operations. Its advantage lies in autonomously reading spec documents, traversing the codebase, running tests, and iterating fixes.
    • Deep Reasoning Agent (Type B): Excels in logical reasoning and solution evaluation. Undertakes architecture design, algorithm selection, and concurrency and boundary condition analysis, outputting design documents for Type A to execute.
    • Multimodal and Broad Retrieval Agent (Type C): Excels in processing images, long documents, and cross-source information integration. Responsible for parsing requirement documents, converting UI drafts into page structures, and summarizing industry compliance materials.

    3.2 Collaboration Mechanism: Complete Workflow of a Task

    A typical task follows a four-stage flow: “Plan — Implement — Review — Archive”:

    1. Pick up task from specs/tasks.md
    2. │
    3. ▼
    4. [Type B] Reason and generate implementation plan (including boundary condition checklist)
    5. │
    6. ▼
    7. [Type A] Implement code per plan + specs, run tests until passed
    8. │
    9. ▼
    10. [Type B or C] Independently review diff: verify item by item against acceptance criteria
    11. │
    12. ▼
    13. [Type A] Fix review comments → regression testing
    14. │
    15. ▼
    16. Update tasks.md status, append decision records → Archive

    The most critical design principle here is separating the generator from the reviewer: the model that writes the code does not review its own code. Another model performs independent verification based on the same spec. In practice, this mechanism significantly reduces the miss rate for boundary condition defects because the “blind spots” of different models do not overlap.

    3.3 How Spec Constraints Are Injected

    Specs do not take effect automatically just by sitting in a repository; they must be injected into the model’s context through a three-layer mechanism:

    1. Entry Injection: Project-level rule files are automatically loaded at the start of each session to constrain code style, directory structure, and prohibited actions.
    2. Task-Level Reference: The description of each task explicitly references the corresponding spec section, such as “Implement premium calculation module, must satisfy AC-003 in requirements.md.”
    3. Review Checklist: The review prompt embeds fixed check items — whether acceptance criteria are covered, whether exception handling exists, whether layered dependencies are violated, and whether tests are sufficient.

    IV. End-to-End Practice: Delivering a Cross-Border Insurance Product from Scratch

    Taking the cross-border insurance product we delivered as an example, the complete closed-loop is divided into five phases:

    Phase 1: Requirement Normalization. Transform the informal requirements provided by the business side into structured requirement documents using the multimodal agent. Break them down into five domains: “underwriting, risk assessment, premium calculation, multi-currency settlement, and electronic policy generation,” and write verifiable acceptance criteria for each domain.

    Phase 2: Architecture Design. The reasoning agent produces the technical design: front-end and back-end separation, domain service division, abstraction of external dependencies for the exchange rate service, and finite state machine modeling for policy status. The design explicitly states that “exchange rate acquisition must go through an anti-corruption layer; business code is prohibited from directly calling external interfaces.” This spec repeatedly prevented the misuse of unvalidated exchange rate data in later stages.

    Phase 3: Task Decomposition and Implementation. The design is broken down into about 40 independently deliverable tasks, sorted by dependency topology. The main developer agent implements them task by task. Each task follows a “TDD-style loop”: generate a failing test first, then write the implementation, and only mark it complete after all tests are green.

    Phase 4: Cross Review. Before each batch of tasks is merged, a model other than the generator performs an independent review, focusing on verifying multi-currency precision (using integer operations with the smallest currency unit uniformly), time zones and natural day boundaries, and illegal transitions in policy state flow.

    Phase 5: Archiving and Knowledge Accumulation. After tasks are completed, the task board is automatically updated. Key trade-offs made during the implementation are written into decision records, forming a traceable delivery archive and providing high-quality context for subsequent iterations.

    V. Lessons Learned and Pitfalls to Avoid

    1. Spec investment determines the output ceiling. We spent about 30% of our time on specs, but in return, we achieved an extremely low rework rate later. The more verifiable the specs are, the more stable the model output will be.
    2. Do not let models do tasks beyond their granularity. Overly large tasks will lead to context overflow and logic drift. It is better to split more than to be greedy for size.
    3. Reviewer independence is more important than model strength. Even if the reviewing model is slightly less capable, as long as it is independent and verifies item by item against the spec, it can catch most structural defects.
    4. Humans must guard key nodes. Architecture decisions, compliance rules, and external interface contracts must be manually confirmed. Models are responsible for execution, while humans are responsible for judgment.
    5. Knowledge must be explicitly archived. Session context is volatile; decision records and spec documents are the team’s long-term assets.

    Multi-agent collaboration + SDD is not a silver bullet, but it upgrades AI programming from “hit-or-miss code generation” to an “engineering workflow with specs, division of labor, and reviews.” For compliance-sensitive and logically complex business systems, this methodology is worth adopting as a baseline practice for team AI engineering.