The generated system prompt
The model learns OpenUI Lang from one string: the system prompt that your library writes. It is built from fixed sections in a fixed order. Your components fill one of them, and each PromptOptions flag switches others on.
By the end of this lesson you can
- list the sections
library.prompt()always writes, and the option that adds each of the others; - read a component signature line and say where each part comes from;
- predict where
additionalRules,examplesandtoolExamplesend up; - build the prompt on the server from a library spec, and explain why a prompt sent by the browser must never be trusted.
One call, one string, fixed slots
library.prompt(options) returns a plain string. It is not a template you edit: it is assembled from a known list of sections, always in the same order. A few sections are always present. The rest appear only when an option, or something in your library, asks for them. The figure builds the prompt for a small weather library, adding one option at a time. The empty dashed slots are the sections that could appear; a filled box is one that is in the prompt.
Assembling the prompt
- With no options,
prompt()writes six parts: the preamble, Syntax Rules 1 to 7, Component Signatures, Hoisting & Streaming, Important Rules and Final Verification. For the lab's weather library that is 2,822 characters, about 706 tokens. It describes layout only: no data, no state. - Component Signatures is the only section your library writes. Each component becomes one line, generated from its Zod schema and description. The next section of this lesson takes one apart.
- Passing
toolsturns ontoolCalls, which turns onbindings, which turns on the built-in functions. Seven sections fill in, and the syntax rules grow from 7 to 16 (state, operators, member access, ternary). The same library now produces 12,496 characters, about 3,124 tokens. - The Action section has nothing to do with tools. It appears when a component in the library has a prop typed
ActionExpression, such as the stockButton, or an action prop restricted to certain steps (for example typed@OpenUrl | @ToAssistant). It also appears when the library defines custom actions withdefineAction; then a short Action section lists them. The lab's weather library has none of these, so its prompt has no Action section even with tools. - Examples go in their own slot after Hoisting & Streaming. When both
examplesandtoolExamplesare set, both are included. editModeandinlineModeeach add one section, just before Important Rules. Edit mode is covered in Patch, don't regenerate.- Your
additionalRulesare appended as bullet lines after Final Verification, at the very end of the prompt. They are the last thing the model reads before the conversation starts.
Key idea
The flags cascade. tools sets the default for toolCalls; toolCalls sets the default for bindings; either of them sets the default for builtinFunctions. Any of the three can be set explicitly to override its default.
Reading a signature line
The model never sees your React code or your Zod schema. It sees one line per component, and that line is the whole contract. These are the real lines lab 02 prints for its weather library:
Stack(children: any[]) — Vertical layout WeatherCard(city: string, temp: number, sky: string, units?: "celsius" | "fahrenheit") — Current weather for one city Forecast(days: Day[]) — A row of forecast days Day(date: string, min: number, max: number) — One forecast day UnitToggle(name: string, value?: $binding<"celsius" | "fahrenheit">) — Celsius / Fahrenheit switch
- Argument order is the key order of the component's
z.object. Arguments are positional, so reordering the keys changes the language. ?marks an optional argument. The prompt tells the model optional arguments may be omitted from the end, so required props belong first.- Types are written TypeScript-style: enums become string unions,
z.array(Day.ref)becomesDay[]. $binding<type>marks a reactive prop: it accepts a$variablefor two-way binding (next module).ActionExpressionmarks an action prop, which accepts@ToAssistant,@Runand the other steps.- After the dash is the component's
description. It is the only place to tell the model when to use the component, so write it as an instruction.
Watch out
A library with a reactive prop gets this line even with no options: "Props marked $binding<type> accept a $variable reference for two-way binding." But Syntax Rule 8, which explains how to declare $varName = defaultValue, only appears when bindings is on. Without tools, a library like this one should pass bindings: true, or the model is shown a binding it was never taught to declare.
Explore the prompt
Toggle the options and watch the section list change. Each row shows where the section comes from and its estimated size; open a row to read its text. Sections that just appeared are outlined. The library is the course's playground library: a subset of the stock components plus WeatherCard and UnitToggle, with the stock examples and rules. The tools are the five fake tools used across the course.
In this simulator, long section bodies are abridged and marked "[abridged in this simulator]", and tokens are estimated as characters divided by four. The headings, their order and the rules for when each appears match @openuidev/lang-core 1.0.1. For real sizes, run lab 02: cd openui && node 02-library/prompt.mjs.
Things to look for
- Turn on tools: seven sections appear and Syntax Rules grows. Then set toolCalls to off while keeping the tools.
- Turn tools off and set bindings to on: Built-in Functions appears, but no Query or Mutation section.
- Turn on additionalRules and open Final Verification: your rule is at its end.
- Turn off examples: the library's default examples disappear. Passing
examples: []is how you drop them.
Where your own words go
Four options carry your own text into the prompt. Each lands in one place:
| Option | Where it appears |
|---|---|
preamble | Replaces the first paragraph ("You are an AI assistant that responds using openui-lang…"). |
examples | The Examples section, after Hoisting & Streaming. |
toolExamples | The same Examples section, after examples. Both are included when both are set. |
additionalRules | One bullet line each, after Final Verification, at the very end. |
A library usually ships its own options. The stock library exports openuiPromptOptions with its examples and rules; to add rules for your own components, spread it and extend its additionalRules, as the reference library does:
OpenUI rule
The built-in functions are listed only when toolCalls or bindings is on, or when you pass builtinFunctions: true. A library's own custom functions (defineFunction) are always listed.
Build it on the server
library.prompt() runs wherever the library is imported, and the library imports React components. A backend should not need them, so OpenUI splits the job in two: at build time, the CLI turns the library into a serialized spec; at request time, generateSystemPrompt turns the spec plus options into the prompt. Lab 02 checks that both paths give the identical string.
Where the prompt is built
- At build time,
openui generate ./src/library.ts --out system-prompt.txtwrites a prebuilt prompt and, next to it,system-prompt.spec.json. With--specit writes only the spec. In code,library.toSpec()returns the same spec. - At request time, the server calls
generateSystemPrompt({ library: spec, promptOptions }). It needs no React, so it runs in any Node, edge or serverless backend, and it can add per-request options such as the tools this user may call. The browser sends only the user's message. - The server sends its own prompt as the system message, with the conversation, to any model. The route never takes the system prompt from the request body.
- If the route accepted a system prompt from the browser, anyone with developer tools could replace it: drop the rules, invent tools, or turn your endpoint into a general-purpose model proxy billed to you.
OpenUI rule
Use the CLI when the prompt is the same for every request. Use generateSystemPrompt with the spec when the prompt needs tools, editMode, inlineMode or other per-request options. library.prompt() is the frontend shorthand: convenient, but it imports React components.
Key idea
The system prompt is configuration, owned by the server, like a database query. Treat a prompt that arrives in a request body the way you would treat SQL that arrives in one. This course's simulator builds prompts in the page only so you can read them.
Break it
Predict first, then set the options in the explorer above and check.
Tools without toolCalls. Pass the five tools but set toolCalls to off. Does the model still learn how to call them?
What should happen
Available Tools is still listed, because tools were passed, but Query, Mutation, Data Workflow and Built-in Functions are gone, and Syntax Rules is back to 7 rules. The model is told the tool names but never shown Query(...). Real lang-core does the same: lab 02's library gives Syntax Rules, Component Signatures, Available Tools, Hoisting & Streaming, Important Rules, Final Verification.
A rule that should be "important". Add additionalRules. Do they appear under Important Rules?
What should happen
No. Important Rules holds one fixed line. Your rules are appended after Final Verification, as the final lines of the prompt. Write them so they still make sense there.
State without tools. Leave tools off, set bindings to on. Which sections appear?
What should happen
Built-in Functions appears (bindings turns it on by default) and Syntax Rules grows to 16, so the model can declare $variables and write expressions. There is still no Query or Mutation section and no Interactive Filters, which needs both toolCalls and bindings.
Quick check
You call library.prompt({ tools: specs }) and set nothing else. Which flags are on?
toolCalls, toolCalls turns on bindings, and either turns on the built-in functions. editMode and inlineMode default to off.Where do your additionalRules appear in the generated prompt?
Your chat endpoint accepts { system, prompt } from the browser so the prompt can be built with library.prompt() on the client. What is the fix?
Recap
library.prompt(options)builds one string from fixed sections in a fixed order. Six are always present; tools, action props, examples,editModeandinlineModeadd the others.toolsdefaultstoolCallson, which defaultsbindingson, which defaults the built-in functions on. Lab 02: 2,822 characters without tools, 12,496 with one.- A signature line is
Name(arg: type, opt?: type) — description, in Zod key order, with$binding<…>for reactive props. additionalRulesland after Final Verification;examplesandtoolExamplesshare the Examples section.- Build the prompt on the server:
openui generatefor a spec,generateSystemPromptper request. Never take a system prompt from the browser.