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.
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:
export interface AIConfig {
mode?: 'auto' | 'fixed';
fixed?: string;
roles?: Partial<Record<Role, string>>;
fallback?: string[];
}- The fixed model, when
modeisfixed. - The model chosen for this role in
roles. - The
fallbacklist, in the order the room set. - 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 forclaude-fablemodels, which manage their own depth. - It enables the
server-side-fallback-2026-07-01beta withfallbacks: 'default'. On a safety decline, the API re-runs the request on Anthropic's recommended fallback model. - A
refusalstop reason becomes aProviderError.
OpenAI-compatible providers
Every other provider uses POST <base>/chat/completions with stream: true and parses the server-sent events.
<base>iscompatBaseUrlwhen set, otherwisebaseUrl.- Each
keyFieldsentry 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 ismax_completion_tokensfor OpenAI andmax_tokensfor the others. - The key goes in
authorization: Bearer <key>, plus anyheadersfrom 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
skipand 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
Add the provider and its models to
docs/research/llm-catalog.json. The build script reads these provider fields:id,label,baseUrl,openaiCompatible,compatBaseUrl,keyUrlandnotes. It reads these model fields:id,provider,label,tier,reasoning,contextandverified.If the provider needs
maxTokensParamorkeyFields, add an entry toSETTINGSinscripts/build-llm-catalog.mjs.Rebuild the catalog:
bashnpm run llm-catalogAdd the provider's letter to the
PROVIDERSmap in the design system bundle, soModelBadgecan show it. The design system owners make this change indesign-system/project/components/bundle.js.Add the provider id to
PROVIDER_PREFERENCEinrouter.ts, at the place where Auto should try it.Only if the provider has no OpenAI-compatible endpoint, write a new stream function in
providers.tsand call it fromstreamChat.
scripts/build-llm-catalog.mjs also leaves out the models in its EXCLUDE set: models the research could not confirm on chat-completions streaming.