Building generative UI with an AI agent
Building generative UI with an AI agent means giving a model a catalog of your components, a tool it calls to return a UI spec, and the live state of the visit to decide from. Your code validates the spec, renders it or falls back to a default, and measures the result against visitors who saw the fixed page.
What does building generative UI with an AI agent involve?
Building generative UI with an AI agent means letting a model choose what a screen shows on each request, from components you built, based on what is true about the visit. The model returns a structured description of the screen, and your app checks it and draws it. If you are new to the idea, what is generative UI? covers it first; this page is the build.
The work splits into six steps:
- Define a component catalog.
- Give the agent a tool whose input is a UI spec.
- Supply the context it decides from, which is the live state of the visit.
- Validate the spec, then render it.
- Fall back to a default when anything fails.
- Measure against a holdout that sees the fixed page.
The model is the easy part to swap. Most of the effort, and most of the failures, sit in steps 3 to 6.
Which architecture should the agent use?
Before step 1, decide how much of the screen the model is allowed to produce. There are three common answers, and they trade flexibility for control.
| What the model returns | Flexibility | Main risk | Where control sits | Fits when |
|---|---|---|---|---|
| Markup (HTML or JSX it writes) | Highest: any layout | Off-brand, inaccessible or unsafe output; script injection; nothing to validate against | Review after the fact, sandboxing | Internal tools and prototypes, rarely customer-facing pages |
| A spec built from your catalog | High within the catalog: new combinations, new props | A valid spec that is a poor choice; props that pass the schema but read badly | The catalog and its schema, checked before render | Pages with several slots and cases nobody wrote a rule for |
| A pick from prebuilt variants | Lowest: one of N screens you designed | A wrong pick, which still looks finished | The variants themselves; every outcome was reviewed | A single slot with a few known answers, or a first release |
The middle row is where most teams land, and it is the one this page follows. Libraries such as json-render from Vercel are built for it. The last row is closer to classification than generation: the verdict panel on this site shows a model choosing from a fixed set of answers about a live visit, with its confidence in each.
Step 1: define a component catalog
The catalog is the list of components the agent may use, each with a name, a one-line description the model reads, and a schema for its props. Write descriptions in terms of when to use the component, since that is the decision the model is making: “PricingCallout: answers a cost question in place; use when the visitor has looked at pricing this visit.”
Keep it small at first. Three or four components per slot, plus a default, gives the model real choices without giving it room to wander. Every component in the catalog should already pass your accessibility and design review on its own, so any combination the agent returns is made of reviewed parts.
Step 2: give the agent a tool that returns a UI spec
Make the spec the input to a tool, so the model has to fill a schema rather than describe a
screen in prose. A library-neutral definition in JSON Schema looks like this. The field
holding the schema is input_schema in Anthropic’s API, parameters in OpenAI’s, and
inputSchema in the Vercel AI SDK:
{
"name": "compose_hero_slot",
"description": "Choose the component for the hero slot of the product page. Use only components from the catalog.",
"input_schema": {
"type": "object",
"properties": {
"component": {
"type": "string",
"enum": ["ComparePanel", "PricingCallout", "ReviewStrip", "DefaultHero"]
},
"props": { "type": "object" },
"reason": { "type": "string", "description": "One sentence on why, for the log. Not shown." }
},
"required": ["component", "props", "reason"]
}
}
The enum is the catalog, enforced by the schema. The reason field is for your logs.
Your code turns the tool call into the spec your renderer reads. In the shape json-render
uses, a root naming the first element and a flat map of elements, each with a type
from the catalog and its props (see the json-render docs):
{
"root": "hero",
"elements": {
"hero": {
"type": "PricingCallout",
"props": { "plan": "team", "headline": "What the team plan includes" }
}
}
}
Step 3: supply the context it decides from
An agent given the route and the product id picks for the average visitor, because that is all it can see. To pick for this visitor it needs the live state of the visit: what they viewed, what they skipped, what they searched, how many times they came back to one page. Why generative UI needs real-time state makes the case; here is what it looks like in practice.
Define each piece of state as an attribute, a value computed from events about one key. The
PricingCallout decision above needs to know how often this visitor has opened pricing this
visit, and which products they have compared. In the Snowplow Signals Python SDK:
from snowplow_signals import (
Attribute, Event, EventProperty, StreamAttributeGroup, domain_sessionid,
)
pricing_view = Event(name="pricing_view", vendor="com.example", version="1-0-0")
product_view = Event(name="product_view", vendor="com.example", version="1-0-0")
pricing_views_session = Attribute(
name="pricing_views_session",
type="int32",
events=[pricing_view],
aggregation="counter",
default_value=0,
)
products_viewed_session = Attribute(
name="products_viewed_session",
type="string_list",
events=[product_view],
aggregation="unique_list",
property=EventProperty(vendor="com.example", name="product_view", major_version=1, path="product_id"),
)
hero_slot_context = StreamAttributeGroup(
name="hero_slot_context",
version=1,
attribute_key=domain_sessionid,
owner="you@example.com",
attributes=[pricing_views_session, products_viewed_session],
)
Pass the values into the agent’s prompt as structured data beside the catalog. Two pricing
views and three products in the list is a reason to pick PricingCallout or ComparePanel
that the model can state, and that you can check later.
Step 4: validate the spec, then render it
Treat the tool call as untrusted input. Before anything reaches the page, check that:
- the JSON parses and matches the tool schema;
- every
typeexists in the catalog and itspropspass that component’s own schema; - strings the visitor will read stay within the length the design allows;
- any id in the props, such as a product or a plan, exists and is one this visitor may see.
Then render. Only the renderer touches the DOM, so the model never writes markup.
Step 5: fall back safely
Every request needs a screen even when the agent does not deliver one. Render the default
component when the call times out, when validation fails, when the context read fails, or
when the model returns DefaultHero itself. The visitor should not be able to tell a
fallback from a deliberate choice. Count fallbacks by cause, because a rising rate is usually
the first sign that a prompt, a catalog change or a dependency has broken.
Step 6: measure against a holdout
Keep a share of traffic on the fixed page and compare outcomes between that group and the visitors who got a generated screen. Each generated screen is close to unique, so compare the whole setup (catalog, prompt, model and context) against the fixed page, not one screen against another. Measuring generative UI argues for the same unit. Generative UI vs static A/B testing covers how this differs from a test with two fixed arms.
What guardrails does a generated interface need?
- A latency budget. The context read, the model call and validation all sit before the first paint. Give the agent a fixed time and fall back when it runs out, or render the default first and swap the slot in when the spec arrives.
- Catalog constraints. The schema limits which components appear. Limit the number of slots the agent may fill, and keep anything legal or price-bearing out of free-text props.
- Accessibility. A screen assembled from accessible components can still fail: a heading order that skips levels, two landmarks with one name, focus that jumps when a slot swaps in. Test the combinations, not only the parts.
- Logging the decision. Record the spec, the
reason, the model and prompt version, the context values the agent read, and whether it fell back. Without that, a bad screen cannot be explained and the holdout cannot be broken down.
Where Signals fits
Step 3 is the one most teams have no ready answer for, because the state has to be computed from the visitor’s events as they happen and be readable on the request. You can build that on your own event stream, or use a service for it. Snowplow Signals is one option: it collects behavioral events, computes attributes like the ones above, and serves them as JSON over a REST API, the SDKs or an MCP server. The catalog, the agent and the renderer stay yours. For more on the pattern, see the generative UI topic.
See it on your own traffic
Add the SDK, define one attribute, and see an attribute updating against your own traffic in the first session. The free tier runs on the real engine: no card, no sales call.