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, endpoint |
Registry slug and the vendor endpoint shape (anthropic-, openai-, openai-, google-, ollama) |
create |
The raw vendor model; the registry wraps it with the shaping middleware |
profile(modelId) |
ModelCapability: 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 Shapings |
classify |
AiErrorClass: kind (authentication, quota, rate_, model_, unsupported_, invalid_, overloaded, server_, timeout, network, unknown), retryable, status |
normalise |
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#
- Add the slug to
AiProviderEnumandAI_DB_PROVIDER_IDSinpackages/ai/src/providers.ts. - Write
packages/ai/src/adapters/<provider>.tsimplementingProviderAdapter; vendor SDK imports and vendor request knobs may appear only underpackages/ai/src/adapters/. - Register it in
ADAPTERSinpackages/ai/src/adapters/index.ts. - Add the slug to the hand-listed unions
AiProvider(packages/schema/src/tables/ai.ts) andAiConnectionProvider(packages/schema/src/tables/ai-connections.ts); the column isvarchar(32), so no migration, but a schema-package change needs operator review. - Add the slug to
ListableProviderandCURATED_MODELS(packages/ai/src/curated-models.ts) and a case to the live model listing inpackages/ai/src/models.ts. - Add price rows with a dated source note to
packages/ai/src/pricing.tsand bumpAI_PRICE_TABLE_VERSION. - Add the provider label and endpoint help to the connection forms (
apps/web/src/server/routers/ai-configs.ts,apps/web/src/app/admin/aiandapps/web/src/app/(dashboard)/settings/ai). - Add tests: profiles, shaping and errors in
packages/ai/src/adapters/adapters.test.ts; arecorded-<provider>.test.tswith a stubbedfetchthat pins the URL path, body keys and parsed usage; the endpoint inrecorded-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 asfatal-alland everything else asretry-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.tsonly. - Cost is computed from
normaliseUsageoutput andaiCostMicroVndper call.
Where it stops#
- Three lists carry the provider slugs and nothing ties them together:
AiProviderEnum, the schema unions andListableProvider; keep them equal by hand. - ADR 0056 lists the providers in prose;
ADAPTERSis the authority. - There is no per-organization provider code: a tenant picks among the built-in providers and endpoints.