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

  1. 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).
  2. 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.
  3. 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.
  4. Your server calls the model with the system prompt and the messages. Any model works; the route never names a provider.
  5. 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.
  6. 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.
  7. 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.
  8. Only an action that talks to the assistant reaches your app's onAction as a continue_conversation event, and your app starts a new turn. That is @ToAssistant("…"), or a Button with 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.

Who works when

Model Browser runtime time writes UI + wiring, once tokens are spent here Query runs get_weather("Pune") re-fetch $city = "Tokyo" model idle: no call, no tokens @ToAssistant click new turn
  1. Generation: the model writes the whole program once, including $city, a Query("get_weather", {city: $city}, …) and the inputs bound to $city. This is the only part that costs model tokens.
  2. 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.
  3. The user types another city. $city changes, the Query's arguments change with it, and the runtime fetches again. The model does not hear about it.
  4. 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.

1model calls (the stream itself)
0tool calls made by the browser

Things to look for

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?

  • Three, one model call for each change of city
  • One, to regenerate the Query with the new city
  • None: the runtime re-runs the Query via the toolProvider
The model wrote the wiring once: $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?

  • On the server, from the library or its exported spec, and never accepted from the client
  • In the browser, with library.prompt(), and sent with each request
  • Nowhere: the model learns the components from the Renderer
The prompt decides what the model may write, so a client must not be able to replace it. Build it on the server with 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?

  • Rendered HTML for each component
  • The OpenUI Lang text itself, as plain text chunks
  • A JSON tree that the server parsed from the model's output
Parsing and rendering happen in the browser. The server streams the program as plain text, and the Renderer updates its parse after every chunk.

Recap