Library, prompt, stream, render
OpenUI is a loop with four parts: a component library you write, a system prompt generated from it, a model that streams OpenUI Lang, and a Renderer that turns each chunk into interface. Then the loop does something unusual: it lets the interface run on its own, without the model.
By the end of this lesson you can
- trace one request from the user's question to a rendered interface, naming what crosses each boundary;
- explain why the same library feeds both the prompt and the Renderer;
- separate generation (the model writes the interface and its wiring, once) from execution (the browser runs it);
- read the two pieces of code that matter: the server route and the
<Renderer>.
One request, end to end
Four parties take part: the user, the browser (where the Renderer runs), your server, and the model. Watch what each one sends, and notice where the model is absent.
The OpenUI loop
- You write a component library: each component is a Zod schema plus a renderer. The same library is imported in two places, the server (to write the prompt) and the browser (to parse and render).
- The server turns the library into the system prompt: syntax rules, one signature line per component, and rules for streaming.
library.prompt()writes it from the library;generateSystemPrompt()writes the same text from an exported spec. - The user asks a question. The browser posts the prompt (and earlier messages) to your server. It never sends the system prompt: a client could rewrite it.
- Your server calls the model with the system prompt and the messages. Any model works; the route never names a provider.
- The model streams OpenUI Lang, and the server passes it to the browser as plain text chunks. No JSON envelope, no HTML: just the program as it is written.
- After every chunk, the browser hands the whole text so far to
<Renderer response={text} isStreaming />. The streaming parser parses the new part, renders every complete statement with your components, and the interface grows line by line. - The user types a city or clicks a button. A Query runs through the Renderer's toolProvider, which calls your API. State changes and re-fetches happen in the browser. The model is not called.
- Only an action that talks to the assistant reaches your app's
onActionas acontinue_conversationevent, and your app starts a new turn. That is@ToAssistant("…"), or aButtonwith no action, which sends its own label. Those are the only paths back to the model.
Key idea
One library, two jobs. It writes the model's instructions on the server and checks the model's output in the browser. If the two copies drift apart, the model is told about a component the Renderer does not know, and that part of the interface is dropped with an unknown-component error.
Generation and execution are separate jobs
In most AI apps, every click on a generated interface goes back through the model: it re-reads the context, calls tools and writes a new answer. Each interaction costs tokens and seconds. OpenUI splits the work in two.
- Generation happens once. The model writes the interface and its wiring: $variables for state,
Querystatements that name a tool and its arguments,Mutationstatements for writes, and button actions. - Execution happens in the browser, as often as the user likes. The runtime evaluates expressions, calls tools through the
toolProvider, re-fetches a Query when a$variablein its arguments changes, and updates the interface. No model round trip.
Who works when
- Generation: the model writes the whole program once, including
$city, aQuery("get_weather", {city: $city}, …)and the inputs bound to$city. This is the only part that costs model tokens. - The stream ends, and the browser runtime runs the Query through the
toolProvider. The Query's third argument, its default value, is shown until the result arrives. - The user types another city.
$citychanges, the Query's arguments change with it, and the runtime fetches again. The model does not hear about it. - Only a button whose action is
@ToAssistant("…")goes back to the model, as a new conversation turn.
OpenUI rule
"OpenUI separates generation from execution. The LLM generates the interface once. After that, the UI runs on its own: fetching data, handling state, and responding to user actions without any LLM involvement." In the docs' words: the LLM generated the wiring; the runtime executes it.
Common misconception
"Every click on a generated interface calls the model." Not in OpenUI. @Set, @Reset and @Run (run a Mutation or re-fetch a Query) are action steps, covered in Actions and onAction. They are handled inside the runtime and never reach onAction. Only @ToAssistant starts a model turn (a Button with no action counts too: it sends its label). @OpenUrl and custom actions reach your app without calling the model at all.
Watch a stream become an interface
The player replays a recorded model response into the simulator, character by character. Three panes show the same moment three ways: the raw text so far, what the parser made of it, and the rendered interface. Start with the static revenue answer, then switch to the weather answer, which has state and a Query.
Things to look for
- In the Parse result pane,
statementCountclimbs one statement at a time, andunresolvedlists names the root uses whose lines have not arrived yet. The list empties as they stream in. - The revenue answer never calls a tool. Its data is written into the program.
- The weather answer's Query does not run while the text is streaming. When the stream ends, the tool-call counter goes up by one.
- After it finishes, change the City field or the Units select. Each change re-fetches, so the tool counter goes up. The model counter does not.
The simulator runs the Query when the stream ends, as the real Renderer does, but it calls a fake get_weather in this page with a short delay. It also draws grey placeholders where a reference has not streamed in yet; the real parser simply leaves those parts out until they arrive.
The two pieces of code that matter
The server side is short. It receives a prompt, calls a model with the system prompt built from your library, and streams the text straight back. The model is a parameter: pass any AI SDK model, chosen on the server from a list you allow.
The browser side is a <Renderer> that receives the growing text. Everything after generation hangs off its props: the toolProvider that runs Queries and Mutations, onAction for the actions that leave the runtime, onStateUpdate to save state, and onError for errors you can send back to the model.
OpenUI rule
toolProvider "handles Query() / Mutation() tool calls. Pass a function map or an MCP client." It runs in the browser, next to the Renderer, so point its functions at your own API routes and authorize them there.
Break it: forget the toolProvider
The weather program asks for Query("get_weather", …), but the model cannot run anything; only the toolProvider can. Suppose the app renders it with no toolProvider at all. Predict first: does the interface show an error, crash, or something else?
What should happen
Nothing visibly fails. With toolProvider missing, the runtime never calls the tool, reports no error, and the Query keeps showing its default value: an empty city and 0°. The tool counter stays at 0 and changing the city does nothing. This is the real @openuidev/lang-core 1.0.1 behaviour, and it is why a "frozen" dashboard is worth checking for a missing provider first.
A provider that exists but lacks the tool's name behaves differently: the Query fails with tool-not-found, which onError does report.
Quick check
The weather interface has finished streaming. The user changes the city three times. How many model calls does that cause?
$city, the bound input and a Query that uses $city. When $city changes, the browser runtime fetches again by itself.Where should the system prompt be built in a production app?
library.prompt() or generateSystemPrompt() from a spec. The model never sees the Renderer.What does the server send the browser while the model is generating?
Recap
- The loop: library (Zod + components) → system prompt on the server → model streams OpenUI Lang → Renderer parses every chunk → interface.
- The same library writes the prompt and validates the output, so the model can only use components the browser can draw.
- Generation happens once: the model writes the interface and its wiring. Execution happens in the browser: state, Queries, Mutations and most actions run with no model round trip.
- Only
@ToAssistant, or aButtonwith no action, goes back to the model. Without atoolProvider, Queries silently keep their defaults.