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, examples and toolExamples end 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

The prompt, top to bottom What you passed Built-in Functions Query — Live Data Fetching Mutation — Write Operations Action — Button Behavior Interactive Filters Forms Data Workflow Available Tools Examples Edit Mode Inline Mode your additionalRules Preamble: "You are an AI assistant that…" Syntax Rules 1–7 Hoisting & Streaming (CRITICAL) Important Rules Final Verification Component Signatures: one line each + rules 8–16 Built-in Functions Query — Live Data Fetching Mutation — Write Operations Interactive Filters Forms Data Workflow Available Tools: your ToolSpecs Action — Button Behavior Examples (examples, then toolExamples) Edit Mode Inline Mode - your additionalRules, one per line library.prompt() library.prompt({tools: [get_weather]}) + a component with anaction: ActionExpressionprop (Button) + examples: [...]+ toolExamples: [...] + editMode: true+ inlineMode: true + additionalRules: ["Prefer WeatherCard…"] 2,822 charactersabout 706 tokens (lab 02, real lang-core)6 sections, layout only UnitToggle(name: string,value?: $binding<"celsius" | …>)— Celsius / Fahrenheit switch 12,496 charactersabout 3,124 tokens (lab 02)one tool more than quadruples it each option adds its own sectioninto its fixed slot; nothing elsemoves after Final Verification,not inside Important Rules:the last thing the model reads always present from your library and your rules tools on (toolCalls, bindings, built-ins) a component has an action prop examples, editMode, inlineMode
  1. 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.
  2. 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.
  3. Passing tools turns on toolCalls, which turns on bindings, 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.
  4. 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 stock Button, or an action prop restricted to certain steps (for example typed @OpenUrl | @ToAssistant). It also appears when the library defines custom actions with defineAction; 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.
  5. Examples go in their own slot after Hoisting & Streaming. When both examples and toolExamples are set, both are included.
  6. editMode and inlineMode each add one section, just before Important Rules. Edit mode is covered in Patch, don't regenerate.
  7. Your additionalRules are 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

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

Where your own words go

Four options carry your own text into the prompt. Each lands in one place:

OptionWhere it appears
preambleReplaces the first paragraph ("You are an AI assistant that responds using openui-lang…").
examplesThe Examples section, after Hoisting & Streaming.
toolExamplesThe same Examples section, after examples. Both are included when both are set.
additionalRulesOne 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

Build time openui generate library.spec.json Your server generateSystemPrompt({ library: spec, promptOptions: { tools } }) per request, no React Model system: your prompt user: the message Browser sends { prompt, messages } { system: "…" } from the browser: anyone can rewrite it ✕
  1. At build time, openui generate ./src/library.ts --out system-prompt.txt writes a prebuilt prompt and, next to it, system-prompt.spec.json. With --spec it writes only the spec. In code, library.toSpec() returns the same spec.
  2. 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.
  3. 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.
  4. 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?

  • Only toolCalls, because the other flags must be set by hand
  • toolCalls, bindings and builtinFunctions, through their defaults
  • None: every flag must be set explicitly, even with tools given
The defaults cascade: tools present turns 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?

  • Inside the Important Rules section, as extra bullet lines
  • At the top, right after the preamble and before the rules
  • After Final Verification, as the last lines of the prompt
lang-core 1.0.1 appends them as bullet lines after Final Verification. Lab 02 shows it, and the explorer above does too.

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?

  • Validate that system starts with the OpenUI preamble and reject anything else
  • Build the prompt on the server with generateSystemPrompt and accept only the user's message
  • Move library.prompt() into a web worker so it runs off the main thread
Anything in the request body is under the caller's control. A prefix check is easy to satisfy while changing everything after it. The server owns the prompt; the CLI's spec lets it build one without importing React.

Recap