Skip to main content

Providers

A provider is a name, an adapter that speaks the API, and its settings. A request's model is 'provider/model', so one document can call many providers, and a hook can route a call from one to another by changing the model.

#[init]
fn providers() {
// anthropic and openai set themselves up (below); everything else is configured
self.ai.provider('groq', {'url': 'https://api.groq.com/openai/v1', 'key': 'groq'});
self.ai.provider('local', {'url': 'http://localhost:11434/v1'}); // Ollama: no key
}

Configuring a provider with a name that's taken replaces it. Each Ai has its own providers.

anthropic and openai set themselves up: a model naming one, when the Ai has no provider by that name, configures it the first time, with its adapter (anthropic, or openai for the Responses API) and the key of the same name. So model: 'anthropic/claude-sonnet-5-5' needs only a key named anthropic. Configure one yourself to change it.

Which adapter​

AdapterAPIUse it for
openai_chat (the default)Chat CompletionsOpenAI, and most compatible providers and local servers (Groq, DeepSeek, Together, Ollama, vLLM, LM Studio, ...)
openaiOpenAI's Responses APIOpenAI's reasoning models: their reasoning goes back between turns, encrypted, so tool-using agents keep it
anthropicAnthropic's Messages APIClaude: thinking with signatures, prompt caching, token counting
hostyour app's codea vendor SDK, a company proxy, signed requests (Ex. AWS SigV4); see Hosts
mocknonetests, demos, and trying things without a key

Every adapter gives the same responses, events, usage, and errors, so the rest of a document doesn't change with the provider.

Keys​

A provider's key names a key the host gave the document, and the adapter sends it the way the API wants (a bearer token, or Anthropic's x-api-key). The host gives each key only for its API's host:

doc.allowHttp(['api.openai.com', 'api.anthropic.com']);
doc.secret('openai', process.env.OPENAI_API_KEY, ['api.openai.com']);
doc.secret('anthropic', process.env.ANTHROPIC_API_KEY, ['api.anthropic.com']);

Document code sees a placeholder, never the key: printing it, storing it, or sending it to another host gives the placeholder, and a request that would carry it anywhere else fails. A local server needs no key (leave key out).

A document can give its own keys, for the CLI (which has no host to give them) or your own scripts: Http::secret('openai', env('OPENAI_API_KEY'), ['api.openai.com']). It has the key then. A key the host gave with the same name wins, so an app can always give its own.

Settings​

FieldWhat
urlThe API's base URL (each adapter has its provider's by default)
headersHeaders for every request
optionsFields added to every request's body
timeout, idleThe longest a request may take, and a stream may go without a chunk
max_tokens_fieldopenai_chat: 'max_tokens' for older servers ('max_completion_tokens' by default)
stream_usageopenai_chat: false for servers that refuse stream_options
storeopenai: true to have OpenAI keep responses (then reasoning isn't sent back)
version, max_tokensanthropic: the anthropic-version header, and max_tokens when a request has none

What only one API has​

A request's options (after the provider's) go into the body as they are, which is how to use what only one API has:

using request = new {
model: 'anthropic/claude-sonnet-5-5',
system: long_instructions,
messages: [new { role: 'user', content: question }],
options: {'cache_control': {'type': 'ephemeral'}, 'thinking': {'type': 'adaptive'}},
};
options: {'reasoning': {'effort': 'low', 'summary': 'auto'}} // openai: a summary comes back as thinking parts
options: {'reasoning_effort': 'low'} // openai_chat

Retries and fallback​

Calls try again on their own after errors that say it can help (rate limits, overloads, server and network errors), waiting longer each time (or as long as the server's retry-after says). When the retries run out, or at once for errors another model might not hit (a bad key, no credit, input too long), the request's fallback models are tried in order:

using request = new {
model: 'anthropic/claude-sonnet-5-5',
fallback: ['openai/gpt-5.6-terra', 'local/llama-4'],
retries: 3,
messages: [new { role: 'user', content: 'Summarize this ticket.' }],
};
const response = await self.ai.chat(request);
pln(response.provider); // who answered

Each try is a whole call with its own hooks, so a fallback model is checked and priced like any other. Once an event reached an #[ai-event] hook, a call isn't tried again, so a UI never shows an answer twice. The Ai's retries, retry_delay, and max_delay set the defaults.

A provider that's a little different​

An adapter is a Stof type, so a provider that differs a little from another is a type that extends that one's adapter and changes one function. The README's Adapters section has one for a server that reports cached input its own way. For a provider with no adapter at all, the host adapter lets your app make the call (see Hosts).