Overview
I read somewhere that working with an agent is like working with a brilliant, chaotic overachiever. An agent produces more in an afternoon than a team used to in a week, and it leaves a mess behind that nobody notices until someone has to clean it up. That makes design system documentation matter more than ever. If you want production-quality code, the agent needs clear guidance on how to build. We love the enthusiasm, but at scale it’s a bit unmaintainable.
The architecture of this POC draws on design system teams who published their work openly. Diana Wolosin (Indeed) shaped a key decision in particular: document the system in JSON rather than prose. In her experience, models perform better with structured data, and a schema can act as a contract the system enforces.
Another huge shout-out to TJ Pitre, Nathan Curtis, and Murphy Trueman. Their work and their willingness to share shaped many of the decisions below, including the language and terminology. I’m grateful for what they’ve put out, and I hope to pay it forward.
Context
The project the system is built for is an analytics tool for monitoring product quality. We use spec-driven development so the whole team, including the agents we build with, works from the same context. Before product work began, I put together my first take on what a machine-readable design system needed. We started with shadcn/ui because it covered the components we needed. I themed a set of common components in Figma and TSX files, then wrote Markdown instructions.
Problem
Once the build started, the agent ignored those instructions. It built custom components instead of using the themed ones, including several that shadcn already provided, and styles drifted from screen to screen. The rules existed, but the Markdown file stated them loosely, without the structure and connections an agent needs to follow them. When the project paused before its next phase, I went back to make the system something an agent would follow.
## Before Writing Any Code
Do both of these first — every time, no exceptions:
**1. Check installed components**
Only use components that exist in `src/components/ui`.
If you need one that isn't there, flag it.
**2. Check the token source**
CSS variables live in `src/index.css`. Scan it to confirm
available token names before writing any color, radius, or shadow.
## Token Rules
Every color, radius, and shadow must use a CSS variable.
No hardcoded values. Ever.
## Component Rules
Always use an installed SHADCN component before building anything
custom.Original agent instructions
variants: {
variant: {
// In Figma
default: "bg-primary text-primary-foreground [a]:hover:bg-primary/80",
// In Figma
outline:
"border-border bg-background hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:border-input dark:bg-input/30 dark:hover:bg-input/50",
// NOT in Figma
secondary:
"bg-secondary text-secondary-foreground hover:bg-secondary/80 aria-expanded:bg-secondary aria-expanded:text-secondary-foreground",
// In Figma
ghost:
"hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:hover:bg-muted/50",
// NOT in Figma
destructive:
"bg-destructive/10 text-destructive hover:bg-destructive/20 focus-visible:border-destructive/40 focus-visible:ring-destructive/20 dark:bg-destructive/20 dark:hover:bg-destructive/30 dark:focus-visible:ring-destructive/40",
// In Figma
link: "text-primary underline-offset-4 hover:underline",
},
// Figma has one size. The install included eight.
size: {
default:
"h-8 gap-1.5 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",
xs: "h-6 gap-1 rounded-[min(var(--radius-md),10px)] px-2 text-xs in-data-[slot=button-group]:rounded-lg has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&_svg:not([class*='size-'])]:size-3",
sm: "h-7 gap-1 rounded-[min(var(--radius-md),12px)] px-2.5 text-[0.8rem] in-data-[slot=button-group]:rounded-lg has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&_svg:not([class*='size-'])]:size-3.5",
lg: "h-9 gap-1.5 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",
icon: "size-8",
"icon-xs":
"size-6 rounded-[min(var(--radius-md),10px)] in-data-[slot=button-group]:rounded-lg [&_svg:not([class*='size-'])]:size-3",
"icon-sm":
"size-7 rounded-[min(var(--radius-md),12px)] in-data-[slot=button-group]:rounded-lg",
"icon-lg": "size-9",
},
}outline and ghost, while the code also included secondary and destructive. Figma had "Show icon," but code expected you to place the icon yourself. Nothing told an agent (or human) which environment to trust.Architecture
The agent didn’t need more instructions. It needed structure it could follow. So instead of describing the design system in prose, I gave the agent a path to the source: where each answer lives, and in what order to read them.
Overview
We see this architecture as a decision tree. The agent still hits a Markdown file first, but from there it gets routed to the files that hold the real answers instead of paragraphs describing them.
Entry point
The AGENTS.md file is still the starting point for the system. Rather than trying to explain every design decision there, it points to the canonical sources, shows where each lives in the repo, and gives a clear order for reading them.
## Building a feature
When you're asked to build or change UI, follow these steps in order.
1. Read `tokens/foundations.json` to see which tokens you can use.
2. Read `src/components/ui/catalog.json` to see which components you can use.
3. For each component you plan to use, read its contract and `usage.md`.
4. Build the feature with only those tokens and components.
5. Run `npm run lint:ds`. If it flags anything, fix it and run it again.
6. Once the linter passes, run `npm run build`.
If you need a component the catalog doesn't have, stop. Don't build a
substitute, install one, or add a dependency. Tell the user what you needed
and the closest catalog entry.Tokens
For us, tokens are authored in Figma. That’s where we make the decisions like color roles, spacing, type, etc. From there, we export tokens.json in the W3C Design Tokens (DTCG) format and use it to keep everything else in sync: index.css, and a smaller “quick-pick” file (foundations.json) that highlights the semantic tokens we want agents to reach for first. The index.css and foundations.json are both generated from tokens.json and are not hand-authored.
foundations.json
foundations.json is a generated summary of our theme (semantic) and primitive tokens. The approach is similar to patterns described in Vercel’s generative UI work: provide a short, approved set of options so the agent selects from the design system rather than inventing new values. The agent starts with a theme token; if none fits, it can use a primitive, but it has to tell the user which one and what for, so the gap is visible. The full tokens.json export is the right place to store a token but the wrong place to choose one.
{
"purpose": "Pick UI tokens from this file. Start with a theme token. If none fit, use a primitive and tell the user which one you chose and what you used it for.",
"theme": {
"color": {
"primary": {
"description": "Primary action surface (default button)",
"alias": "primitives.color.neutral.900"
},
"primary-foreground": {
"description": "Text/icon on primary surface",
"alias": "primitives.color.neutral.50"
}
},
"typography": {
"roles": {
"body": {
"class": "text-body",
"use": "Default UI copy.",
"variants": { "emphasis": "text-body-emphasis" }
}
}
}
},
"primitives": {
"radius": {
"xs": { "value": "2px", "description": "Barely rounded. Use for very small elements, such as inline code, keyboard shortcuts and progress bars." },
"sm": { "value": "4px", "description": "Use for small controls, such as badges, checkboxes and small buttons." },
"md": { "value": "8px", "description": "Default for controls, such as buttons, inputs and selects." },
"xl": { "value": "12px", "description": "Use for surfaces that hold other content, such as cards, dialogs and popovers." }
}
}
}Trimmed to a few tokens. Contracts refer to them by short names: a theme token by its own name, like primary, and a primitive without its primitives prefix, like radius.md.
Components
Each component ships as a folder rather than a single file, so the implementation, the contract, and the usage guidance travel together. An agent that picks up a component gets all three at once. This reduces the classic drift where code lives in one place and “documentation” lives somewhere else and slowly becomes wrong.
catalog.json
catalog.json is the closed list of components the agent can use, the component counterpart to foundations.json. Each entry says what the component is for, how to import it, when to use it, what to use instead, and which variants it has. It’s generated from each component’s contract and usage.md, and the linter fails if it falls out of date. If a feature needs something that isn’t in the catalog, the agent stops and asks instead of building its own.
{
"purpose": "Pick UI components from this file. If none fit, stop and tell the user what you needed instead of building your own. Open a component's contract only when you need element-level detail.",
"components": {
"button": {
"title": "Button",
"description": "Buttons let users take an action on the current screen, such as saving a form, confirming a choice, or closing a dialog. The label states what happens when the user selects the Button. The `variant` prop sets visual emphasis: use `default` for the primary action, `outline` for a secondary action, and `ghost` for a low-priority or repeated action.",
"import": {
"from": "@/components/ui/button/button",
"exports": ["Button", "buttonVariants"]
},
"useWhen": [
"Triggering an action on the current screen, such as Save, Submit, Confirm, or Cancel.",
"Triggering an icon-only action, such as closing a dialog. Provide an `aria-label`."
],
"useInstead": [
"Navigating to another page. Use `Link` from `react-router-dom` instead.",
"Switching a setting on or off. Use `Toggle` instead.",
"Switching between views on the same page. Use `Tabs` instead."
],
"props": {
"variant": { "enum": ["default", "outline", "ghost"], "default": "default" },
"size": { "enum": ["default", "sm", "icon", "icon-sm"], "default": "default" }
},
"figmaOnlyAxes": ["state"],
"files": {
"contract": "src/components/ui/button/button.contract.json",
"usage": "src/components/ui/button/usage.md"
}
}
}
}Contract script (Figma Desktop Bridge)
Component authoring happens on canvas for hands-on control. That layer of specificity was what I wanted to see in the code implementation. The variants offered to a designer should be the same for an engineer or agent.
To align what's true in Figma with what ships in code, I needed a deterministic way to extract decisions from the canvas. I created a script to run in Figma via Desktop Bridge to do this for me.
The contract’s shape draws on Nathan Curtis’s Specs schema: anatomy, props, variants, and token references, in a form you can regenerate the same way every time. I am not using the Specs plugin or CLI, since the free tier does not include token bindings. I used the schema as a reference and wrote a separate extractor that reads live bindings through the Figma Desktop Bridge.
After the contract is written, the linter checks that every prop in it, like variant and size, exists in the co-located .tsx, and marks the ones that only exist in Figma.
{
"components": {
"button": {
"title": "Button",
"props": {
"variant": { "type": "string", "enum": ["default", "outline", "ghost"], "default": "default" },
"size": { "type": "string", "enum": ["default", "sm", "icon", "icon-sm"], "default": "default" },
"state": { "type": "string", "enum": ["default", "disabled"], "default": "default", "figmaOnly": true }
},
"default": {
"elements": {
"root": {
"styles": {
"backgroundColor": { "$token": "primary" },
"cornerRadius": { "$token": "radius.md" },
"padding": { "$token": "spacing.2" },
"itemSpacing": { "$token": "spacing.1" }
}
},
"Label": {
"styles": {
"textColor": { "$token": "primary-foreground" },
"typography": {
"fontFamily": { "$token": "font.family.sans" },
"fontSize": { "$token": "font.size.sm" },
"fontStyle": { "$token": "font.weight.medium" }
}
}
}
}
},
"variants": [
{
"configuration": { "variant": "outline", "state": "default", "size": "default" },
"elements": {
"root": {
"styles": {
"backgroundColor": { "$token": "secondary" },
"strokes": { "$token": "input" }
}
},
"Label": {
"styles": { "textColor": { "$token": "secondary-foreground" } }
}
}
}
]
}
}
}.tsx
The .tsx file keeps shadcn's component names, props, and behavior, but its variant options now match those available in Figma.
The file sits in a folder with its contract and its usage.md, which was my engineering partner's suggestion. When the catalog tells an agent to import Button from @/components/ui/button/button, the rules for using it are in the same folder, so opening the implementation and opening the guidance happen in one step.
variants: {
variant: {
default:
"bg-primary text-primary-foreground hover:bg-primary/90 [&_i]:text-primary-foreground aria-expanded:bg-primary/90",
outline:
"border-input bg-secondary text-secondary-foreground [&_i]:text-secondary-foreground hover:bg-accent hover:text-accent-foreground hover:[&_i]:text-accent-foreground aria-expanded:bg-accent aria-expanded:text-accent-foreground",
ghost:
"text-secondary-foreground [&_i]:text-secondary-foreground hover:bg-accent hover:text-accent-foreground hover:[&_i]:text-accent-foreground aria-expanded:bg-accent aria-expanded:text-accent-foreground",
},
size: {
default: "h-8.5 gap-1 px-2 text-body-emphasis",
sm: "h-5 gap-0.5 rounded-sm px-1 text-label [&_svg]:size-3",
icon: "size-8",
"icon-sm": "size-5 rounded-sm [&_svg]:size-3",
},
}usage.md
Some rules don't belong in the component itself. The code in button.tsx doesn't stop an agent from using a Button to navigate to another page. The code compiles, looks right, and ships. But navigation should use Link, not a Button. usage.md sits next to the component and tells the agent when to use it, when to choose something else, and what to provide even if the code doesn't require it.
# Button
## description
Buttons let users take an action on the current screen, such as saving a form,
confirming a choice, or closing a dialog. The label states what happens when the
user selects the Button. The `variant` prop sets visual emphasis: use `default`
for the primary action, `outline` for a secondary action, and `ghost` for a
low-priority or repeated action.
## useCases
### recommended
- Triggering an action on the current screen, such as Save, Submit, Confirm, or Cancel.
- Triggering an icon-only action, such as closing a dialog. Provide an `aria-label`.
### discouraged
- Navigating to another page. Use `Link` from `react-router-dom` instead.
- Switching a setting on or off. Use `Toggle` instead.
- Switching between views on the same page. Use `Tabs` instead.
## guidelines
- **should** (`visual-design`): Use no more than one `variant="default"` Button per
form, dialog footer, or card.
- **rationale:** When two filled Buttons share a container, users can't tell
which action is primary.
- **should** (`visual-design`): Use `outline` for a secondary action that offers
an alternative to the primary action, such as Back, Save draft, or Preview.
- **rationale:** The outline keeps the action visible as a real choice while
staying below the primary action in the hierarchy.
- **should** (`visual-design`): Use `ghost` for the lowest-priority action in a
group, such as Cancel or Skip, and for actions that repeat across a layout,
such as toolbar actions, table row actions, card actions, and icon-only Buttons.
- **rationale:** Ghost Buttons add no border or fill at rest, so dismissive and
repeated actions don't compete with the content or the primary action.
- **should** (`visual-design`): When a group has three actions, rank them
`default`, then `outline`, then `ghost`. Do not use two `outline` Buttons in
the same group.
- **rationale:** Each variant represents one level of emphasis. Repeating a
level makes the ranking ambiguous.
- **must** (`interaction`): Disable a Button with the `disabled` prop. Do not
simulate a disabled state with a no-op handler or an opacity utility.
- **rationale:** A simulated disabled Button still fires its handler and stays
in the tab order. The `disabled` prop prevents both and communicates the
state to assistive technology.
- **must** (`development`): Set `type="submit"` on the Button that submits a form.
- **rationale:** Unlike the native `<button>` element, this Button defaults to
`type="button"`. Without `type="submit"`, a Button inside a `<form>` does not
submit it.
## content
- **Visible label:** Start with a verb that describes the action.
Do: `Save changes`. Don't: `Click here to save`.
- **Accessible name:** For icon-only Buttons, describe the action, not the icon.
Do: `aria-label="Close"`. Don't: `aria-label="X icon"`.The linter
The linter runs 11 checks to keep the design-system files in sync. It checks that the generated token files are up to date, that each component’s props exist in its code, and that the catalog reflects the contracts and usage files. Three checks compare the generated token files with each other, which catches anyone hand-editing one. It also catches hard-coded colors and sizes in the theme and components, with a few documented exceptions. Right now, it’s the only automated check we have for the design system. It can’t check Figma, so I still have to re-run the extraction when a design changes.
/**
* Checks that the design-system files all agree with each other.
*
* Checks (11):
* tokens-fresh index.css and foundations.json match tokens.json
* theme-no-raw-values every theme token in index.css points to a primitive, never a raw value
* components-no-raw-values every component uses tokens, never raw units or colors
* foundations-primitives foundations.json hex and radius values match index.css
* foundations-theme-exists every theme token in foundations.json exists in index.css
* foundations-theme-alias every theme token in foundations.json points to the primitive it names
* contract-props every option in a contract exists in its .tsx, or is marked figmaOnly
* contract-schema every contract follows the structure in contract.schema.json
* contract-canonical every contract uses the same JSON formatting
* contract-tokens every token a contract uses exists in foundations.json
* catalog-fresh catalog.json matches what the contracts and usage files produce
*
*/The test
I tested the system with a simple feature: a way for users to report a data problem. For this POC, I’d made four components machine-readable: Button, Dialog, Textarea, and Toast. The old versions of all four were still installed, so when I gave a fresh agent a spec to let users flag an issue, add details, and submit it from the left rail, it could have used either.
The test ran once, in the live repo, not a clean demo. The file the agent worked in already imported the old Button for the sign-in control, so the wrong component was one line away. The agent imported the new Button next to it, under its own name, and built the whole feature from the four new components. I was really excited.
// The old Button stays for sign-in. The agent imported the new one beside it.
import { Button } from "@/components/ui/button";
import { Button as DSButton } from "@/components/ui/button/button";
// On submit
toast.add({
title: "Problem reported",
description: "Thanks — we'll look into the data.",
type: "success",
});
// If it fails
toast.add({
title: "Couldn't report the problem",
description: "Please try again.",
type: "error",
timeout: 0,
priority: "high",
});
// The dialog
<Dialog>
<DialogTrigger render={<DSButton variant="ghost" size="sm" />}>
<RiErrorWarningLine />
Report a problem
</DialogTrigger>
<DialogContent>
<form className="contents" onSubmit={handleReportSubmit}>
<DialogHeader>
<DialogTitle>Report a problem</DialogTitle>
<DialogDescription>
Tell us what looks wrong with the data so we can investigate.
</DialogDescription>
</DialogHeader>
<Textarea
aria-label="Problem description"
placeholder="What looks wrong…"
maxLength={2000}
disabled={reportPending}
/>
<DialogFooter>
<DialogClose render={<DSButton variant="outline" />}>Cancel</DialogClose>
<DSButton type="submit" disabled={!trimmedReport || reportPending}>
{reportPending && <Spinner />} Submit report
</DSButton>
</DialogFooter>
</form>
</DialogContent>
</Dialog>The agent’s code as it came back, trimmed to the components and props it chose.
Changing one rule
The agent gave Cancel the outline style, exactly as the Dialog’s usage file said at the time. Looking at the result, I decided Cancel should be ghost, the quietest style, so it wouldn’t compete with Submit. I changed the rule once, in usage.md, so the next agent builds it that way. That’s the loop I wanted: the agent follows the system, and a design decision made in one place reaches every build.
What’s next
Next, my engineering partners at Crux and I are generalizing this so it works across products and clients. Two gaps come first. The linter confirms that each prop in a contract exists in code, but not yet that its options match Figma’s. And it can’t see Figma at all, so today I re-run the extraction by hand whenever a design changes.