@openclaw/ai package: reusable model transports and isolated runtimes

This page describes the @openclaw/ai npm package, which provides provider-neutral contracts, validation, diagnostics, and isolated runtimes for AI models. It is intended for developers integrating OpenClaw's model execution layer into their projects.

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 publishable library form of OpenClaw's model execution layer. It provides provider-neutral contracts for messages, tools, and streams, along with validation, diagnostics, event streams, an isolated runtime registry, and lazy adapters for the eight built-in API families (Anthropic Messages, OpenAI Completions, OpenAI Responses, Azure OpenAI Responses, ChatGPT/Codex Responses, Google Generative AI, Google Vertex, Mistral Conversations).

It ships alongside the root openclaw package on every release, pinned to the same version, with its own npm-shrinkwrap.json so its transitive dependency tree is locked at install time. Installing openclaw automatically installs the matching @openclaw/ai. 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 lives in the repository at examples/ai-chat.

Design contract

  • Instance-scoped by default. Importing the package does not register anything globally. createApiRegistry() and createLlmRuntime() return isolated instances. registerBuiltInApiProviders(registry) opts one registry into the built-in transports. Provider SDK modules load lazily on first use.
  • Host policy is injected, not bundled. Request fetch guarding (for example SSRF policy), secret redaction of tool-result replay text, OpenAI strict-tool defaults, and diagnostics logging are AiTransportHost ports configured with configureAiTransportHost. The library defaults are inert. OpenClaw installs its real implementations in its stream facade.
  • One event-stream identity. @openclaw/ai/event-stream is the canonical EventStream constructor shared by OpenClaw core, agent-core, and external consumers.
  • internal/* subpaths are not API. They exist for the OpenClaw application itself and carry no semver guarantee.
  • Provider ids, credentials, model catalogs, retries, and failover remain application concerns. OpenClaw layers those around this package. A library consumer supplies 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