any-llm-ts
Operations

Messages API

Use a normalized Messages-shaped API across native and compatible providers.

messages() accepts Messages-style content blocks, tools, thinking controls, and stream events. Anthropic-family adapters use their native Messages protocol. Other compatible providers translate the request through chat completions and translate the result back into MessageResponse. metadata.messagesNative is true only for providers that call a real Anthropic Messages endpoint (anthropic, azureanthropic, vertexaianthropic, meta, and otari).

import { messages } from "any-llm-ts";

const response = await messages({
  provider: "anthropic",
  model: "claude-sonnet-4-5",
  maxTokens: 512,
  system: "Be concise.",
  messages: [{ role: "user", content: "Explain mapped types." }],
});

for (const block of response.content) {
  if (block.type === "text") console.log(block.text);
}

Messages calls also accept a per-request timeout in seconds and a provider serviceTier. Native Anthropic-family adapters forward both directly; compatibility adapters map them through chat completions. Native Anthropic-family adapters also accept container so a later turn can continue a previous top-level code-execution container. Compatibility adapters, Meta, and Otari reject container. When returned by the provider, requestId is normalized on MessageResponse.

Tools

const response = await llm.messages({
  model,
  maxTokens: 512,
  messages: [{ role: "user", content: "Look up order 123." }],
  tools: [
    {
      name: "get_order",
      description: "Retrieve an order",
      inputSchema: {
        type: "object",
        properties: { id: { type: "string" } },
        required: ["id"],
      },
    },
  ],
  toolChoice: { type: "auto" },
});

Tool uses and results are normalized as tool_use and tool_result blocks. Providers may preserve signatures and other native fields in their content blocks.

Streaming

const events = await llm.messages({
  model,
  maxTokens: 512,
  messages: [{ role: "user", content: "Count to five." }],
  stream: true,
});

for await (const event of events) {
  if (event.type === "content_block_delta" && event.delta.type === "text_delta") {
    process.stdout.write(event.delta.text);
  }
}

Compatibility streams emit the standard message-start, content-block, message-delta, and message-stop lifecycle. Native-only beta and context-management options reject on adapters that cannot preserve their semantics.

Anthropic and Otari can stream schema-constrained Messages events together with outputFormat. Other providers reject that combination because structured Messages parsing needs a complete response.

On this page