English

Developer guide Internals

LLM router

The model catalog, how the router picks a model, and how to add a provider.

On this page

The router lets one canvas use many AI providers. It lives in server/src/llm/: catalog.ts reads the model catalog, router.ts orders the candidates, providers.ts streams from one provider, and vault.ts stores server keys.

The catalog

The catalog is data, not code. The server reads server/llm/catalog.json, or the file in ARCHIPELEGO_LLM_CATALOG. It re-reads the file when its modification time changes, so you can update models without a restart. When the file is missing, a built-in fallback offers three Claude models through the Anthropic SDK.

ts
export type Tier = 'flagship' | 'balanced' | 'fast';

export interface ProviderDef {
  id: string;
  label: string;
  baseUrl: string;
  openaiCompatible: boolean;
  /** OpenAI-compatible base URL when it differs from baseUrl (Gemini, DashScope). */
  compatBaseUrl?: string;
  keyUrl?: string;
  notes?: string;
  /** Name of the output-limit field on /chat/completions. */
  maxTokensParam?: 'max_tokens' | 'max_completion_tokens';
  /** Extra fields a key needs besides the secret itself (Qwen: the workspace id in its URL). */
  keyFields?: { id: string; label: string; placeholder?: string }[];
  /** Extra headers some providers require. */
  headers?: Record<string, string>;
}

export interface ModelDef {
  id: string;
  provider: string;
  label: string;
  tier: Tier;
  reasoning?: boolean;
  context?: number;
  verified?: boolean;
  note?: string;
}

Models whose provider is not in the catalog are dropped when the file loads.

Roles

Each request has a role. The role decides the tier order and the effort.

Role Used by Tier order Effort
architect @architect in the chat, and feature drafts flagship, balanced, fast medium
explain Explain flagship, balanced, fast medium
quick short replies fast, balanced, flagship low

Ordering candidates

orderCandidates builds one ordered list of model ids from the canvas's AIConfig:

ts
export interface AIConfig {
  mode?: 'auto' | 'fixed';
  fixed?: string;
  roles?: Partial<Record<Role, string>>;
  fallback?: string[];
}
  1. The fixed model, when mode is fixed.
  2. The model chosen for this role in roles.
  3. The fallback list, in the order the room set.
  4. Every catalog model, tier by tier in the role's tier order, and inside a tier by provider preference: anthropic, openai, google, moonshot, xai, mistral, qwen, deepseek.

Duplicates are removed, and models whose provider has no key are dropped. In fixed mode only the fixed model stays: a fixed model is a promise, so the router never quietly answers with another one.

The router tries at most four candidates (MAX_ATTEMPTS).

Key sources

A provider is available when one of these has a key for it. A later source replaces an earlier one:

Source Where the key comes from
platform The server's own Anthropic credentials: ANTHROPIC_API_KEY, ANTHROPIC_AUTH_TOKEN, ANTHROPIC_PROFILE, or ARCHIPELEGO_PLATFORM_AI=on. ARCHIPELEGO_PLATFORM_AI=off turns this source off.
server A key stored in the vault for this canvas.
browser A key sent with the request from the browser's key store.

The model event tells the client which source answered.

Streaming

streamChat in providers.ts yields text pieces.

Anthropic

Claude goes through the official Anthropic SDK. With a key it uses new Anthropic({ apiKey }). With platform credentials it uses new Anthropic(), which reads the environment or the ant auth login profile.

  • The request passes output_config: { effort }, except for claude-fable models, which manage their own depth.
  • It enables the server-side-fallback-2026-07-01 beta with fallbacks: 'default'. On a safety decline, the API re-runs the request on Anthropic's recommended fallback model.
  • A refusal stop reason becomes a ProviderError.

OpenAI-compatible providers

Every other provider uses POST <base>/chat/completions with stream: true and parses the server-sent events.

  • <base> is compatBaseUrl when set, otherwise baseUrl.
  • Each keyFields entry fills a {field} placeholder in the base URL. Qwen uses this for its workspace id. The value must match ^[A-Za-z0-9-]{1,64}$.
  • The output limit goes in maxTokensParam. The default is max_completion_tokens for OpenAI and max_tokens for the others.
  • The key goes in authorization: Bearer <key>, plus any headers from the catalog.
  • Mistral streams reasoning models as an array of thinking and text parts. The router keeps only the text parts.

Errors and fallbacks

A ProviderError carries a readable message, an optional HTTP status and a retryable flag.

  • An error before the first word is retryable: the router emits skip and tries the next candidate.
  • An error after the first word is not retryable, because the room already saw part of the answer.
  • A cancelled request is never retried.
  • When nothing answers, the error says either that no provider is connected or what the last error was.

Adding a provider

  1. Add the provider and its models to docs/research/llm-catalog.json. The build script reads these provider fields: id, label, baseUrl, openaiCompatible, compatBaseUrl, keyUrl and notes. It reads these model fields: id, provider, label, tier, reasoning, context and verified.

  2. If the provider needs maxTokensParam or keyFields, add an entry to SETTINGS in scripts/build-llm-catalog.mjs.

  3. Rebuild the catalog:

    bash
    npm run llm-catalog
  4. Add the provider's letter to the PROVIDERS map in the design system bundle, so ModelBadge can show it. The design system owners make this change in design-system/project/components/bundle.js.

  5. Add the provider id to PROVIDER_PREFERENCE in router.ts, at the place where Auto should try it.

  6. Only if the provider has no OpenAI-compatible endpoint, write a new stream function in providers.ts and call it from streamChat.

scripts/build-llm-catalog.mjs also leaves out the models in its EXCLUDE set: models the research could not confirm on chat-completions streaming.