# Design tokens, layered so a new brand costs one file

Primitives hold the values, semantics say what each one is for, and a third layer waits until a brand restyles one component. Every step is a prompt for a coding agent, with a check that fails when a rule breaks.

Disciplines: Design systems, Design engineering · Topics: Design tokens, Semantic tokens, Multi-brand theming, Style Dictionary, AI coding agents

Source: https://www.ahmedamr.com/writing/design-token-layers

Part 3 in *Build a design system with a coding agent*.

**A rebrand should cost one file.** In a product where every screen holds its own copy of the blue, it costs a search for every copy. This part builds the layers that make it one. Primitive tokens hold the values, semantic tokens say what each value is for, and a third layer appears only where one brand restyles one component. Every step is a prompt for a coding agent, and by the end eight checks fail with a plain sentence the moment one of their rules breaks.

The starter is a public repository[^repository] holding the system this series builds, with a tag at the end of each part, a name pinned to the commit[^commit] where that part finishes. Every prompt is printed in full, and under each one that builds part of the system a replay of the agent's work plays beside what it made, taken from the matching commit in the starter.

None of the prompts asks for a pull request or an explanation, because the rules file[^rules-file] from part two already does. In your repository each one ends with a pull request[^pull-request] to read, check and merge. The starter has one commit for each prompt that changes the repository, plus two that write the prompts down, so your history can be compared with mine under Commits on its GitHub page.

The starter repository at tag `part-3-tokens`: https://github.com/ahmedamr-r/design-system-starter/tree/part-3-tokens

This part starts from the tag [`part-2-setup`](https://github.com/ahmedamr-r/design-system-starter/tree/part-2-setup).

This part needs Node 24 or later. The demo page it builds runs on Vite, which needs Node 22.12 or later, and the series uses 24 from here on. If `node --version` prints less than 24, install Node 24 or later from [nodejs.org](https://nodejs.org/en/download), then open a new terminal so it takes effect and start your agent again.

Starting with this part? You need Git, the GitHub CLI and a coding agent too, set up as [part two shows](/writing/design-system-repository-setup#start-with-an-empty-folder-node-and-git). Download the starter at `part-2-setup` as a [ZIP file](https://github.com/ahmedamr-r/design-system-starter/archive/refs/tags/part-2-setup.zip) and unzip it. Move the folder it makes, `design-system-starter-part-2-setup`, into your home folder and rename it `design-system-starter`, so it sits where part two's route puts it. On Windows, Extract All wraps it in a second folder of the same name, so move the inner one. A new terminal opens in your home folder, so the commands start by moving into it.

```bash
cd design-system-starter
git init -b main
npm install
git add -A
git commit -m "Start from part-2-setup"
```

`cd` moves into the folder. The download has no history, so `git init -b main` makes it a repository whose main line is called `main`. `npm install` downloads the tools the build uses, and the last two save every file as the first commit. If Git stops to ask who you are, run the two `git config --global` lines it prints, with your own name and email, and commit again.

Start your agent in the same folder and give it this prompt.

**Prompt** Create the repository and push

```text
Create a public GitHub repository called design-system-starter with the GitHub CLI, connect this folder to it, and push the main branch. This first push to main is the one exception to the rule in AGENTS.md. Explain each command in one sentence before you run it. If I'm not signed in to the GitHub CLI, tell me the one command to sign in and stop.
```

Last, set up the [ruleset](/writing/design-system-repository-setup#nothing-reaches-main-without-passing-the-check) so a failing check blocks a merge.

## Tokens hold the decisions, in two layers

A design token[^design-token] is one decision with a name. `color.blue.600` is a particular blue, and `size.200` is 8 pixels. Tokens are stored as data, so one decision can become a CSS variable[^css-custom-property] for the web, a constant for an iOS app and a variable in a design tool.

Tokens work best in layers. The first layer holds raw values and names them for what they are. These are primitive tokens[^primitive-token], the palette of every blue, grey and size the product may use. **Nothing on screen reads a primitive directly.**

The second layer names jobs. `color.fill.brand` is the fill of whatever should be pressed first, and it points at one of the primitives instead of holding a value of its own. These are semantic tokens[^semantic-token], and they are what screens and components read. Point `color.fill.brand` at a green and every button turns green without anyone touching a button. The starter has two brands to try this on, Harbor in blue and Grove in green.

A third layer, component tokens[^component-token], names one part of one component, such as `button.radius`. This part starts with two layers and adds the third only when one brand restyles one component on its own.

*A drawing sits at this point on the page.* A building in three parts. The foundation is primitives, raw stone being hauled in. Two floors on it are semantics, with doors lettered fill, text, border, surface, radius and space. On the roof one room is built and labelled button, and the other rooms are only pegged out, with the words only when a client asks lettered across them.

Three layers of tokens, drawn as a building. This part builds the first two and one room of the third.

## A product you already have holds the first draft of its primitives

Most teams start from a product that already has colours in it. Before the first token, open your agent in that product's folder and ask it to count what the product uses. The prompt only reads, so it makes no commit.

**Prompt** Count what a product already uses

```text
This folder holds a product that already exists. Before we add any tokens, read its styles and list every colour, font size, font weight, spacing value and corner radius they use, with how many times each one appears. Put colours that are nearly the same next to each other, and mark anything used fewer than three times. Don't create or change any files.
```

Near-duplicates sit side by side on the list, which makes the drift easy to see, and a value used once or twice is a candidate to merge into its neighbour. The primitives this part builds keep each hue as a ramp, ten steps from 50, the lightest, to 900, the darkest, with white and a darker 950 added to grey, and merging moves each colour on the list to its nearest step. A product that lives only in design files can be counted in Figma instead, a screen at a time, starting from the Selection colors panel.

*An interactive figure sits at this point on the page. The five drifted main-button blues from part one sit under the starter's blue ramp, with a switch between the blues as found and the blues moved onto the ramp.*

- As found, Sign in is #2f72d6 (4.67:1 with white text), Checkout #1f62bd (5.93:1), Settings #3b80e3 (3.90:1, under the 4.5:1 WCAG asks), Team #2a5fae (6.28:1) and Billing #3470c9 (4.88:1).
- On the ramp, each moves to the nearest step by distance in OKLab. Sign in, Checkout and Billing land on `color.blue.600` (#226ec5, 5.12:1), Settings on `color.blue.500` (#3d87e0, 3.66:1, still under 4.5:1) and Team on `color.blue.700` (#21589a, 7.17:1).

Five button blues made up for part one, one per screen, each matched to the nearest step of the starter's blue ramp. Three land on blue 600.

**Moving a colour onto the ramp keeps about the lightness it had, and with it the contrast.** The Settings blue was too light for white text before the move, and the nearest step, blue 500, still is. Which step the main button gets is the semantic layer's decision.

If you made the list, copy it out of the agent's answer, because the agent in your design system's folder will not see it. Paste it at the end of the primitives prompt below, followed by these two sentences.

**Prompt** Build on the list

```text
Build the ramps and scales around the values in this list, and keep the gray, blue, green and red ramps the later prompts use. Tell me which values you merged.
```

If you are starting from nothing, skip this step.

## Primitives hold values and nothing else

The first prompt that changes the repository fills the bottom layer, and its one design decision is in the line about colour. **Every hue has the same lightness at each step**, so blue 600 is as light as green 600.

Token files are JSON, plain text in the format the W3C Design Tokens Community Group[^dtcg] published as its first stable version in October 2025. Each token is an object with a `$value`, the value itself, and a `$type` saying what kind of value it is, such as a colour or a size, written on the token or once on the group around it. A colour can be written in OKLCH[^oklch], where the first number is lightness, so the same lightness in every hue is the same first number. `npm run build` hands the files to Style Dictionary[^style-dictionary], the tool the starter's build runs on, which writes them out as CSS.

**Prompt** Add the primitives

```text
Add primitive tokens in tokens/primitives/. A primitive is a raw value named for what it is, never for where it's used.

- color.json: ramps for gray, blue, green and red, steps 50 to 900. Gray also gets 0 (white) and 950. Make the steps even in perceived lightness, so 600 is equally dark in every hue. Write colours in the format's OKLCH form, with a hex fallback.
- size.json: one scale for spacing and corners, in px: 0, 2, 4, 6, 8, 12, 16, 24, 32, and full (9999px).
- font.json: one sans-serif family, sizes 14, 16, 20 and 24, weights 400, 500 and 600.

Every token needs a $value and a $type. Run the build and show me part of the CSS.
```

*A replayed agent session plays at this point on the page:*

> Add primitive tokens in tokens/primitives/. A primitive is a raw value named for what it is… (full prompt above)

- Read `tokens/primitives/`: Empty.
- Created `tokens/primitives/color.json`: Four ramps. Each step has the same lightness in every hue.
- Created `tokens/primitives/size.json`: One scale from 0 to 32px, and full for pills.
- Created `tokens/primitives/font.json`: One sans family, four sizes, three weights.
- Ran `npm run build`: 60 primitives in dist/tokens.css.
- Said: The ramps were worked out in OKLCH, where the first number is lightness, so step 600 is 0.54 in blue, green, red and grey alike. The hex beside each one is the same colour, kept as a fallback.

A replay of the session behind the starter commit “Add primitive tokens”, with its excerpts trimmed.

**The blue ramp, trimmed** · `tokens/primitives/color.json`

```json
"color": {
  "$type": "color",
  "blue": {
    "50": { "$value": { "colorSpace": "oklch", "components": [0.97, 0.011, 255], "hex": "#f0f6fd" } },
    "500": { "$value": { "colorSpace": "oklch", "components": [0.62, 0.154, 255], "hex": "#3d87e0" } },
    "600": { "$value": { "colorSpace": "oklch", "components": [0.54, 0.154, 255], "hex": "#226ec5" } },
    "700": { "$value": { "colorSpace": "oklch", "components": [0.46, 0.121, 255], "hex": "#21589a" } },
    "900": { "$value": { "colorSpace": "oklch", "components": [0.3, 0.057, 255], "hex": "#192f4a" } }
  }
}
```

A colour's value names its colour space, lists its three numbers and carries a hex fallback. Style Dictionary turns each colour into hex for the CSS, and uses the fallback only for a colour outside sRGB, the range of colours every screen can show. It writes every token as a CSS variable in `dist/tokens.css`, with dashes where the name has dots, so `color.blue.600` comes out as `--color-blue-600: #226ec5;`. A style reads a variable back with `var()`, as in `var(--color-fill-brand)` once the semantics exist, so **a screen holds the name and the build supplies the value**.

White text measures 5.1:1 on blue 600 and 4.7:1 on green 600, both above the 4.5:1 that [WCAG asks of body text](https://www.w3.org/WAI/WCAG22/Understanding/contrast-minimum.html), so a brand that swaps one hue for another keeps close to the contrast it had. Part four adds a check that measures every pair.

*In Figma.* A Primitives collection with one mode, where color/blue/600 holds #226EC5 and size/200 holds 8.

The primitives file is one collection with a single mode and a variable per value. Figma separates the names with slashes where the code uses dots. Select every variable in it, choose Edit variables, untick Show in all supported properties and tick Hide from publishing, so designers pick from the semantics the way screens do in the code.

## Every job starts from merged work, and a bad one can be thrown away

Open the pull request the agent links to. Files changed shows every line it wrote, and the check runs near the bottom of the Conversation tab. When both look right, choose Squash and merge from the button's menu, so the job lands as one commit like the starter's, and confirm. Then tell the agent you merged it, because **each job should start a new branch[^branch] from the merged `main`** and carry everything before it.

**Prompt** Start the next job from main

```text
I merged the pull request. Switch to main and pull the merged work. I'll give you the next job, and it goes on a new branch from there.
```

The rules file counts a change to the build or check script as a job of its own. Some prompts in this part change a script beside other files, and one asks for two jobs at once, so if the agent stops to ask before the second job, say yes, since the prompt asked for both.

When the check fails, a red cross appears beside it. The Details link next to the cross opens the log of the run, and the failing step prints the same lines `npm run check` prints on your computer, naming the file and the rule. Paste them to the agent and ask for a fix on the same branch, and the check runs again once the fix is pushed.

A session that goes wrong can be thrown away before it reaches a pull request.

**Prompt** Throw away the last job

```text
Throw away everything you changed for this job, committed or not, switch back to main, and tell me what you discarded.
```

A pull request you decide against can be closed without merging. One that was merged and turns out wrong has a Revert button on GitHub, which opens a new pull request that undoes it, checked like any other.

## Semantic names say what a value is for, so they survive a rebrand

Name a colour by what it looks like, `blue-600`, and the name holds only until the brand changes. Name it by its job, `fill.brand`, and it stays true whatever colour the brand paints it. Rebrand the two cards below and watch which names start to lie.

*An interactive figure sits at this point on the page. It shows the same pricing card twice, one wired to colour names (`--blue-600`, `--blue-50`, `--gray-900`), one to job names (`--color-fill-brand`, `--color-fill-brand-subtle`, `--color-text-default`), and a switch that rebrands both to green.*

- Under Harbor every name on both sides is true.
- Under Grove both cards turn green. On the colour-named side, `--blue-600` and `--blue-50` now hold greens, so the names say blue and paint green. On the job-named side every name is still true.

The same card twice, one wired to colour names and one to job names.

The starter names semantic colours role first, then intent, then an optional state, as in `color.fill.brand`, `color.fill.brand-hover`, `color.text.on-fill` and `color.border.focus`. Four roles cover most interfaces. Surface is what things sit on, fill is the colour inside a button or a tag, and text and border are what they sound like. The intent says which job within the role, such as brand or link, and the state says when it applies, such as hover. Focus in `color.border.focus` is the focus ring's job, so it is an intent, and `-subtle` at the end marks a quieter version of the same job, as in `color.fill.brand-subtle`.

**The naming rule**

A primitive is named for what it is. A semantic is named for what it does.

This part ends with a check that rejects a semantic colour named after a colour, and part seven extends the pattern into a grammar for every layer.

**Prompt** Add the semantics

```text
Add semantic tokens in tokens/semantics/. A semantic token names a job, like "the fill of the main button", and points at a primitive. It never holds a raw value.

Name colours color.<role>.<intent>, with an optional state at the end: color.fill.brand, color.fill.brand-hover, color.text.on-fill, color.border.focus. The roles are surface, fill, text and border.

Cover what a simple product card needs: page and card surfaces, body and secondary text, a main button with a hover state, a subtle brand tint for tags, a link, a border and a focus ring. Add radius, spacing and type roles too.

Point the brand roles at gray for now. Give every token a one-line $description saying where it's used.
```

*A replayed agent session plays at this point on the page:*

> Add semantic tokens in tokens/semantics/. A semantic token names a job, like "the fill of the main button"… (full prompt above)

- Read `tokens/primitives/color.json`: Grey, blue, green and red ramps. Grey runs from 0 to 950.
- Created `tokens/semantics/color.json`: Twelve colours, each a job pointing at one primitive.
- Created `tokens/semantics/shape.json, space.json, type.json`: Radius, spacing and type roles, all pointing at primitives.
- Ran `npm run build`: Built. Each semantic comes out as a copy of its value.
- Said: Each colour is named role, then intent, so color.fill.brand is the fill of whatever should be pressed first. The brand roles point at grey for now, so the card reads as ink until a brand re-points them.

A replay of the session behind the starter commit “Add semantic tokens”, with its excerpts trimmed.

**A semantic points at a primitive** · `tokens/semantics/color.json`

```json
"color": {
  "$type": "color",
  "fill": {
    "brand": { "$value": "{color.gray.900}", "$description": "The main button: the one thing to press first." },
    "brand-hover": { "$value": "{color.gray.950}", "$description": "The main button under the pointer." }
  }
}
```

The braces make a reference[^token-reference]. **`{color.gray.900}` means whatever value that primitive holds**, so the semantic has no value of its own to go stale.

Shapes get roles the same way. `radius.control` rounds buttons, inputs and tags, `radius.surface` rounds cards and panels, and `radius.pill` rounds a shape's ends fully.

Every semantic that will carry a brand points at grey for now, so the card reads as ink on white until a brand claims it. That suits a white-label product, one sold to several clients under their own brands. The `$description` on each token is for the next person, or the next agent, deciding whether a new screen should use it.

*In Figma.* A Semantics collection where every variable is an alias, color/fill/brand pointing at color/gray/900.

A second collection whose variables are aliases of the primitives, never raw colours. Screens and components get their colours from this one. Give each variable a code syntax for the web, var(--color-fill-brand), and on a paid plan Dev Mode shows developers the name the code uses.

## A build that copies values breaks the day one token reads another

Run the build at this point and Style Dictionary writes each semantic as a copy of its value, so `--color-fill-brand` comes out as `#2d2e30`, the same hex as `--color-gray-900`, with nothing left to say the two are related.

With two layers nothing breaks yet, because a brand re-points semantics and screens read semantics directly. The trouble starts the day one token reads another that a brand has changed, and every component token does that. **Both systems I built asked their build for references and still shipped copies**, because the hand-written code that produced their CSS wrote each token's final value whatever the setting said. In one of them, a second brand turned the brand fill orange and every main button stayed ink, because the button's own token was a copy of the old fill.

Style Dictionary's `outputReferences` option writes `var(--color-gray-900)` where it would copy the hex. A page picks a brand with an attribute, so `data-brand="grove"` on a card makes that card and everything inside it Grove. In CSS, `:root` is the element that holds the whole page, and `[data-brand]` matches any element that carries the attribute. A CSS variable is declared where the file gives it a value, inside a block headed by the elements it applies to, so `:root { --color-fill-brand: var(--color-gray-900); }` declares the brand fill for the whole page.

**A CSS variable that reads another is worked out on the element where it is declared**, and the elements inside inherit the result. Declared only on `:root`, a token that reads another is worked out once, for the page. A card that names a different brand still gets new values for the tokens its brand file sets, but every token that only reads them keeps the page's result. The button's own background, later in this part, is one of those. Declared on `[data-brand]` as well, those tokens are worked out again on the card. In the other system I built, the shadows were declared on the page alone, so each brand folder repeated the same nine shadow tokens word for word only to have them worked out again with its colour.

*An interactive figure sits at this point on the page. A Harbor page holds two pricing cards, one that sets no brand and one set to Grove, and a switch builds the same token files three ways. Each build's CSS is put on the page, and the browser works out the cards' colours from it.*

- Copies, with references off. Every token holds its value, so `--button-background` is #2d2e30, the neutral fill, wherever it is read. Both buttons are grey 900, while each card's link and tag follow its brand. Check 5, which this part adds later, fails on 43 tokens, `--button-background` among them, and prints the first five.
- References on the page only, declared on `:root`. `--button-background` is worked out once, on the Harbor page, so both buttons are Harbor's `color.blue.600`, while the Grove card's link and tag are green. Check 6, which this part adds later, fails on 30 tokens, `--button-background` among them.
- References on every brand, declared on `:root` and on `[data-brand]`, as the starter builds them. `--button-background` is worked out again on the Grove card, so its button is `color.green.600` and the other stays `color.blue.600`. It passes every check this part adds.

The same token files built three ways, on a Harbor page with a Grove card inside. Only one build gets the Grove button right.

The next prompt asks for both.

**Prompt** Keep references

```text
Change the build so semantic tokens stay references in the CSS: --color-fill-brand should come out as var(--color-gray-900), not as a hex value.

Put primitives on :root. Put semantics on :root and also on [data-brand], so a brand set on any element re-resolves them there.

Show me a few lines of dist/tokens.css before and after.
```

*A replayed agent session plays at this point on the page:*

> Change the build so semantic tokens stay references in the CSS… (full prompt above)

- Ran `npm run build`: Before the change, every semantic is a copy of its value.
- Edited `scripts/build.js`: Primitives and semantics now build as two blocks.
- Ran `npm run build`: After it, semantics point at primitives, on both scopes.
- Said: outputReferences keeps each semantic as var() pointing at its primitive, so changing what a semantic points at is the only edit a brand ever needs. Declaring semantics on [data-brand] too means a brand set on any element is worked out right there.

A replay of the session behind the starter commit “Build CSS that keeps references”, with its excerpts trimmed.

**Keep every reference in the built CSS**

Chose: Every semantic and component token is built as var() pointing one layer down, and declared on :root and on [data-brand].

Trade-off: The CSS is longer, and the browser's inspector shows a chain of names before it shows a colour.

*In Figma.* Two Semantics variables, one an alias of color/gray/900 and one holding the pasted hex #2D2E30.

This is the difference between setting a variable to another variable and pasting its hex. Both look the same today, and only the alias follows when the primitive changes.

## A new brand is one file of re-pointed semantics

With references kept, a brand is a file that re-points the few semantics that carry the brand. **It adds no new tokens and copies none of the semantics it leaves alone.**

**Prompt** Add two brands

```text
Add two brands, Harbor (blue) and Grove (green), as one file each in tokens/brands/. A brand file only re-points existing semantic tokens at different primitives. It adds nothing new.

The build should find brands by reading that folder, so adding a brand never means editing code. Write each brand as a [data-brand="name"] block after the semantics.
```

*A replayed agent session plays at this point on the page:*

> Add two brands, Harbor (blue) and Grove (green), as one file each in tokens/brands/… (full prompt above)

- Read `tokens/semantics/color.json`: Six roles carry the brand. Three fills, brand text, the link and the focus ring.
- Created `tokens/brands/grove.json`: Six semantics re-pointed at the green ramp. Harbor's file does the same with blue.
- Edited `scripts/build.js`: Brands are whatever files sit in tokens/brands/.
- Ran `npm run build`: Wrote dist/tokens.css with 2 brands: grove, harbor
- Said: Each brand is a block that re-points six semantics and nothing else. To add a third brand, copy one of the files and change the colours; the build finds it.

A replay of the session behind the starter commit “Add brands as one file each”, with its excerpts trimmed.

**A whole brand** · `tokens/brands/harbor.json`

```json
{
  "color": {
    "$type": "color",
    "fill": {
      "brand": { "$value": "{color.blue.600}" },
      "brand-hover": { "$value": "{color.blue.700}" },
      "brand-subtle": { "$value": "{color.blue.50}" }
    },
    "text": {
      "brand": { "$value": "{color.blue.700}" },
      "link": { "$value": "{color.blue.600}" }
    },
    "border": {
      "focus": { "$value": "{color.blue.600}" }
    }
  }
}
```

**The built CSS, trimmed** · `dist/tokens.css`

```css
:root {
  --color-gray-900: #2d2e30;
  --color-green-600: #1e8441;
}

:root, [data-brand] {
  --color-fill-brand: var(--color-gray-900);
  --color-text-link: var(--color-gray-900);
}

[data-brand="grove"] {
  --color-fill-brand: var(--color-green-600);
  --color-text-link: var(--color-green-600);
}
```

The brand blocks come last because `[data-brand]` and `[data-brand="grove"]` weigh the same in CSS, so whichever comes later wins. The check the next prompt adds fails if a brand block ever moves above the semantics.

The build finds brands by reading the folder, and an agency, which adds clients as it goes, needs that most. **In both systems I built, the list of brands was typed out by hand in at least seven files**, from the build config and the package's exports to the theme switcher and the documentation. With the folder as the only list, a new client is a new file and a pull request anyone can review.

*A drawing sits at this point on the page. It shows two copies of the same building, one per brand. On the second, a worker repaints only the floors, the semantic layer, from blue to green; the foundation is untouched.*

A rebrand repaints one floor.

*In Figma.* The Semantics collection with a Harbor mode and a Grove mode. color/fill/brand is color/blue/600 in Harbor and color/green/600 in Grove, and color/text/default is color/gray/900 in both.

A brand file is a mode. Rename the Value mode to Harbor, add a Grove column, point the brand rows at each ramp, and leave every other row aliased as it was. Modes need a Professional, Organization or Enterprise plan, or Figma's Education plan, with up to 10 per collection on Professional and 20 on Organization.

## A demo page and six checks catch a broken rule before a screen shows it

The next prompt puts a brand inside a brand on a demo page, and adds a script that fails when one of six rules breaks.

**Prompt** Add a demo and a check

```text
Add demo/index.html with the same pricing card three times, side by side: Harbor, Grove, and a Grove card inside a Harbor panel. The card shows a plan name with a "Most popular" tag, a short description, a price, three features with check marks, a main button and a link to compare plans. Its CSS reads semantic tokens only. Add npm run demo to build and open it.

Then write scripts/check.js for npm run check. It should print a plain message and fail when:
1. a colour value appears anywhere outside tokens/primitives
2. a semantic or brand token points at anything but a primitive
3. the demo CSS reads a primitive
4. a brand file sets a token that doesn't exist (suggest the closest name)
5. a semantic in dist/tokens.css is a raw value instead of var()
6. a semantic is declared without the [data-brand] scope
```

This is the prompt with two jobs, because the check needs the demo to have something to read.

*A replayed agent session plays at this point on the page:*

> Add demo/index.html with the same pricing card three times, side by side… (full prompt above)

- Created `demo/index.html`: The plan card three times. The third is Grove inside a Harbor panel.
- Created `demo/demo.css`: Every colour is a semantic token.
- Created `scripts/check.js`: Six checks, each failing with a sentence about what is wrong.
- Ran `npm run check`: All checks passed.
- Said: The third card sets Grove inside a panel that is already Harbor, and it comes out green. The last two checks keep it that way once a token starts reading another token.

A replay of the session behind the starter commit “Add a side-by-side demo and a check script”, with its excerpts trimmed.

`npm run demo` builds the tokens and opens the page through [Vite](https://vite.dev), a small development server the agent added for it. Run it yourself in a second terminal window, so the agent keeps the first. Cmd+N opens one in Terminal on a Mac, and Ctrl+Shift+N in Windows Terminal.

```bash
cd design-system-starter
npm run demo
```

The page shows the card three times. Harbor's is blue and Grove's is green, and the third comes out green too, button and link included, inside a panel whose own link stays Harbor blue. The server runs until you press Ctrl+C in that window. Vite keeps serving the token file it read first, so after the agent rebuilds the tokens, press Ctrl+C and run `npm run demo` again. If the agent starts the demo itself, its session waits on the server, so stop it and tell the agent you will run the demo.

Break a rule and the check says which one, and where. Misspell a token in a brand file and, at this step, it answers with `tokens/brands/grove.json: color.fill.brand-hovr is not a semantic token. Did you mean color.fill.brand-hover?` **Checks 5 and 6 read the built CSS instead of the token files**, so a build that ignores its own setting still fails.

## The rules file holds the decisions an agent cannot guess

Every section above is a decision, and **an agent that has not been told a decision will make a different one next week**. Part two started the rules file, `AGENTS.md`, with how to work in the repository. The decisions about tokens go under those rules.

**Prompt** Add the token rules

```text
Add the token rules to AGENTS.md, after the ones on how to work. Keep them short:
- the layers, and what each one may point at
- the naming pattern for semantic colours
- raw values live only in primitives
- a brand is one file in tokens/brands, and its name appears nowhere else

Say which of these the check enforces.
```

*A replayed agent session plays at this point on the page:*

> Add the token rules to AGENTS.md, after the ones on how to work. Keep them short… (full prompt above)

- Read `AGENTS.md`: How to work and where things go, from part two.
- Read `scripts/check.js`: Six checks to line the rules up with.
- Edited `AGENTS.md`: What each layer may point at, how semantic colours are named, and brands.
- Ran `npm run check`: All checks passed.

A replay put together from the starter commit “Add the token rules”, with its excerpts trimmed.

In the starter, the file claims more than the check does. A line at the top of the token rules says `npm run check` enforces “everything from here down”, which takes in the naming pattern and the brand rule, and nothing checks those yet. Only two rules have a check behind them, colour values only in primitives and every layer pointing at the one below. **Read `AGENTS.md` under Files changed as well as the agent's summary**, because the next session reads only the file. If yours makes the same claim, merge it as it is and stay in step with the starter. The last prompt in this part gives both rules a check and corrects the line, and until then only your reading of each pull request catches a name like `color.fill.blue`.

## A third layer pays off only when a brand restyles one component

Adding component tokens such as `button.background` for every component from day one is common advice, and what it costs is the number of tokens. **In one library I built, the button alone had 116 tokens**, and the library had more than twelve hundred across 42 components, each of which had to be named, documented and kept true. More than a third of them pointed straight at a primitive, past the layer meant to carry the brand, and no check stopped it. Terrazzo, a tool that builds tokens like Style Dictionary, advises against component tokens altogether in its [style guide for design systems](https://terrazzo.app/docs/guides/styleguide/), and keeps to primitives and semantics.

Grove wants pill-shaped buttons, and its inputs and tags should keep their corners. **With two layers the only lever is `radius.control`**, which buttons, inputs and tags share, so pulling it rounds all of them. Answer the questions for your own project.

*An interactive figure sits at this point on the page. Three questions decide the layer count.*

- Will more than one brand or client use these components? Yes puts one file per brand in `tokens/brands/`, which is otherwise empty. It does not add a layer.
- Has a brand asked to restyle one component and leave the rest as they are? No means two layers.
- If yes, would the same change suit every component in that role, such as every control rounder? Yes means two layers, with the brand re-pointing that role in its own file. No means a component layer for that one component, in `tokens/components/`.

Three questions that decide whether a project needs a component layer, and the folders each answer leaves.

**Prompt** A component layer for Button

```text
Grove wants pill-shaped buttons. Cards, tags and inputs stay as they are.

Add a component layer for Button only, in tokens/components/button.json: background, background on hover, label colour and radius, each pointing at a semantic token. Make the demo button read these tokens. Then set Grove's button radius to the pill radius in tokens/brands/grove.json.

Update the check so component tokens may only point at semantics, and add the component layer to AGENTS.md.
```

*Replayed agent session, as built:*

> Grove wants pill-shaped buttons. Cards, tags and inputs stay as they are… (full prompt above)

- Created `tokens/components/button.json`: Four Button tokens, each pointing at a semantic.
- Edited `tokens/brands/grove.json`: One line more, so Grove's buttons take the pill radius.
- Edited `demo/demo.css, scripts/build.js, scripts/check.js, AGENTS.md`: The button reads its own tokens, the build writes them as references on :root and [data-brand], and the check and the rules know the new layer.
- Created `vite.config.js`: Vite's own build now goes elsewhere, so a token rebuild reaches the open demo.
- Ran `npm run build`: Wrote dist/tokens.css with 2 brands: grove, harbor
- Ran `npm run check`: All checks passed.
- Read `demo/index.html`: A blue button with 8px corners for Harbor, and a green pill for Grove, nested or not.
- Said: Only Button reads the new tokens, and only Grove changes one of them. Cards, tags and links still read semantics, so nothing else got rounder.

*Replayed agent session, if the build copied values:*

> Grove wants pill-shaped buttons. Cards, tags and inputs stay as they are… (full prompt above)

- Created `tokens/components/button.json`: Four Button tokens, each pointing at a semantic.
- Edited `tokens/brands/grove.json`: Grove's buttons take the pill radius.
- Ran `npm run build`: Wrote dist/tokens.css, with every value copied in.
- Ran `npm run check`: 1 check failed. It finds 43 copied tokens, --button-background among them.
- Read `demo/index.html`: Links follow each brand. Every button stays ink, on Harbor, Grove and the nested Grove card alike.
- Said: The brand re-pointed color.fill.brand, but button.background was already a copy of the old value, so it kept that value.

The first tab replays the session behind the starter commit “Add a component layer for Button”. The second replays the same steps against a build with references turned off.

Besides the component layer, the first tab creates `vite.config.js`, which the prompt never asked for. It moves Vite's own build out of `dist/`, so a token rebuild reaches the open demo without a restart, and your agent can add the same file if you ask for it. The second tab is the failure the references section warned about, and check 5 catches it.

The resolver below opens with two layers, as the starter stood before the component layer. Pick the button's corners, switch to Grove, then turn the component layer on, and the corners pass through `button.radius` to the pill while the tag keeps the corners it had.

*An interactive figure sits at this point on the page. A pricing card has two switches, brand and component layer, and a table follows each part of the card down the layers to its value, as the starter builds them at `part-3-tokens`.*

- Under Harbor the button fill reads `color.fill.brand`, which points at `color.blue.600` (#226ec5). Switching to Grove re-points that semantic at `color.green.600` (#1e8441). The button, the tag and the link move, because each reads a semantic the brand file re-points. The card's surface, border and text keep the same values.
- Turning on the component layer puts `button.background`, `button.label` and `button.radius` between the button and the semantics. Under Grove, `button.radius` points at `radius.pill`, so Grove's button becomes a pill and nothing else gets rounder.

Each part of the pricing card, followed from the token it reads down to its value.

**Two layers by default**

Chose: Primitives and semantics for everything, and component tokens only for a component a brand restyles on its own, Button here.

Trade-off: When a second component needs tokens of its own, someone has to notice and add its file.

*In Figma.* A Button collection with Harbor and Grove modes, aliased to the semantics. button/radius is radius/control in Harbor and radius/pill in Grove.

A component layer is a small collection of its own, aliased to the semantics. Grove's mode changes one row, and only the button component is bound to these variables. A Grove frame picks Grove in both the Semantics and the Button collection.

## A rule without a check holds only while someone reads every change

Two rules in the file still have no check, the naming pattern and the rule that no script names a brand, so the last prompt gives each of them one.

**Prompt** Check the naming pattern and brand names

```text
Two rules in AGENTS.md have no check yet. Add a check for each to scripts/check.js, failing with a plain message like the others:
1. a semantic colour is named color.<role>.<intent>, with an optional -state at the end. The role is surface, fill, text or border, the intent can be any name for a job, and no word in the name is a colour ramp, such as blue or green. Read the ramps from tokens/primitives/color.json, so a new one is covered too, and treat grey and gray as the same word.
2. no file in scripts/ or .github/, and no config file at the root such as package.json or vite.config.js, contains a brand name as a whole word. Read the brand names from the files in tokens/brands/, and never write one into the check itself.

Number the new checks 7 and 8, and leave the six that are there as they are, numbers included. Then change AGENTS.md so it says exactly which rules the check enforces. Break each rule once to show me its message, with a semantic colour named color.text.blue and a brand name in scripts/build.js, then put both back.
```

Beyond the two checks, the prompt asks the agent to break each rule once, because **a check you have never seen fail could be passing everything**. It keeps brand names out of the check itself, since `scripts/check.js` sits inside `scripts/` and would fail on its own words. And it counts grey and gray as one word, so a British spelling cannot slip a colour into a name.

*A replayed agent session plays at this point on the page:*

> Two rules in AGENTS.md have no check yet. Add a check for each to scripts/check.js… (full prompt above)

- Read `AGENTS.md`: It says npm run check enforces everything from the token rules down, which is not true of the naming pattern or the brand rule.
- Read `scripts/check.js, scripts/build.js`: Six checks, and a stop before checks 5 and 6 when dist/tokens.css is missing. The build reads the brands from the file names in tokens/brands/ and never lists one, so the new check can read them the same way.
- Edited `scripts/check.js`: Checks 7 and 8 go after check 6, and checks 1 to 6 are unchanged.
- Edited `tokens/semantics/color.json`: A semantic colour named for a colour, for now. It points at a primitive, so only the naming rule breaks.
- Ran `npm run build && npm run check`: Check 7 fails on the name, and the other seven pass.
- Ran `git checkout -- tokens/semantics/color.json`: The semantic colours are back as they were.
- Edited `scripts/build.js`: A brand name in the build for now, with a capital letter to test letter case.
- Ran `npm run build && npm run check`: The build still runs, and check 8 fails with the file, the line and the brand.
- Ran `git checkout -- scripts/build.js; npm run build && npm run check`: scripts/build.js is back as it was, and all eight checks pass.
- Edited `AGENTS.md`: The file no longer claims the check enforces everything. A last section lists the eight checks, numbered as in check.js, and names what nothing checks.
- Ran `npm run build && npm run check`: All eight checks pass, and only AGENTS.md and scripts/check.js have changed.
- Said: Check 7 fails when a semantic colour is not shaped color.<role>.<intent> with an optional state, uses a role other than surface, fill, text or border, or has a word that matches a ramp in tokens/primitives/color.json, with grey and gray counted as one word. Check 8 fails when a file in scripts/ or .github/, or a config file at the root, names a brand from tokens/brands/ as a whole word.
- Said: Both test breaks were undone with git, so neither is in the commit. Please confirm three choices the request left open. Brand names match in any letter case. Root config means dotfiles plus .json, .js, .mjs, .cjs, .ts, .yml, .yaml and .toml files, but not package-lock.json. Checks 7 and 8 sit after the dist/tokens.css gate, so they only run once npm run build has run.

A replay of the session behind the starter commit “Check the naming pattern and brand names”, with its excerpts trimmed.

The prompt left three choices open, such as whether `Harbor` with a capital counts as a brand name, and the agent makes each one and asks you to confirm it. The starter kept all three as the agent made them, so a yes keeps your repository in step with it. `AGENTS.md` now lists the eight checks under their numbers in `scripts/check.js`, and closes with what nothing checks, such as a raw size in the demo CSS, since check 1 only catches colours. Those stay with whoever reviews the pull request.

With the component layer and all eight checks in place, every message names what the check found and where.

| The check | The mistake it catches | What it prints |
| --- | --- | --- |
| 1. Colour values appear only in `tokens/primitives` | A hex pasted into a semantic, a brand file or the demo CSS | `tokens/brands/grove.json: color.fill.brand holds a colour value. Point it at a primitive instead.` |
| 2. Every token points one layer down | A semantic that points at another semantic, or a component token that skips to a primitive | `tokens/components/button.json: button.background points at {color.blue.600}, which is not a semantic token.` |
| 3. Demo CSS reads no primitives | A page that asks for a blue by name | `demo/demo.css reads --color-blue-600, a primitive. Use the semantic token for that job.` |
| 4. Brand files only set tokens that exist | A typo, or a brand adding a token of its own | `tokens/brands/grove.json: color.fill.brand-hovr is not a semantic or component token. Did you mean color.fill.brand-hover?` |
| 5. Every semantic and component token in `dist/tokens.css` is a `var()` reference | A build that copies values | `--color-fill-brand is baked to #2d2e30 under :root, [data-brand]. A brand that re-points what it reads will not reach it.` |
| 6. Semantic and component tokens are declared on `:root` and `[data-brand]`, before the brands | Tokens worked out once for the page, or a brand block that comes too early | `--color-fill-brand is declared under ":root" only. Anything that reads it inside a data-brand would keep the page's value.` |
| 7. Semantic colours are named `color.<role>.<intent>` after a job, not a colour | A colour in the name, a fifth role, or a name of the wrong shape | `tokens/semantics/color.json: color.text.blue names the colour blue. Name the job it does instead, as in color.fill.brand.` |
| 8. No script, workflow or root config file names a brand | A list of brands typed into the build, the workflow or a config file | `scripts/build.js:52 names the brand Harbor. Brands are the files in tokens/brands/, so read them from there instead.` |

The starter then moved `.nvmrc` to 24 in a commit of its own, so the check on GitHub runs the Node this part asked you to install. Make the same change in yours.

**Prompt** Run the check on Node 24

```text
Change .nvmrc to 24, so the check on GitHub runs Node 24 like this computer.
```

## Several tools build the same tokens, but they should be edited in one place

The starter uses Style Dictionary. Other tools build the same CSS, and the right one depends on where a team edits its tokens.

| Tool | Reads | Writes | Fits when |
| --- | --- | --- | --- |
| Style Dictionary | W3C token JSON | CSS, JavaScript, iOS, Android and more | One build should feed every platform. |
| Terrazzo | W3C token JSON | CSS, Sass, JavaScript, Swift, Tailwind and more | You want a tool built around the W3C format from the start. |
| Tokens Studio | Its own panel inside Figma | Token JSON, synced to a Git repository, as a folder of files on a paid plan | Designers edit tokens in Figma and own every change. |
| Figma | Variable collections, and W3C token JSON on import | JSON, one mode or a whole collection | Designers edit in Figma and someone exports after each change. |
| Plain CSS | Nothing | CSS variables, by hand | One brand, a small project, no build step. |

**Pick one place where tokens get edited**, because two copies that both get edited drift apart. When the repository holds them, the design tool is updated from it after each change. When a design tool holds them, an export writes the JSON. Figma imports token files in the W3C format as variables, and exports a collection's modes as JSON when you right-click the collection and choose Export modes. Its import takes colours in sRGB or HSL, sizes in pixels and one font name rather than a stack, and its Variables REST API, the route to a scripted sync, needs an Enterprise plan. Part thirteen builds a copy of the starter's tokens in the shape the import takes. Until then, make the collections by hand, as the panels in this part show.

At `part-3-tokens` the starter holds the layers, two brands, a component layer for Button and eight checks.

**The repository at part-3-tokens** · `design-system-starter`

```text
tokens/
  primitives/   raw values: colour ramps, sizes, fonts
  semantics/    jobs (color.fill.brand), each pointing at a primitive
  components/   button.json only: one brand restyles buttons on their own
  brands/       one file per brand, re-pointing semantics
  modes/        empty until part four
  locales/      empty until part five
scripts/
  build.js      Style Dictionary 5, keeping every reference as var()
  check.js      eight checks that fail with a plain message
.github/
  workflows/
    check.yml   runs the build and the check on every pull request
demo/           the same card for Harbor, Grove, and Grove inside Harbor
prompts/        the prompt behind each commit, in the starter only
AGENTS.md       how to work here, and the rules for tokens
vite.config.js  moves Vite's own build out of dist/, so a rebuild reaches the open demo
```

Part four gives every brand a dark mode from one more file, adds a contrast check that runs on every brand in light and dark, and turns a brand file into a template the next client can fill in.

## In this series

1. [A design system is a set of decisions, and the code is where they live](https://www.ahmedamr.com/writing/why-design-system-in-code.md)
2. [Decide the switches first, and the folders follow](https://www.ahmedamr.com/writing/design-system-repository-setup.md)
3. Design tokens, layered so a new brand costs one file (this part)
4. Dark mode and many brands (soon)
5. Right to left, and every language (soon)
6. The tokens after colour (soon)
7. Naming, versions and shipping tokens (soon)
8. Components for the web (soon)
9. From components to screens (soon)
10. Components for mobile, in React Native (soon)
11. Documentation people and agents can read (soon)
12. Teaching agents to build with the system (soon)
13. Shipping it, adopting it and keeping it alive (soon)

[^repository]: **Repository.** A project folder whose every change Git records, kept on GitHub so others can read and review it.
[^commit]: **Commit.** A saved change to a repository, with a note saying what changed and who made it.
[^rules-file]: **Rules file.** A Markdown file at the root of a repository that coding agents read before working in it, such as AGENTS.md.
[^pull-request]: **Pull request.** A request to bring a branch into the main line of a repository, where the change can be read, discussed and checked first.
[^design-token]: **Design token.** A design decision with a name, such as a colour, a size or a font, stored once so design and code read the same value.
[^css-custom-property]: **CSS custom property.** A named value in CSS, written --name and read with var(--name). Often called a CSS variable.
[^primitive-token]: **Primitive token.** A raw value named for what it is, such as color.blue.600. Nothing on screen reads it directly.
[^semantic-token]: **Semantic token.** A name for a job, such as color.fill.brand, that points at a primitive. Screens read these.
[^component-token]: **Component token.** A name for one part of one component, such as button.radius, that points at a semantic token.
[^dtcg]: **DTCG.** The W3C Design Tokens Community Group, which publishes the JSON format for token files that Style Dictionary, Terrazzo and Tokens Studio read.
[^oklch]: **OKLCH.** A way of writing a colour as lightness, chroma and hue, where the first number is how light the colour looks.
[^style-dictionary]: **Style Dictionary.** An open-source tool that turns token files into code: CSS variables, JavaScript, iOS and Android values.
[^branch]: **Branch.** A line of changes in a repository, kept apart from the main one until it is ready.
[^token-reference]: **Token reference.** A token whose value is another token's name in braces, such as {color.gray.900}, so it follows that token when it changes.
