How to Write Specs for AI Coding Agents (With a Real Example)

By Arso Stojović•November 23, 2026•12 min read
A developer asks an AI coding agent to "add search and filtering to the content dashboard." The agent adds a search box. Search only checks the entries already on the screen. A status filter leaves the user on page 3. Clearing the search box does not restore the expected list.
The code runs. The feature still misses the request.
To write specs for AI coding agents, you need more than a feature name. A useful specification tells the agent what to build, what to preserve, what could go wrong, and how to verify the result.
This tutorial turns that request into a complete spec.md. It is a practical slice of spec-driven development: one feature an agent can implement, and you can check.
How to write a good spec for AI agents?
A vague prompt names a feature. An actionable specification defines the behavior.
Vague prompt | Actionable specification |
|---|---|
Add search and filtering to the dashboard. | Add title-based search and status filtering to the existing content list. Define how search and filters interact with pagination, sorting, loading states, and API errors. |
A coding agent specification has four characteristics:ty
Clear requirements: What the agent should build, stated as observable behavior.
Relevant context: How the feature fits the existing application.
Explicit constraints. What the agent must preserve, and what it must leave alone.
Testable acceptance criteria. The conditions that show the implementation is finished.
Length is not what makes a specification useful. A specification is a structured description of expected behavior, implementation boundaries, and the conditions that count as success. When requirements are unclear, coding agents may make assumptions that lead to implementations you didn't intend. That is how a search box ends up querying only the current page.
Step 1: Define the feature goal and scope
Start with a clear feature goal
This tutorial uses a content dashboard as the example. It lists entries from a headless CMS. Users can move through pages and change the sort order. They cannot search by title or limit the list to Draft or Published entries, which complicates the workflow.
Content-driven applications often rely on structured entries retrieved through an API, so developers commonly need to implement or extend search, filtering, and pagination.
A useful feature overview answers three questions:
The problem: Editors cannot find an entry without paging through the list.
The outcome: A user can search by title, filter by status, or do both, and see a matching page of results.
What stays intact: Pagination, sorting, layout, and the rest of the dashboard keep their current behavior, except where this feature explicitly changes it.
Define what's in scope and out of scope
In scope: | Out of scope: |
|---|---|
|
|
Those boundaries belong in the spec from the first draft:
## 1. Feature Overview The content dashboard lists entries from a headless CMS. Users can page through the list and change the sort order. They cannot search by title or limit the list to Draft or Published entries. Add title search and status filtering to the existing list. Users should be able to find an entry by its title, narrow the list by publication status, and use both together. Pagination, sorting, and the rest of the dashboard stay as they are, except where this spec changes them. ## 3. Scope - Search entries by title. - Filter entries by Draft or Published status. - Combine search and status filters. - Run search and status filtering against all accessible entries. - Integrate the filters with the existing pagination. ## 8. Out of Scope - Full-text search of entry body content. - Filtering by author, date, or category. - Changes to content models or the content schema. - A redesign of the dashboard layout, navigation, or visual style. - Editing, creating, or deleting entries.
Key takeaway: Scope tells the agent which feature to build, and which architectural changes were never requested.
Step 2: Give the agent the right project context
Describe the existing architecture
The agent needs to see where the feature sits. For this worked example, the application looks like this:
A Next.js application.
An existing content dashboard.
A content API integration that loads entries from a headless CMS.
A paginated content listing.
Existing sorting, plus reusable UI components.
Without that picture, the agent has to guess where entries come from, how the list renders them, and how pagination and sort state already work. Those guesses become new state containers, a second API client, or a search that only sees the current page.
State the UI the feature may reuse. In this fictional dashboard, a loading indicator already exists, and an empty-state component does not. The agent reuses the indicator while a request is in flight. When a successful request returns no entries, it renders one line in the list area, using existing text styles: "No entries match the current search or filters."
The file paths in this tutorial are fictional. They illustrate a pattern. Swap in the files from your own repository before you run an agent. The dashboard is a worked example, separate from BCMS product behavior.
Reference relevant files and existing patterns
Point at the files the feature touches. Leave the source code in the repo, where the agent can read it.
## 2. Project Context Fictional layout. Replace each path with the matching file in your repo. - Framework: Next.js - Content source: headless CMS entries, loaded through the existing content API - Content listing: components/ContentList.tsx - API integration: lib/content-api.ts - Pagination: components/Pagination.tsx - Sorting: the existing sort control and sort state used by the list Existing conventions: - Reuse existing UI components and styling. - Preserve the current sorting behavior. - Follow the established API error-handling pattern. - Keep search, status, page, and sort in the same state the dashboard already uses for pagination and sorting. UI assumptions for this example: - A loading indicator already exists. Reuse it while a request is in flight. - An empty-state component does not exist. On zero results, render "No entries match the current search or filters." in the list area, using existing text styles.
Include the files, the data source, and the behaviors this feature touches. Leave out unrelated routes, old tickets, and components the task will not change. Extra context gives the agent more places to "improve" something you wanted left alone.
Naming those files is enough context for this feature. The same principle applies to structured information for AI systems: provide the fields, relationships, and context relevant to the task without introducing unnecessary details in the spec kit.
Step 3: Write clear functional requirements
Describe the expected search behavior
Turn the feature goal into behavior someone can observe.
For this dashboard, decide each of these before the agent starts:
Searchable fields: Title only.
Matching: Case-insensitive. "Homepage" matches "homepage".
Dataset: All accessible entries, not the rows on the current page.
Empty query: Show the entries that match any active status filter.
Debounce: Wait 300 ms after the user stops typing, then send one request for the latest query. Characters typed less than 300 ms apart do not each send a request.
Status during a pending debounce: Changing the status filter cancels that pending delay and immediately requests results for the current search query and the selected status.
Where filtering runs: On the server, through the existing content API.
Server-side search across the full accessible set is the decision that stops the "current page only" bug from the introduction. The 300 ms debounce stops a request on every keystroke. Canceling that debounce on a status change stops a delayed search from overwriting the filter the user just chose.
Define how search and filters work together
Status, search, pagination, and sorting meet in a few specific rules. Number them so you can refer back to each one in tests and in review.
## 4. Functional Requirements
FR-01: Users can search content entries by title.
FR-02: Search matching is case-insensitive.
FR-03: Search applies across all accessible entries, through the
existing content API. It does not search only the current page,
and it does not search body content.
FR-04: Users can filter by Draft or Published status. With no status
selected, entries of both statuses are eligible.
FR-05: Search and status filters can be combined. A result must match
the title query and the selected status.
FR-06: Changing search or filters resets pagination to the first page.
FR-07: Clearing search restores entries matching any remaining active
filters, and resets pagination to the first page.
FR-08: Existing sorting behavior is preserved. The active sort still
applies to the filtered result set.
FR-09: The search field waits 300 ms after the user stops typing before
the dashboard requests results. Characters typed less than 300 ms
apart do not each send a request.
FR-10: Changing the status filter cancels any pending search debounce
and immediately requests results using the current search query
and the selected status.Each line names one observable behavior.
FR-03 names the dataset, so a search of the current page cannot count as done.
FR-06 names the page reset, so a filter change cannot leave the user on page 3.
FR-07 names what an empty query restores when a status filter is still active.
FR-09 and FR-10 name when a request is sent, so a status change cannot race a search that has not left the browser yet.
Key takeaway: Functional requirements should leave as little room as possible for the agent to invent product behavior.
Step 4: Document technical constraints and edge cases
Set technical boundaries for the AI coding agent
Requirements describe the feature. Constraints describe what must stay unchanged around it:
Reuse the existing content API integration, with no second client.
Leave the content schema and content models unchanged in the spec file..
Preserve sorting and pagination, except for the page reset in FR-06 and FR-07.
Reuse established UI components, styling, and the existing loading indicator.
Add no dependency for search, filtering, debounce, or state.
Leave navigation, entry editing, and other unnamed dashboard behavior unchanged.
Settle choices like server-side filtering in the spec. Left open, the agent may pick one while writing code.
Define edge cases and failure scenarios
Edge cases belong in the spec alongside the happy path.
Situation | Expected behavior |
|---|---|
No matching entries | Display a clear empty state |
Empty search query | Display entries matching the active status filter |
Search changes while viewing page 3 | Reset pagination to page 1 |
API request fails | Display an error message with a retry option |
User types quickly | Send no request until 300 ms after the last keystroke |
Status changes while a search debounce is pending | Cancel the debounce and request results immediately |
Search and status filter return no matches | Display the standard empty state |
A response arrives for an earlier query during the 300 ms wait | Do not display it. Show only results for the current query, status, page, and sort |
User clears all filters | Restore the default content listing |
There is no empty-state component. A successful response with zero entries shows "No entries match the current search or filters." in the list area. That message is not an API error.
An API failure shows an error and a retry action. Retry repeats the same request.
Results from an earlier search or filter state must not replace the results for the current state, including while a new search request is waiting for its debounce interval. Only results matching the latest search query, selected status, page, and sort may be displayed. Leave the mechanism open.
A status change does not wait out a pending debounce in the code review process. It cancels that timer and sends one request for the query currently in the field and the status just selected, updating the spec for better clarity.
The default listing is all accessible entries, in the current sort, on page 1.
Key takeaway: Constraints protect the existing application. Edge cases define how the feature behaves past the happy path.
Step 5: Define testable acceptance criteria
Turn requirements into verifiable outcomes
Functional requirements describe what the system must do. Acceptance criteria define the conditions that demonstrate the requirement has been met.
Given/When/Then works well because each criterion has a starting state, an action, and a result you can check.
Given the dashboard contains multiple content entries When the user searches for "homepage" Then only entries with "homepage" in their title are displayed
"Search works correctly" cannot fail a review. The criterion above can. Either the list shows only matching titles, or it does not.
Include tests for combined behavior
Cover the interactions, not only the single controls:
Searching by title.
Filtering by publication status.
Combining search and filters.
Resetting pagination.
Handling empty results.
Handling API failures.
Preventing stale search results.
Debouncing search input.
Changing status while a search debounce is still pending.
Given the user is on page 3 And the Published status filter is active When the user enters a new search query Then pagination resets to page 1 And only published entries matching the search query are displayed
FR-09 needs its own check. A debounce that is only described in the requirements can ship as a request on every keystroke.
Given the user is entering a search query When they type several characters less than 300 ms apart Then the dashboard does not send a search request between those keystrokes When 300 ms pass without further input Then the dashboard sends one request using the latest search query
The finished spec also checks the status interaction: if the user selects a status before those 300 ms elapse, the pending debounce is canceled and one request goes out immediately for the current query and the selected status. AC-07 checks the other half of that wait: a response for the previous query must not update the list during those 300 ms, before the new request has started.
With criteria like these, review becomes a checklist. Search can look fine on page 1 while a filter change on page 3 never resets. A status filter can look fine alone while it ignores the title query, or while a delayed search overwrites it.
Key takeaway: If an acceptance criterion cannot be objectively verified, it probably needs to be rewritten.
Step 6: Put everything together in a complete spec.md
Now it's time to combine everything into a single spec.md file. The example below includes the feature goal, project context, functional requirements, technical constraints, edge cases, and acceptance criteria for our content dashboard.
Before using it with an AI coding agent, replace the fictional file paths with those from your repository and verify that your content API supports the required filtering behavior.
Treat this file as a starting point, not a specification you can copy into any project without reviewing its assumptions.
# spec.md — Content dashboard: title search and status filtering
Worked example for a fictional Next.js app. The dashboard reads entries
from a headless CMS. Replace the file paths and the API assumption with
the ones in your repository before handing this file to an agent.
## 1. Feature Overview
The content dashboard lists entries from a headless CMS. Users can page
through the list and change the sort order. They cannot search by title
or limit the list to Draft or Published entries.
Add title search and status filtering to the existing list. Users should
be able to find an entry by its title, narrow the list by publication
status, and use both together. Pagination, sorting, and the rest of the
dashboard stay as they are, except where this spec changes them.
## 2. Project Context
Fictional layout. Replace each path with the matching file in your repo.
- Framework: Next.js
- Content source: headless CMS entries, loaded through the existing content API
- Content listing: components/ContentList.tsx
- API integration: lib/content-api.ts
- Pagination: components/Pagination.tsx
- Sorting: the existing sort control and sort state used by the list
Existing conventions:
- Reuse existing UI components and styling.
- Preserve the current sorting behavior.
- Follow the established API error-handling pattern.
- Keep search, status, page, and sort in the same state the dashboard
already uses for pagination and sorting.
Assumptions:
These describe the fictional dashboard. They are not BCMS product behavior.
- lib/content-api.ts is the only content-fetching path this list uses.
- That API can return one page of entries filtered by title and by status
(draft or published), using the pagination and sort parameters the list
already sends.
- "Accessible entries" means every entry the current user can already see
in this dashboard.
- Each entry has a string title and a status of Draft or Published.
- A loading indicator already exists. Reuse it while a request is in flight.
- An empty-state component does not exist. On a successful response with
zero entries, render this message in the list area, using existing text
styles: "No entries match the current search or filters."
- Page size stays at the dashboard's current value.
## 3. Scope
- Search entries by title.
- Filter entries by Draft or Published status.
- Combine search and status filters.
- Run search and status filtering on the server, across all accessible entries.
- Debounce search input by 300 ms.
- Cancel a pending search debounce when the status filter changes, and
request results immediately.
- Reset pagination to page 1 when the search query or status filter changes.
- Empty, error, loading, and stale-response behavior defined in this spec.
## 4. Functional Requirements
FR-01: Users can search content entries by title.
FR-02: Search matching is case-insensitive. "Homepage" matches "homepage".
FR-03: Search applies across all accessible entries, through the existing
content API. It does not search only the current page, and it does
not search body content.
FR-04: Users can filter by Draft or Published status. With no status
selected, entries of both statuses are eligible.
FR-05: Search and status filters can be combined. A result must match the
title query and the selected status.
FR-06: Changing the search query or the status filter resets pagination
to the first page.
FR-07: Clearing the search query restores entries that match any remaining
active status filter, and resets pagination to the first page.
FR-08: Existing sorting behavior is preserved. The active sort still
applies to the filtered result set.
FR-09: The search field waits 300 ms after the user stops typing before
the dashboard requests results. Characters typed less than 300 ms
apart do not each send a request.
FR-10: Changing the status filter cancels any pending search debounce and
immediately requests results using the current search query and
the selected status.
## 5. Technical Constraints
- Reuse lib/content-api.ts. Send the title query, status, page, and current
sort through that integration. Do not add a second content client.
- Perform filtering on the server, across all accessible entries.
- Do not modify the content schema or content models.
- Preserve existing sorting and pagination behavior, except for the page
reset in FR-06 and FR-07. Keep the current page size.
- Reuse established UI components and styling. Add the search field and
status control to the existing list controls.
- While a request is in flight, show the dashboard's existing loading indicator.
- Do not add an empty-state component. Use the message defined in Project Context.
- Do not add a dependency for debouncing, filtering, or list state.
- Do not change dashboard behavior this spec does not name. That includes
navigation, entry editing, and create flows.
- Results from an earlier search or filter state must not replace the
results for the current state, including while a new search request is
waiting for its debounce interval. Only results matching the latest
search query, selected status, page, and sort may be displayed.
## 6. Edge Cases
| Situation | Expected behavior |
| --- | --- |
| No matching entries | Show "No entries match the current search or filters." in the list area. Do not show an error. |
| Empty search query | Show entries that match the active status filter. If no status filter is active, show the default listing. |
| Search changes while the user is on page 3 | Reset pagination to page 1, then show the new result set. |
| API request fails | Show an error message and a retry action. Retry repeats the same request. Do not present the failure as an empty result set. |
| User types quickly | After the last keystroke, wait 300 ms, then send one request for the latest query. Do not send a request between keystrokes that are less than 300 ms apart. |
| Status changes while a search debounce is pending | Cancel the pending debounce. Immediately request results for the current query and the selected status. |
| A response arrives for an earlier search or filter state, including during the 300 ms debounce | Do not display it. Only results matching the latest search query, selected status, page, and sort may be shown. |
| Search and status filter return no matches | Show the same empty message used when search alone matches nothing. |
| User clears all filters | Restore the default listing: all accessible entries, current sort, page 1. |
## 7. Acceptance Criteria
AC-01 Title search
Given the dashboard contains entries titled "Homepage hero" and "Pricing FAQ"
When the user searches for "homepage"
Then only entries whose titles contain "homepage", regardless of letter case, are shown
And the match is evaluated across all accessible entries, not only the current page
AC-02 Status filter
Given the dashboard contains Draft and Published entries
When the user selects Published
Then only Published entries are shown
AC-03 Combined search and status
Given a Published entry titled "Homepage hero" and a Draft entry titled "Homepage notes"
When the user searches for "homepage" and selects Published
Then "Homepage hero" is shown
And "Homepage notes" is not shown
AC-04 Pagination reset
Given the user is on page 3
And the Published status filter is active
When the user enters a new search query
Then pagination resets to page 1
And only published entries matching the search query are shown
AC-05 Empty results
Given no accessible entry title contains "zzzz-missing"
When the user searches for "zzzz-missing"
Then the list shows "No entries match the current search or filters."
And the dashboard does not show an API error
AC-06 API failure
Given the content API returns an error for the current search
When the results request fails
Then the user sees an error message and a retry action
And activating retry sends the same request again
AC-07 Stale responses
Given the list is showing results for the query "home"
When the user changes the query to "homepage" and a response for "home" arrives during the 300 ms debounce, before the new request is sent
Then that response does not replace the list
And only results matching the latest search query, selected status, page, and sort may be displayed
Given a request for "home" is already in flight
When the user changes the query to "homepage" and the "home" response arrives after the "homepage" response
Then the list shows the "homepage" results
And the later "home" response does not replace them
AC-08 Clearing search
Given the Published filter is active and the search query is "homepage"
When the user clears the search query
Then the list shows Published entries with no title query
And pagination is on page 1
AC-09 Default listing
Given a title query and a status filter are both active
When the user clears the search and the status filter
Then the list shows all accessible entries in the current sort, on page 1
AC-10 Sorting preserved
Given the user has chosen the dashboard's existing sort
When they apply a title search or a status filter
Then the returned page is still ordered by that sort
AC-11 Search debounce
Given the user is entering a search query
When they type several characters less than 300 ms apart
Then the dashboard does not send a search request between those keystrokes
When 300 ms pass without further input
Then the dashboard sends one request using the latest search query
AC-12 Status change during a pending debounce
Given the user has typed a search query and the 300 ms debounce has not elapsed
When the user selects a status filter
Then the pending debounce is canceled
And the dashboard immediately requests results for the current query and the selected status
## 8. Out of Scope
- Full-text search of entry body content.
- Filtering by author, date, or category.
- Changes to content models or the content schema.
- A redesign of the dashboard layout, navigation, or visual style.
- A new empty-state component or a new dependency for search, filtering, debounce, or state.
- Editing, creating, or deleting entries.Key takeaway: A complete spec gives the agent enough to implement the feature without making product or architectural decisions on its own.
Step 7: Review your spec before running the agent
Read the spec once before you paste it into an agent session. The review looks for decisions the file still leaves open. An agent will answer those during implementation, usually without stopping to ask.
Use a short checklist:
Are all requirements specific and unambiguous?
Does the spec contain enough relevant project context?
Are search and filtering behaviors clearly defined?
Are technical constraints documented?
Are important edge cases covered?
Can every acceptance criterion be tested?
Are there unresolved decisions that require developer input?
You can ask an agent to run that review. Give it the spec and this prompt:
Review this spec for ambiguous requirements, missing edge cases, conflicting constraints, and acceptance criteria that cannot be objectively verified. Identify decisions that need clarification before implementation. Do not write code yet.
Treat the reply as a list of questions, not as permission to start coding. A review might reveal that the spec doesn't define how search handles leading and trailing whitespace, or what happens when the user changes sorting while a search request is pending. Resolve these questions if they affect the expected behavior before implementation. The agent can help you find gaps. You still approve the product and architectural decisions.
Common mistakes when writing specs for AI coding agents
Writing vague requirements
"Make search fast and user-friendly" does not name a field, a dataset, or a timing rule. The requirements in this tutorial do: title only, all accessible entries, and one request 300 ms after typing stops.
Providing too much irrelevant context
A repository map and a design history give the agent more files it might change. Name the list, the API module, and the pagination component.
Leaving implementation boundaries undefined
Without constraints, the agent may add a search library, rewrite the list, or change the content model. Say what must stay: the API client, the schema, and the current sort.
Writing acceptance criteria that cannot be tested
"Search works correctly" cannot be checked. A criterion needs a starting state and a visible result, such as a page reset or a single request after typing stops.
A longer specification is not automatically a better one; clarity is key in writing the spec. Cut background that does not change a requirement, a constraint, or a test.
Key takeaway: The goal is to reduce ambiguity, not to maximize document length.
Conclusion: Start with one feature and a clear spec
Writing specs for AI coding agents doesn't require documenting your entire application. Start with one feature, define the behavior you expect, identify what must remain unchanged, and make the outcome testable.
Use the spec.md example above as a starting point for your next feature. Adapt the project context, requirements, and acceptance criteria to your codebase before handing it to an AI coding agent.
The goal isn't to write the longest specification. It's to give the agent enough clarity to build the right feature and give you a reliable way to verify it.
Try BCMS free
Headless CMS for developers. Agents and clients can edit content. You keep the code.
14-day free trial · No credit card required
Try BCMS free
14-day free trial. No credit card. It takes a minute to start using BCMS.
Content
Try BCMS free
Headless CMS for developers. Agents and clients can edit content. You keep the code.
14-day free trial · No credit card required
Continue reading

AI Structured Content: Why AI depends on structure more than ever
How AI structured content is making machine-readable content for AI systems. Learn how MCP, structured outputs, metadata, and content models help AI.

BCMS AI Agent capabilities: What makes an AI Agent useful?
Explore AI agent capabilities: understand what AI agents can actually do, how they use AI, and how AI agents automate tasks with real-world examples.

UI Framework vs UI Library: What’s the difference in 2026?
UI framework vs UI library: learn the real difference in 2026. Compare frameworks, libraries, component libraries, and CSS frameworks with practical examples.
