Point the OpenAI chat-tools adapter at any OpenAI-compatible server and register it with the hot-ai run loop.

A CompatibleProvider is a name, an ::openai::api/Endpoint (base URL, optional key, optional headers), an optional model prefix used for routing (e.g. "groq/"), and ChatRequestOptions for the server's quirks. provider(...) turns it into an ::ai::chat/ProviderRegistration whose chat-fn and chat-stream-fn call ::openai::chat-tools/chat-with-tools-at, so ::ai::chat/run-loop and run-loop-stream can target it.

Nothing here reads ctx. Look the key up yourself (or use the presets in hot.dev/openai-compatible), so a keyless local server never needs openai.api.key.

Identity. A registration's variant is Provider.Compat({name}), enrolled in hot-ai's open Provider enum by arrow, unless the CompatibleProvider carries its own variant. So a compatible server never passes for OpenAI: is-type(reg.variant, Provider.Compat) and reg.variant.name tell them apart.

Per-call credentials. The registration's chat-opts-fn and chat-stream-opts-fn take ChatOptions.provider-opts, applied over the provider's endpoint by ::openai::api/with-opts: api-key, base-url, or a whole endpoint.

Example

::compat ::openai::compatible

groq ::compat/provider("Groq", "https://api.groq.com/openai/v1", ::hot::ctx/get("groq.api.key"), "groq/")
opts ::ai::chat/ChatOptions({
    model: "groq/llama-3.3-70b-versatile",
    providers: ::ai::chat/with-providers([groq])
})
answer ::ai::chat/run-loop(opts, "What is 17 + 25?")

Values

DEFAULT_CAPABILITIES

DEFAULT_CAPABILITIES: Vec [
  "tools",
  "streaming"
]

Functions

chat-fn

fn (p: CompatibleProvider): Fn

A ::ai::chat/run-loop chat function, (model, messages, system, tools) -> ChatReply, bound to this provider. Use it as ChatOptions.chat-fn when you don't need registry routing.

Example

local ::openai::compatible/config("Ollama", ::openai::api/endpoint("http://localhost:11434/v1"), null)
opts ::ai::chat/ChatOptions({chat-fn: ::openai::compatible/chat-fn(local), model: "llama3.2"})

chat-opts-fn

fn (p: CompatibleProvider): Fn

A chat-opts-fn bound to this provider, (model, messages, system, tools, provider-opts) -> ChatReply: chat-fn that also takes ChatOptions.provider-opts (see resolve).

Example

groq ::openai::compatible/config("Groq", ::openai::api/endpoint("https://api.groq.com/openai/v1"), null)
opts ::ai::chat/ChatOptions({
    chat-opts-fn: ::openai::compatible/chat-opts-fn(groq),
    model: "llama-3.3-70b-versatile",
    provider-opts: {api-key: ::hot::ctx/get("tenant.groq.key")}
})

chat-stream-fn

fn (p: CompatibleProvider): Fn

A ::ai::chat/run-loop-stream chat function, (model, messages, system, tools, on-delta) -> ChatReply, bound to this provider.

Example

local ::openai::compatible/config("Ollama", ::openai::api/endpoint("http://localhost:11434/v1"), "ollama/")
opts ::ai::chat/ChatOptions({model: "llama3.2", chat-stream-fn: ::openai::compatible/chat-stream-fn(local)})
::ai::chat/run-loop-stream(opts, "Hello!", (delta) { print(delta) })

chat-stream-opts-fn

fn (p: CompatibleProvider): Fn

A chat-stream-opts-fn bound to this provider: chat-stream-fn that also takes ChatOptions.provider-opts (see resolve).

Example

groq ::openai::compatible/config("Groq", ::openai::api/endpoint("https://api.groq.com/openai/v1", ::hot::ctx/get("groq.api.key")), "groq/")
opts ::ai::chat/ChatOptions({
  model: "llama-3.3-70b-versatile",
  chat-stream-opts-fn: ::openai::compatible/chat-stream-opts-fn(groq),
  provider-opts: {api-key: ::hot::ctx/get("tenant.groq.key")}
})
::ai::chat/run-loop-stream(opts, "Hello!", (delta) { print(delta) })

config

fn (name: Str, endpoint: ::openai::api/Endpoint, model-prefix: Str?): CompatibleProvider
fn (name: Str, endpoint: ::openai::api/Endpoint, model-prefix: Str?, options: ::openai::chat-tools/ChatRequestOptions?): CompatibleProvider

Build a CompatibleProvider.

Example

::compat ::openai::compatible

local ::compat/config("Ollama", ::openai::api/endpoint("http://localhost:11434/v1"), "ollama/")
deepseek ::compat/config(
    "DeepSeek",
    ::openai::api/endpoint("https://api.deepseek.com", ::hot::ctx/get("deepseek.api.key")),
    "deepseek/",
    ::openai::chat-tools/ChatRequestOptions({max-completion-tokens: 4096, max-tokens-param: "max_tokens"})
)

create-embeddings

fn (p: CompatibleProvider, request: Map): Map
fn (p: CompatibleProvider, request: Map, opts: Map): Map

POST /embeddings on the provider's endpoint. request is the OpenAI embeddings body ({model, input, dimensions?, encoding_format?}). Returns the decoded body as a Map, or an Err carrying HttpError.

Example

local ::openai::compatible/config("Ollama", ::openai::api/endpoint("http://localhost:11434/v1"), "ollama/")
result ::openai::compatible/create-embeddings(local, {model: "ollama/nomic-embed-text", input: "Hello"})
result.data[0].embedding

detect-fn

fn (prefix: Str?): Fn

A ProviderRegistration.detect function matching models that start with prefix (case-insensitive).

Example

detect ::openai::compatible/detect-fn("groq/")
detect("groq/llama-3.3-70b")  // true
detect("gpt-4o")  // false

list-models

fn (p: CompatibleProvider): Map
fn (p: CompatibleProvider, opts: Map): Map

GET /models on the provider's endpoint. Returns the decoded body ({data: [{id, ...}]} on most servers) as a Map, or an Err carrying HttpError.

Example

groq ::openai::compatible/config("Groq", ::openai::api/endpoint("https://api.groq.com/openai/v1", ::hot::ctx/get("groq.api.key")), "groq/")
models ::openai::compatible/list-models(groq)
map(models.data, %.id)

matches-model

fn (model: Str, prefix: Str?): Bool

True when model starts with prefix (case-insensitive). An empty or null prefix matches nothing.

Example

::openai::compatible/matches-model("Groq/llama-3.3-70b", "groq/")  // => true
::openai::compatible/matches-model("gpt-4o", "groq/")              // => false

provider

fn (p: CompatibleProvider): ::ai::chat/ProviderRegistration
fn (name: Str, base-url: Str, api-key: Str?, model-prefix: Str?): ::ai::chat/ProviderRegistration

Build an ::ai::chat/ProviderRegistration for an OpenAI-compatible server, reusing the OpenAI chat-tools adapter for chat-fn, chat-stream-fn, chat-opts-fn and chat-stream-opts-fn. Pass a CompatibleProvider, or a name, base URL, key (null for keyless servers) and model prefix.

variant is p.variant, else Provider.Compat({name}); display-name is the provider's name. meta carries {base-url, model-prefix, capabilities}; the key is never copied into it.

Example

::compat ::openai::compatible

registry ::ai::chat/with-providers([
    ::compat/provider("Ollama", "http://localhost:11434/v1", null, "ollama/"),
    ::compat/provider("Groq", "https://api.groq.com/openai/v1", ::hot::ctx/get("groq.api.key"), "groq/")
])
opts ::ai::chat/ChatOptions({model: "ollama/llama3.2", providers: registry})
::ai::chat/run-loop(opts, "Hello")

request-options

fn (p: CompatibleProvider): ::openai::chat-tools/ChatRequestOptions

The provider's ChatRequestOptions, or ::openai::chat-tools/DEFAULT_REQUEST_OPTIONS when unset.

Example

local ::openai::compatible/config("Ollama", ::openai::api/endpoint("http://localhost:11434/v1"), "ollama/")
::openai::compatible/request-options(local).max-completion-tokens  // 1024

resolve

fn (p: CompatibleProvider): ::openai::api/Endpoint
fn (p: CompatibleProvider, opts: Map): ::openai::api/Endpoint

The Endpoint for one call to provider p: opts applied over its endpoint (see ::openai::api/with-opts). An Err when opts are refused, or when p needs a key (key-name) and the call has none. Pure, apart from masking a key from opts in the run's call records.

Example

groq ::openai::compatible/config("Groq", ::openai::api/endpoint("https://api.groq.com/openai/v1"), "groq/")
::openai::compatible/resolve(groq, {api-key: "tenant-key"}).api-key  // => "tenant-key"

strip-model-prefix

fn (model: Str, prefix: Str?): Str

Remove the routing prefix from a model id before it is sent. Models without the prefix are returned unchanged.

Example

::openai::compatible/strip-model-prefix("openrouter/anthropic/claude-sonnet-4.5", "openrouter/")
// => "anthropic/claude-sonnet-4.5"
::openai::compatible/strip-model-prefix("llama3.2", "ollama/")
// => "llama3.2"

Types

Compat

Compat type {
    name: Str
}

A compatible server's identity in ::ai::chat/Provider: name is the provider's display name.

CompatibleProvider

CompatibleProvider type {
    name: Str,
    endpoint: ::openai::api/Endpoint,
    model-prefix: Str?,
    options: ::openai::chat-tools/ChatRequestOptions?,
    capabilities: Vec<Str>?,
    variant: ::ai::chat/Provider?,
    key-name: Str?
}

An OpenAI-compatible chat provider.

  • name: display name used in logs and ProviderRegistration.display-name.
  • endpoint: where to send requests (::openai::api/endpoint(...)).
  • model-prefix: routing prefix such as "groq/". The registration handles models that start with it (case-insensitive), and the prefix is stripped before the request, so "groq/llama-3.3-70b-versatile" is sent as "llama-3.3-70b-versatile". Null or empty means the registration never matches by name; use chat-fn directly.
  • options: ChatRequestOptions (output ceiling, max-tokens-param, stream-usage, extra-body). Null uses ::openai::chat-tools/DEFAULT_REQUEST_OPTIONS.
  • capabilities: advertised in the registration's meta (default ["tools", "streaming"]).
  • variant: the ::ai::chat/Provider for the registration. Null uses Provider.Compat({name}).
  • key-name: set when the server needs a key: the setting to name in the error when a call has none, such as groq.api.key. Null means keyless calls are fine, and no Authorization header is sent.