Skip to content
MSO CloudDocumentation

Add a language-model provider

On this page

All model calls go through @yng/ai. A provider is one adapter that builds the vendor model, states what each model can do, reshapes every request for it, and classifies its failures.

When to use#

Add a provider when a vendor needs its own endpoint shape, request knobs or error codes. A vendor that speaks the OpenAI chat protocol does not need code: the openai-compatible adapter takes a per-connection endpoint, so an organization adds it as a connection. A new model of an existing provider needs only a capability rule, a curated-list entry and a price row, not an adapter. Call sites never branch on a provider: they state an intent and the adapter shapes the call (ADR 0056, failover in ADR 0005).

The contract#

ProviderAdapter in packages/ai/src/adapters/types.ts:

Member Owns
provider, endpointKind Registry slug and the vendor endpoint shape (anthropic-messages, openai-responses, openai-chat, google-generate-content, ollama)
createModel(config) The raw vendor model; the registry wraps it with the shaping middleware
profile(modelId) ModelCapabilityProfile: tools, forced tool choice, thinking, sampling, prefill, structured output, limits, cache; unknown models fail closed
shapeCall(params, modelId) Pure rewrite of one call for that exact model; returns ShapingNotes
classifyError(error) AiErrorClass: kind (authentication, quota, rate_limited, model_not_found, unsupported_request, invalid_request, overloaded, server_error, timeout, network, unknown), retryable, statusCode
normaliseUsage(usage) One usage shape (cached, uncached, reasoning, 1-hour cache writes) for cost accounting

Call intents are conversation, bounded, bounded-reasoning and probe; call sites build options with the functions in packages/ai/src/provider-options.ts.

How registration works#

ADAPTERS: Record<AiProvider, ProviderAdapter> in packages/ai/src/adapters/index.ts is the one provider-to-adapter map and is typed by AiProviderEnum (packages/ai/src/providers.ts), so an enum member without an adapter fails to compile. Stored slugs differ in one case: the database holds gemini where the registry says google (AI_DB_PROVIDER_IDS, toRegistryProvider).

Add one#

  1. Add the slug to AiProviderEnum and AI_DB_PROVIDER_IDS in packages/ai/src/providers.ts.
  2. Write packages/ai/src/adapters/<provider>.ts implementing ProviderAdapter; vendor SDK imports and vendor request knobs may appear only under packages/ai/src/adapters/.
  3. Register it in ADAPTERS in packages/ai/src/adapters/index.ts.
  4. Add the slug to the hand-listed unions AiProvider (packages/schema/src/tables/ai.ts) and AiConnectionProvider (packages/schema/src/tables/ai-connections.ts); the column is varchar(32), so no migration, but a schema-package change needs operator review.
  5. Add the slug to ListableProvider and CURATED_MODELS (packages/ai/src/curated-models.ts) and a case to the live model listing in packages/ai/src/models.ts.
  6. Add price rows with a dated source note to packages/ai/src/pricing.ts and bump AI_PRICE_TABLE_VERSION.
  7. Add the provider label and endpoint help to the connection forms (apps/web/src/server/routers/ai-configs.ts, apps/web/src/app/admin/ai and apps/web/src/app/(dashboard)/settings/ai).
  8. Add tests: profiles, shaping and errors in packages/ai/src/adapters/adapters.test.ts; a recorded-<provider>.test.ts with a stubbed fetch that pins the URL path, body keys and parsed usage; the endpoint in recorded-endpoints.test.ts.

Guards#

packages/ai/src/adapters/adapters.test.ts (includes the guard that every curated model resolves to a known profile), recorded-*.test.ts, packages/ai/src/pricing.test.ts (every curated id of a priced provider has a price row), apps/web/src/__tests__/ai-failover.test.ts; in audit:all: scripts/audit/ai-adapter-boundary.py (token audit-ok-ai-adapter) and scripts/audit/ai-resolver-single-source.py (one config resolver, no raw model id on end-user surfaces).

Operating it#

  • Failover: an organization holds several connections; classifyFailover (packages/ai/src/failover.ts) treats an invalid prompt or schema as fatal-all and everything else as retry-next. A candidate that fails permanently is skipped for a cooldown keyed by connection and model.
  • Probe: the Test button on the AI settings screens runs probeAiConnection (packages/ai/src/probe.ts): a schema-valid call to a probe tool, reported with a typed failure reason. Results are not stored.
  • Keys are envelope-encrypted per organization; resolution goes through packages/ai/src/resolve.ts only.
  • Cost is computed from normaliseUsage output and aiCostMicroVnd per call.

Where it stops#

  • Three lists carry the provider slugs and nothing ties them together: AiProviderEnum, the schema unions and ListableProvider; keep them equal by hand.
  • ADR 0056 lists the providers in prose; ADAPTERS is the authority.
  • There is no per-organization provider code: a tenant picks among the built-in providers and endpoints.