Design tokens to code with an agent
For engineers
Take a token file from design to production with an IDE agent: check the tokens, scope and review the agent edit, and keep the result maintainable.
The one-sentence idea: a design system only reaches production when its tokens become code that an agent reuses, so you define the tokens in a standard format, give the IDE agent the context and rules to use them, review what it changed, and then check the result for duplicated, dead or off-convention code.
Why it matters
Design tokens are named design decisions: a colour, a spacing step, a type size. When a coding agent builds a screen without them, it invents values. It writes #0B5FFF in one file and #0c60ff in another, adds a second button component, and the system you designed starts to split. Agents are fast, so the split happens in hours, not months.
This scenario follows one team through the job. Harbor Pay has a React app with CSS Modules. The designer, Lena, has a token file. The developer, Omar, uses an IDE agent (Copilot agent mode, Cursor, Claude Code in the editor and Windsurf all work this way) to turn the tokens into CSS variables and swap them into the Button and Card components.
How to do it
- Define and check the tokens. Keep one source of truth in the W3C design-token format (
$value,$type, aliases such as{color.brand}). Ask a model to draft or extend the set, then check it yourself: contrast ratios against WCAG 2.2 (4.5:1 for normal text), names that follow one pattern, and scales without gaps or odd steps. - Make the system readable by the agent. Put the rules in the instruction file the tool loads at the start of every session (
AGENTS.md, or the tool's own file). List the tokens file, the component inventory and the rule "use a token, never a hex value; extend Button, never add a new button component". - Scope the task and attach context. Name the files: the tokens file, the two components, the styles entry point. State what is out of scope (no new dependencies, no unrelated formatting). Read the agent's plan before you approve it.
- Review the edit, not the summary. An agent's message says what it meant to do. The diff and a search of the repository say what it did. Check for partial edits, such as a renamed variable that one file still spells the old way, and revert unrelated files one by one.
- Check maintainability. Search for helpers the codebase already has, leftover scaffolding, abstractions built for needs nobody has, and comments that no longer match the code.
- Govern the change. A token or component change from an agent goes through the same review as one from a person. Near-duplicate tokens are rejected, and the docs are corrected to match the real props.
Before and after
Weak prompt: "Use our design tokens in the app."
Better prompt:
Convert tokens/tokens.json into src/styles/tokens.css as CSS custom
properties named --<group>-<name> (color.brand -> --color-brand).
Then replace hard-coded colours and spacing in Button.module.css and
Card.module.css with those variables. Follow AGENTS.md.
Out of scope: new dependencies, other components, formatting changes.
When done, run: grep -rnE "#[0-9a-fA-F]{3,8}" src --include=*.css
and paste the output.
The second prompt names inputs, an output format, a boundary and a check the agent must show.
What goes wrong
- Overclaiming. The agent says "all colours replaced" while a search still finds hex values in two other files. Ask for evidence, then run the search yourself.
- Silent breaks. A CSS variable with a misspelt name does not raise an error. The browser drops the declaration and the button turns transparent.
- Quiet scope creep. The agent adds a library, loosens a lint rule or skips failing tests to turn the build green.
- Parallel systems. The agent adds
PrimaryButtonorcolor.brand-blue-2instead of reusing what exists, and nobody notices until the next redesign.
Each of these is cheap to catch in review and expensive to catch after release.
Steps
Draft the tokens and check them
Lena asks a model to extend the token file with a muted text colour and a spacing scale. Before it is merged she checks contrast with a checker, confirms the names follow one pattern, and confirms every spacing step is a multiple of 4. Brand colours are defined once; other tokens alias them.
tokens/tokens.json tokens/tokens.json
{ "color": { "brand": { "$type": "color", "$value": "#0B5FFF" }, "text": { "default": { "$type": "color", "$value": "#1A1A1A" }, "onBrand": { "$type": "color", "$value": "#FFFFFF" } } }, "space": { "2": { "$type": "dimension", "$value": "8px" }, "4": { "$type": "dimension", "$value": "16px" } }}Write the rules the agent reads
Omar adds a short component inventory and the token rule to the instruction file. The agent now sees that Button already has variants, so it has no reason to invent a new one.
AGENTS.md AGENTS.md
## Design system- Tokens: tokens/tokens.json. CSS variables: src/styles/tokens.css.- Never write a hex colour or a pixel spacing value in a component. Use var(--...).- Components: Button (primary, secondary, ghost), Card, Modal. Extend these; do not add new button or card components.- New tokens need a design review. Do not add near-duplicates of existing tokens.Scope the agent-mode task and read its plan
Omar attaches the tokens file, the two components and AGENTS.md, then states what is out of scope. Before approving, he reads the agent plan and removes any step that is not in the request.
Agent plan before approval Goal: Convert tokens to CSS variables and use them in Button and Card
- Read tokens/tokens.json and AGENTS.md
- Generate src/styles/tokens.css
- Replace hex and px values in Button.module.css and Card.module.css
- Run the stylelint check
- Report remaining hex values found by grep
Review the diff and run the search yourself
The agent says it replaced every colour. Omar does not rely on that. He reads the diff, looks for variable names that differ from the generated file, and runs the grep himself. Unrelated formatting changes are reverted file by file in the editor.
Agent report and search - assistant
All hard-coded colours now use tokens.
- tool
$ grep -rnE "#[0-9a-fA-F]{3,8}" src --include=*.css
src/components/Modal.module.css:7: background: #FFFFFF;
src/components/Toast.module.css:3: color: #1a1a1a;
- assistant
Check the code for maintainability
Before merging, the team searches for helpers that already exist, leftover scaffolding and comments that contradict the code. The agent had written its own colour helper, although the codebase already had one. The duplicate is deleted and the existing helper is imported.
Search for an existing helper $ grep -rn "hexToRgb" srcsrc/lib/colors.ts:4:export function hexToRgb(hex: string) {src/utils/color.ts:2:export const hexToRgb = (h: string) => {Govern the change and fix the docs
The pull request is reviewed like any other. A proposed near-duplicate token is rejected in favour of the existing one. The generated documentation is compared with the real props, and a statement about a variant that does not exist is removed.
Skills this scenario uses
Where to study
- Design Tokens Community Group (opens in a new tab)StandardFree
The W3C community format for tokens: $value, $type and aliases.
- Web Content Accessibility Guidelines (WCAG) 2.2 (opens in a new tab)StandardFree
The contrast and focus criteria your colour tokens must meet.
- Style Dictionary (opens in a new tab)ToolFree
A build tool that turns a token file into CSS variables and other platform formats.
- AGENTS.md (opens in a new tab)DocsFree
A plain-markdown instruction file that coding agents read at the start of a session.
- Build with AI in VS Code (opens in a new tab)DocsFree
How agent mode plans, edits many files and lets you review changes.
- Refactoring (Martin Fowler) (opens in a new tab)BookFree
Small, behaviour-preserving steps for cleaning up code you did not write.
- Yagni (martinfowler.com) (opens in a new tab)ArticleFree
Why to avoid building abstractions for needs that do not exist yet.