Skip to content
Talk to an engineerRequest access

Connect your backend

Give your model your components as tools, and stream an answer your app can draw. Your model, your loop, your keys.

  • ⏱ 15 minutes
  • Tested on Node.js 24
  • Any provider
Let your coding agent set it upOne prompt: it finds your LLM route and chat UI, installs the SDKs and wires them in.
Claude CodeCodexCursorCopilotWindsurf
Set up useval (generative UI) in this app.

useval lets this app's AI assistant answer with interactive components that our team designs. The model never writes UI: it picks a published component and fills in its values with a tool call; the frontend draws it natively.

Work in this order and keep the app's existing behaviour intact:

1. Explore first. Find (a) the backend code that calls the LLM — provider (OpenAI, Anthropic, Vercel AI SDK or other), the chat route and the tool-calling loop — and (b) the frontend that renders assistant replies — React or Flutter. Tell me what you found and your plan before editing anything.

2. Install the SDKs. They ship as archives in the useval release download; ask me for the extracted sdk/ directory if it isn't in the repo.
Terminal window
npm install ./vendor/val-genui/val-genui-protocol-2.0.0.tgz ./vendor/val-genui/val-genui-server-1.0.0.tgz
  1. Create a client and a reply — once per request.

    app/api/chat.ts
    import { GenUi } from '@val-genui/server'
    const genui = new GenUi({ apiKey: process.env.GEN_UI_SECRET_KEY! })
    const ui = genui.reply({ client: request.headers.get('gen-ui-client') })

    The gen-ui-client header comes from your app’s SDK. It tells the server what this app can draw, so an older app never receives a component it can’t.

    baseUrl defaults to http://localhost:8080; when you deploy, pass your Val service’s address as new GenUi({ apiKey, baseUrl }).

  2. Add your components as tools, beside your own, in your provider’s shape.

    const tools = [...myTools, ...(await ui.tools('openai'))]
  3. Append the guidance to your system prompt.

    const system = `${mySystem}\n\n${await ui.guidance()}`
  4. Feed the model’s output through, then return the stream.

    ui.text(delta) // prose, as it streams
    if (genui.owns(call.name)) {
    const { forModel } = await ui.call(call) // drawn into its place
    messages.push({ role: 'tool', tool_call_id: call.id, content: forModel })
    }
    void ui.end()
    return ui.response()
✓ That’s the integration. Your app reads ui.response() and draws it — see React or Flutter.
import { openai } from '@val-genui/server/openai'
// Chat Completions
const { messages, rest } = await openai.chatToolCalls(ui, message.tool_calls)
// Responses API
const { outputs } = await openai.responsesCalls(ui, response.output)

messages are Val’s tool results; rest are your own calls, untouched.

Not the component. forModel is a short note — {"shown": "order-summary", "version": 3, …} — or {"error": "<the reason>"} when a call is refused, which the model fixes on its next turn. The markup never enters your model’s context.

ui.call never throws: a refusal removes the placeholder and returns ok: false.

ui.response() Server-sent events as a standard Response — Next.js, Hono, Bun, Deno, Workers. { format: 'ndjson' } for NDJSON.
ui.pipe(res) Node http, Express, Fastify (reply.raw).
ui.events() The events themselves, for a stream you already have.
await ui.end() Waits for every component, closes the stream, returns the finished reply — the thing to store.
messages.push({ role: 'user', content: genui.actionMessage(action) })
const genui = new GenUi({ apiKey, genUiLevel: 'medium' })

low uses components selectively, medium (default) whenever one clearly fits, high throughout substantive answers. Every level respects text-only requests.