Topic 2 of 5 · 5 min read
Writing a spec an agent can follow
Goal: Write a one-page spec with problem, scope, non-goals and testable acceptance criteria for a real feature.
A good agent spec fits on one page and has six parts: problem, scope, non-goals, constraints, examples and acceptance criteria that a test can check. Short and exact beats long and loose.
Check yourself · 1 of 3
Which part of a spec tells the agent what it must NOT touch?
Why it matters
The agent reads the whole spec on every run. Each part answers a question it would otherwise guess: why are we doing this, how far does it go, what must it leave alone, which rules bind it, what does it look like in practice, and how will we know it works.
The six parts
- Problem. Two or three sentences on who is blocked and why. This lets the agent choose sensibly when something unforeseen comes up.
- Scope. What is in. Name endpoints, screens, files or modules where you can.
- Non-goals. What is out, stated plainly. This is the cheapest defence against scope creep: agents love to add a refactor, a new dependency or a settings page.
- Constraints. Stack, existing patterns, performance, security and compatibility. Link to the files that show the pattern. Use MUST for a hard rule and SHOULD for a preference, as RFC 2119 defines them.
- Examples. Two or three concrete inputs and outputs, including one edge case and one error.
- Acceptance criteria. A numbered list where each line is true or false after the work. Given / When / Then works well because each line maps to one test.
Before and after
Weak:
Add rate limiting to the login endpoint so it is more secure.
"More secure" cannot be tested, and the agent picks the limit, the key, the storage and the response.
Better:
## Problem
Credential-stuffing bots hit POST /login about 50 times a minute per IP.
## Scope
Limit failed logins on POST /login only.
## Non-goals
No CAPTCHA. No change to password rules. No new dependency.
## Constraints
- MUST use the existing Redis client in src/lib/redis.ts.
- MUST NOT log passwords or tokens.
## Acceptance criteria
1. Given 5 failed logins for one email in 10 minutes, when a 6th is sent, then the response is 429 with a Retry-After header.
2. Given a successful login, then the counter for that email resets.
3. Given Redis is unreachable, then login still works and the failure is logged.
What goes wrong
- Criteria that cannot fail. "Works correctly" and "is fast" are wishes, not checks.
- A spec that is a design. If it dictates function names and loops, the agent follows the plan even when it is wrong.
- No examples. Without a concrete case, two readings of one sentence both look correct.
- Copy-pasted template with empty headings. Remove sections you do not need; empty ones invite guessing.
Examples
Turning a wish into a testable criterion
Wish: "Search should be fast."
Criterion: "Given 10,000 products, when a user searches for a term with 50 matches, then the first page returns in under 300 ms on the CI database."
The agent now has a number, a data size and a place to measure, and a test can fail.
A non-goals block that stops scope creep
## Non-goals
- Do not rename existing columns.
- Do not add a caching layer.
- Do not touch the mobile client.
Without these three lines an agent asked to speed up a query may also refactor the model and add a cache.
Key points
- Cover six parts: problem, scope, non-goals, constraints, examples and acceptance criteria.
- Non-goals are the cheapest guard against scope creep, such as an unrequested refactor or a new dependency.
- Write acceptance criteria so each line is true or false and can become one test, for example in Given / When / Then form.
- Use MUST for hard rules and SHOULD for preferences, and link to the files that show the existing pattern.
- Include at least one edge case and one error case in the examples.
Common mistakes
- Writing acceptance criteria such as 'works well' that no test can fail.
- Leaving out non-goals, so the agent adds a dependency, a refactor or extra features.
- Dictating the implementation step by step instead of the behaviour, which locks in a wrong plan.
- Giving no example, so one sentence has two valid readings and the agent chooses silently.
Check yourself · 2 of 3
Which acceptance criterion can a test actually check?
Check yourself · 3 of 3
In a spec, what does MUST mean compared with SHOULD (RFC 2119)?
Practice items, not counted towards your mastery.
Start here
- GitHub Spec Kit (opens in a new tab)ToolFree
An open-source toolkit with commands for writing a spec, a plan and a task list before code.
- Understanding Spec-Driven Development: Kiro, spec-kit and Tessl (martinfowler.com) (opens in a new tab)ArticleFree
A critical comparison of three spec tools, including where specs still fail agents.
- Specs (Kiro docs) (opens in a new tab)DocsFree
How one agentic IDE structures a spec as requirements, design and tasks files.
More for this topic
- RFC 2119: Key words for use in RFCs (opens in a new tab)StandardFree
Defines MUST, SHOULD and MAY, a precise way to say how strict a requirement is.
- Gherkin reference (Cucumber) (opens in a new tab)DocsFree
The Given / When / Then format for writing acceptance criteria that map to tests.