@openclaw/ai Package: Model Transports and Runtime Isolation

Learn about the @openclaw/ai npm package, which provides provider-agnostic contracts for messages, tools, and streams, plus lazy adapters for eight API families. Ideal for developers integrating model execution into their applications.

Read this when

  • You want to reuse OpenClaw's model transports in another application
  • You are changing packages/ai or the AI transport host ports
  • You are reviewing what the openclaw release publishes to npm besides the root package

@openclaw/ai is the distributable package version of OpenClaw's execution layer for models. It provides provider-agnostic contracts for messages, tools, and streams, along with validation, diagnostics, event streams, a separate runtime registry, and lazy adapters covering the eight built-in API families: Anthropic Messages, OpenAI Completions, OpenAI Responses, Azure OpenAI Responses, ChatGPT/Codex Responses, Google Generative AI, Google Vertex, and Mistral Conversations.

Each release publishes this package together with the root openclaw package, both pinned to the same version. Direct dependencies are exact-pinned and resolved during installation; no npm lockfile ships with the package. Installing openclaw automatically brings in the matching @openclaw/ai, and library consumers can depend on it directly without any OpenClaw application code.

Quick start

import { createLlmRuntime } from "@openclaw/ai";
import { registerBuiltInApiProviders } from "@openclaw/ai/providers";

const runtime = createLlmRuntime();
registerBuiltInApiProviders(runtime.registry);

const stream = runtime.streamSimple(model, { messages }, { apiKey });
for await (const event of stream) {
  if (event.type === "text_delta") process.stdout.write(event.delta);
}
const result = await stream.result();

A runnable version is available in the repository at examples/ai-chat.

Design contract

  • Isolated by default per instance. Importing the package has no global registration side effects. createApiRegistry() and createLlmRuntime() each return separate instances; registerBuiltInApiProviders(registry) opts a single registry into the built-in transports. Provider SDK modules are loaded lazily upon first use.
  • Host policy is injected, not bundled. Request fetch guarding (such as SSRF policy), secret redaction for tool-result replay text, OpenAI strict-tool defaults, and diagnostics logging are AiTransportHost ports configured through configureAiTransportHost. The library ships with inert defaults; OpenClaw installs its real implementations within its stream facade.
  • A single event-stream identity. @openclaw/ai/event-stream serves as the canonical EventStream constructor used by OpenClaw core, agent-core, and external consumers alike.
  • internal/* subpaths are not part of the API. They exist solely for the OpenClaw application itself and carry no semver guarantees.
  • Provider ids, credentials, model catalogs, retries, and failover fall under application responsibility. OpenClaw wraps those concerns around this package; a library consumer passes a Model object and options directly.

Subpath exports

SubpathContents
.Contracts, createApiRegistry, createLlmRuntime, configureAiTransportHost
./providersregisterBuiltInApiProviders, resetApiProviders
./typesModel/message/tool/stream types
./validationTool argument validation
./diagnosticsDiagnostics contracts
./event-streamShared EventStream implementation
./internal/*OpenClaw-internal, no semver guarantee
396 words · updated Aug 4, 2026