Sub topic 5
Capturing design decisions before code
This chapter assumes you have read the chapters on requirements and acceptance criteria. You should be comfortable with Git and basic automated testing. You should also have prompted a coding agent at least a few times.
Why a design document sits between requirements and tasks
Requirements say what the system must do. Tasks say what to build, one step at a time. Neither says how the pieces fit together, and that gap is where a coding agent makes decisions on its own.
Suppose a requirement says a reset link expires after 30 minutes. The agent still has to choose several things:
- where tokens are stored
- whether the token is random or signed
- which module owns expiry
- what the endpoint returns on failure
If you don't write these choices down, the agent makes them for you, and it may make them differently in each session. Task 2 might assume tokens live in memory while task 5 assumes a database table. Each task passes its own tests, but the system doesn't hold together.
A design document fixes those choices once, in a place the agent reads on every task. It also gives you a cheap checkpoint. Reading a one-page design takes minutes. Finding the same mistake after 20 files of generated code takes much longer.
What a design document contains
A design does not describe every line of code. It records the decisions that are expensive to reverse or that several tasks depend on.
| Section | What it records | Why the agent needs it |
|---|---|---|
| Overview | The approach in a few sentences | Gives every task the same goal |
| Components | Modules or services, each with one responsibility | Stops logic from spreading across files |
| Interfaces | Function signatures, endpoints, message shapes | Lets tasks be built and tested separately |
| Data model | Entities, fields, constraints | Prevents conflicting storage assumptions |
| Error handling | What fails, how, and what the caller sees | Makes failures consistent |
| Decisions | Choices made, alternatives rejected, and why | Stops the agent from reversing a choice |
| Test strategy | What is unit tested and what is integration tested | Links the design to verification |
You rarely need all seven sections. A change that adds one function needs only an interface and a decision or two. A new service needs most of them.
How the design fits the workflow
The design is derived from the requirements and constrains the tasks. Every design element should trace back to at least one requirement. Every task should trace to the design.
The dotted arrow matters. Sometimes implementation shows that the design was wrong, for example a library does not support what you assumed. The right response is to update the design and then continue. Do not let the code quietly diverge. Later chapters cover that drift in detail.
A worked example
Here is a design for the password reset feature from the requirements chapter. It is short on purpose. The fenced block holds the whole document. Inside it, code names use plain text, since fences cannot be nested.
# Design: Password reset
## Overview
Reset tokens are random, single-use, and stored server-side with an
issue time. A reset-request endpoint creates tokens. A redemption
function validates them. Neither touches password hashing, which
stays in the existing `accounts` module.
## Components
- `reset/tokens.py`: issues and redeems tokens. Owns expiry and
single-use rules. Has no knowledge of HTTP or email.
- `reset/api.py`: HTTP endpoint for requesting a reset. Calls
`tokens.issue` and the existing mailer.
- `accounts/passwords.py` (existing): sets a new password hash.
Not modified.
## Interfaces
- `issue(token: str, now: datetime) -> None`
- `redeem(token: str, now: datetime) -> None`
Raises `TokenError` if the token is unknown, expired, or used.
- `POST /password-reset` with body `{"email": "..."}`.
Always returns 202 with an empty body.
## Data model
Table `reset_tokens`: `token_hash` (primary key), `issued_at`,
`used` (boolean, default false). Only a hash of the token is stored.
## Error handling
- `TokenError` maps to HTTP 400 with a generic message.
- The request endpoint never reveals whether an email is registered.
- Mail delivery failures are logged and do not change the response.
## Decisions
1. Server-side token store rather than signed tokens (JWT).
Rejected signed tokens because single-use needs server state
anyway, so a signature adds complexity and no benefit.
2. Time is passed in as an argument, not read inside `tokens.py`.
This keeps expiry testable without patching the clock.
3. No rate limiting in this change. Tracked as a separate spec.
## Requirements coverage
- 1.1 (30-minute expiry): `tokens.redeem`, `issued_at`
- 1.2 (same response for unknown email): `reset/api.py`
- 1.3 (single use): `tokens.redeem`, `used` flagSeveral things in this document are worth copying.
- Each component has one job.
tokens.pyknows nothing about HTTP, so it can be tested alone. TheResetTokensclass in the earlier test example works because of this separation. - Existing code is named and marked "not modified." Agents often rewrite code that already works. Saying what to leave alone prevents that.
- Decision 2 shapes the tests. Passing
nowas an argument is why the test example can run without mocking the clock. - Decision 3 states a non-goal. An agent that sees no mention of rate limiting may add it. A stated exclusion keeps the change small.
- The coverage section links design to requirements. A reviewer can check each criterion against a named component.
Recording decisions and rejected alternatives
The Decisions section does the most work per line. A decision without its reasoning is easy to override. An agent may see the server-side store, judge that signed tokens are "more standard," and switch. If the document says why signed tokens were rejected, the agent has something concrete to respect, and a human can check whether the reason still holds.
A usable decision entry has three parts:
- The choice you made.
- The alternative you considered.
- The reason, ideally tied to a requirement or constraint.
Keep entries to a few sentences. If you cannot name an alternative, you may not have made a decision, and the item may not belong in the section.
You can also ask the agent to draft the design. Ask it to list the options and trade-offs for each open question, then choose yourself. Agents tend to pick the first workable option. A written comparison makes you pick on purpose.
Keeping the design at the right level
A design that is too vague gives the agent no constraint. A design that is too detailed becomes a second copy of the code, and it goes stale as soon as the code changes.
Use this test: would a different choice here break another task, a requirement, or an external contract? If so, write it down. If the choice only affects the inside of one function, leave it to the implementation.
- Write down: module boundaries, public signatures, data shapes, error contracts, security-relevant choices, and which library to use for a cross-cutting concern.
- Leave out: local variable names, internal helper functions, loop structure, and formatting. Project instruction files and linters cover style.
Diagrams help when the design involves order or ownership. A sequence diagram of the reset flow shows which component calls which. Only add one if it answers a question the prose does not.
Common errors
The design restates the requirements. If every line could be moved to the requirements file, nothing has been decided. A design should name modules, signatures, and data, which requirements avoid.
Decisions hide in prose. A choice buried in the third paragraph of an overview is easy to miss, for the reviewer and for the agent. Put choices in a Decisions list.
No link to requirements. Without a coverage section, a design may leave a criterion unimplemented, or add components that no requirement asks for. Both are easier to catch in a table than in code.
Interfaces left implicit. If two tasks must agree on a function signature and the design does not state it, each task invents its own. Write signatures and exceptions out, even in pseudo-notation.
Designing against code that does not exist. Check that the existing modules, tables, and libraries named in the design are real. Agents sometimes describe a plausible accounts.set_password that is actually called update_credentials. Open the file and confirm.
Approving without reading. The design is the cheapest place to catch a wrong approach, but only if someone reads it. Treat approval as a real gate. Check the decisions, the interfaces, and what is left out.
Checking your design before you approve it
Run through these questions before you move on to tasks:
- Can every requirement criterion be found in the coverage section?
- Does every component have one responsibility, stated in a single sentence?
- Are all interfaces between components written out with types and failure behavior?
- Does each decision name a rejected alternative and a reason?
- Does the document say what is out of scope?
- Have the existing files, functions, and tables it references been confirmed to exist?
If the answer to any of these is no, fix the design now. A design that passes these checks gives the task breakdown in the next chapter a stable base: each task can name a component and an interface, and a test can check it.
Exercise
A design says "Tokens are stored in the database" and nothing else about storage. Two tasks later, the agent builds a token store backed by an in-memory dictionary for one task and a SQL table for another. Name two specific additions to the design that would have prevented this, and say where in the document each belongs.
Show answer
First, add a Data model entry that names the table, its fields (for example token_hash, issued_at, used), and its constraints. This pins down the storage shape for every task. Second, add a Decisions entry that states the choice of a database-backed store, names the rejected alternative (an in-memory dictionary or signed tokens), and gives the reason (for example, tokens must survive restarts and be single-use across multiple processes). A Components entry stating that tokens.py is the only module that touches the store would also help, because it keeps the storage logic in one place.
Exercise
You are reviewing an agent-written design for a "export report as CSV" feature. It lists three components, but the coverage section says criterion 2 ("the export must exclude archived rows") is covered by "the export module." What is wrong, and what would you ask the agent to change?
Show answer
The coverage entry is too vague to check. "The export module" does not say which function or query enforces the exclusion, and the design never states a rule for archived rows. Ask the agent to name the component and interface that applies the filter (for example, a fetch_rows(include_archived=False) signature), to add the filter to the data model or interface section, and to record in Decisions whether it filters in the query or after fetching, with the reason. Each requirement should map to a named element that a task and a test can point to.