Chat completions
Send messages and receive a normalized completion or stream.
Chat completion is the common operation supported by every registered provider.
Stateless helper
import { completion } from "any-llm-ts";
const response = await completion({
provider: "openai",
model: "gpt-4.1-mini",
messages: [{ role: "user", content: "Hello" }],
temperature: 0.2,
maxCompletionTokens: 500,
});The stateless form also accepts apiKey, apiBase, and clientOptions because it creates a client
for the call.
Set timeout in seconds to bound an individual request. serviceTier forwards the provider's
service-tier selection when the underlying API supports it:
const response = await llm.completion({
model: "gpt-4.1-mini",
messages: [{ role: "user", content: "Hello" }],
timeout: 30,
serviceTier: "auto",
});Adapters that cannot preserve per-request timeout semantics reject timeout instead of silently
ignoring it.
Multiple choices
n asks for more than one choice in a single completion. n=1 is OpenAI's default, so any-llm-ts
drops it before the request reaches any provider. Clients that always send it still work everywhere.
Any other value is forwarded only to providers that can return several choices: OpenAI-compatible
providers (including registry gateways and AnyLLM.createOpenAICompatible endpoints), Azure AI
Inference, Gemini, Vertex AI, Hugging Face, Mistral, and Together. Groq, Cohere, Ollama, Anthropic,
Bedrock, SageMaker, and watsonx raise UnsupportedParameterError naming n instead of silently
returning one choice.
Inspect metadata.supportsCompletionN to see which path a provider takes.
responseFormat is advertised per provider on metadata.supportsCompletionResponseFormat, and per
model on AnyLLM.supportsCompletionResponseFormat(provider, model). Callers such as a model catalog
can leave the parameter out where a request carrying it would raise.
Reusable client
const llm = AnyLLM.create("openai");
const response = await llm.completion({
model: "gpt-4.1-mini",
messages: [{ role: "user", content: "Hello" }],
});When using a client, pass the provider's native model name without a provider prefix.
Multimodal messages
Message content can be a string or a list of content parts:
const response = await llm.completion({
model: "vision-capable-model",
messages: [
{
role: "user",
content: [
{ type: "text", text: "What is in this image?" },
{
type: "image_url",
image_url: { url: "https://example.com/image.jpg", detail: "auto" },
},
],
},
],
});The common content types include text, image URLs, files, and input audio. Actual support depends on the provider and selected model.
Bedrock TwelveLabs Pegasus models (twelvelabs.pegasus- in the id) take one video_url part plus
text: a base64 data URL, or an s3:// URI with video_url.bucket_owner (required on 1.5, optional
on 1.2). Those requests go to InvokeModel instead of Converse. Application inference profile ARNs
are resolved with GetInferenceProfile so Pegasus profiles still take that path.
Normalized response
const choice = response.choices[0];
console.log(choice?.finishReason);
console.log(choice?.message.content);
console.log(choice?.message.reasoning);
console.log(choice?.message.refusal);
console.log(choice?.message.toolCalls);
console.log(response.usage);
console.log(response.provider);The provider SDK response remains available as response.raw for fields that have no normalized
equivalent. Liner answer agents map each search reference onto message.citations (and
delta.citations while streaming).