Skip to content

Models

Models are named <org>/<name>, and each org runs on one backend. The functions on this page choose that backend, and LangModel runs a model without an agent. For explanations and examples, see Model providers.

When a call fails, Python raises ailoy.AiloyError, Node.js throws an Error with code set to "AILOY_ERROR", and Rust returns an Err. Each entry’s Raises line says when. See Errors.

Member Does
set_lang_model_provider Sets the backend, server or factory for an org.
get_lang_model_provider Returns the provider an org uses.
remove_lang_model_provider Removes the provider for an org.
LangModel Runs a model without an agent: create, infer, infer_stream.
Custom model Runs an org with your own code.

set_lang_model_provider

Sets how every model of an org runs, for the whole process. Call it before you build an agent. Node.js: setLangModelProvider.

ailoy.set_lang_model_provider(
org: str,
provider: Backend | LangModelServer | Callable[[str, str | None], LangModel | CustomLangModel],
) -> None
Parameter Type Description
org string The part of the model name before the slash, such as "anthropic" or "Qwen".
provider Backend A backend by name: "cli", "api", "bedrock", "openrouter" or "self-hosted".
LangModelServer Your own server for this org: {"backend": "self-hosted", "base_url": ..., "api_key": ..., "models": {...}}.
factory function Called with the org and the model name after the slash (None/null if there’s none). Must return a custom model or a LangModel directly, not an awaitable.

Returns nothing. Models already built keep the backend they were built with.

Example

import ailoy
ailoy.set_lang_model_provider("anthropic", "api")
ailoy.set_lang_model_provider("Qwen", {"backend": "self-hosted", "base_url": "http://localhost:8000/v1"})

get_lang_model_provider

Returns the backend that a model name would use. Node.js: getLangModelProvider.

ailoy.get_lang_model_provider(desc: str) -> Backend | Literal["custom"] | None
Parameter Type Description
desc string A model name, such as "anthropic/claude-haiku-5-5".

Returns the backend’s name, "custom" for a factory, or None/null if the org has no provider. A LangModelServer returns "self-hosted". Rust returns the provider itself, so you can match on it.

Example

ailoy.get_lang_model_provider("anthropic/claude-haiku-5-5") # "cli"

remove_lang_model_provider

Removes an org’s provider. Its models can’t be built until you set one again. Node.js: removeLangModelProvider.

ailoy.remove_lang_model_provider(org: str) -> bool

Returns whether the org had a provider (Rust: the removed provider).

LangModel

Runs a model directly, without an agent: no tools are run and no history is kept. Built the same way an agent builds its model.

create

from ailoy import LangModel
model = await LangModel.create(desc: str)
Parameter Type Description
desc string The model name, <org>/<name>.

Returns a model, ready for infer and infer_stream.

Raises if the org has no backend, or the backend needs an API key that isn’t set.

Example

from ailoy import LangModel
model = await LangModel.create("anthropic/claude-haiku-5-5")

infer

Returns the model’s whole reply.

async def infer(
messages: list[Message],
*,
tools: list[ToolDesc] | None = None,
options: LangModelOptions | None = None,
) -> MessageOutput
Parameter Type Description
messages list of Message The conversation to send.
tools list of ToolDesc Tools to describe to the model. Ailoy doesn’t run them: tool calls come back in the reply.
options LangModelOptions thinking_effort and output_schema.

Note that in Python tools and options are keyword-only, while in Node.js they are positional.

Returns a MessageOutput: the reply, why it ended and the token usage.

Raises if the model can’t be reached or rejects the request, after automatic retries.

Example

messages = [{"role": "user", "contents": [{"type": "text", "text": "Hi"}]}]
output = await model.infer(messages, options={"thinking_effort": "low"})
print(output["message"]["contents"])

infer_stream

Returns the reply in pieces as the model writes it. Node.js: inferStream.

def infer_stream(
messages: list[Message],
*,
tools: list[ToolDesc] | None = None,
options: LangModelOptions | None = None,
) -> AgentRun[MessageDeltaOutput]

Returns an async iterator of MessageDeltaOutput. The last piece has a finish_reason.

Raises while iterating, under the same conditions as infer.

Example

async for delta in model.infer_stream(messages):
for part in delta["delta"]["contents"]:
if part["type"] == "text":
print(part["text"], end="", flush=True)

Custom model

The object a factory passed to set_lang_model_provider returns, to serve an org with your own code. For a full example in all three languages, see Use a custom model.

class CustomLangModel(Protocol):
def infer(
self, messages: list[Message], tools: list[ToolDesc], options: LangModelOptions
) -> MessageOutput | Awaitable[MessageOutput]: ...
class StreamingCustomLangModel(CustomLangModel, Protocol):
def infer_stream(
self, messages: list[Message], tools: list[ToolDesc], options: LangModelOptions
) -> AsyncIterable[MessageDeltaOutput]: ...
LangModelFactory = Callable[[str, str | None], LangModel | CustomLangModel]
Method Required Description
infer(messages, tools, options) Yes Returns the model’s whole reply as a MessageOutput. In Python and Node.js it may return an awaitable or a promise of one.
infer_stream(messages, tools, options) (inferStream) Python and Node.js: no. Rust: yes Yields the reply in pieces as MessageDeltaOutputs. Without it, streaming returns infer’s reply as one piece. A Python model that has it matches StreamingCustomLangModel.
Argument Type Description
messages list of Message The conversation so far, system message first.
tools list of ToolDesc The tools the model may call. Return calls in the message’s tool_calls.
options LangModelOptions Thinking effort and output schema.

The arguments are passed positionally, as dicts (Python) or objects (Node.js). In Node.js, the methods are called with this set to the model.

The factory is called once for each model built, with the org and the name after the slash (None or null when there’s none). It must return the model directly, not an awaitable. It can also return a LangModel, to serve the org with another org’s model.

Return a built-in model

A factory can return one of Ailoy’s own models instead of your code, for example to run an org under a name of your choosing.

Create the LangModel first, since the factory can’t await:

claude = await LangModel.create("anthropic/claude-haiku-5-5")
ailoy.set_lang_model_provider("mine", lambda org, name: claude)