ChaiBuilder Logo

AI Setup

ChaiBuilder ships with a built-in AI assistant. Getting it running is fast: add credentials for one AI provider to your environment and the assistant is ready - no code changes required.

Model ids are plain provider/model strings (for example google/gemini-3-flash). Whichever provider you configure is the one that serves them.

The fastest path: a gateway key

AI_GATEWAY_API_KEY=your_key_here

That's it. With no other AI provider configured, requests fall back to the Vercel AI Gateway, which resolves provider/model ids across many model vendors without wiring each one up individually. Nothing to install.

Provider options

Four routes are supported out of the box. Each is activated purely by environment variables.

Provider Environment variables Package to install
Vercel AI Gateway (fallback) AI_GATEWAY_API_KEY none
OpenRouter OPENROUTER_API_KEY @openrouter/ai-sdk-provider
OpenAI-compatible OPENAI_COMPATIBLE_BASE_URL @ai-sdk/openai-compatible
Cloudflare Workers AI CLOUDFLARE_ACCOUNT_ID + CLOUDFLARE_API_TOKEN workers-ai-provider

The three non-gateway packages are optional peer dependencies, imported lazily only when their credentials are present - an unused provider costs you nothing.

OpenRouter

OPENROUTER_API_KEY=your_key_here

# Optional - attribution shown in your OpenRouter dashboard
OPENROUTER_APP_NAME=My Site
OPENROUTER_APP_URL=https://example.com
pnpm add @openrouter/ai-sdk-provider

OpenRouter uses the same provider/model slug convention as the Vercel AI Gateway, so the built-in model catalogue works unchanged.

OpenAI-compatible endpoints

One adapter covers everything exposing an OpenAI-style /v1 API - Groq, Together, Fireworks, DeepInfra, Hugging Face Inference Router, Ollama, LM Studio, vLLM, and self-hosted gateways.

OPENAI_COMPATIBLE_BASE_URL=https://router.huggingface.co/v1   # required - activates this provider
OPENAI_COMPATIBLE_API_KEY=your_key_here                        # optional - omit for keyless local servers
OPENAI_COMPATIBLE_NAME=huggingface                             # optional - label, defaults to "openai-compatible"
pnpm add @ai-sdk/openai-compatible

Model ids pass straight through, so use whatever the endpoint expects (for example meta-llama/Llama-3.3-70B-Instruct, or llama3.1 on a local Ollama). Set those ids in ai.models - see Customization.

Cloudflare Workers AI

CLOUDFLARE_ACCOUNT_ID=your_account_id
CLOUDFLARE_API_TOKEN=your_api_token
pnpm add workers-ai-provider

This uses the REST transport, so it works in any Node or serverless runtime. Workers AI uses @cf/... model ids (for example @cf/meta/llama-3.1-8b-instruct) rather than the gateway's slugs, so configure those in ai.models.

Running inside a Cloudflare Worker? Bind env.AI directly using the ai.provider escape hatch instead - see Customization.

Which provider wins

If more than one is configured, resolution order is:

  1. globalThis.AI_SDK_DEFAULT_PROVIDER, if your app set it - ChaiBuilder never overwrites it.
  2. ai.provider in chaibuilder.config.ts - an explicit provider instance or factory.
  3. The first provider whose credentials are present: your custom ai.providers first, then the built-ins in order - OpenRouter → OpenAI-compatible → Cloudflare.
  4. Nothing configured - the Vercel AI Gateway fallback.

So setting OPENROUTER_API_KEY alongside AI_GATEWAY_API_KEY means OpenRouter serves the requests. Pick one deliberately.

Verify it works

Open a page in the builder and launch the AI panel. The model picker lists the models from ai.models (the built-in default model is google/gemini-3-flash). Ask it to change something on the page - if the request completes, your provider is wired correctly.

If it fails, the usual causes are a missing peer-dependency package, a model id your provider doesn't recognize, or a second provider's env var quietly taking precedence.

When to go further

The steps above give you the assistant with its default prompts, models, and provider. To change how it behaves - restrict the model list, adjust action prompts, add your own tools, or route through a provider with no built-in adapter - see AI - Customization. Those changes involve code, because the core AI is built on the open-source Vercel AI SDK.

© ChaiBuilder. All rights reserved.