ai() LLM Models Create provider clients and select models. typescript subsystems subsystems/ai website/content-src/templates/subsystem-ai.md subsystems ai() LLM Models

ai() LLM Models

Use ai() to create provider clients and keep model traffic behind one Ax request shape.

TypeScript
import { AxAIOpenAIModel, ai } from '@ax-llm/ax';

const openai = ai({
  name: 'openai',
  apiKey: process.env.OPENAI_APIKEY!,
  config: { model: AxAIOpenAIModel.GPT4OMini },
});

What It Does

ai() selects a provider implementation from configuration and returns a client that Ax programs can call. The client handles chat, streaming, embeddings, media where supported, usage normalization, provider options, model keys, routing hooks, tracing, and runtime defaults.

flowchart LR
  A["Model key or alias"] --> B["Model catalog"]
  B --> C["Capability filter"]
  C --> D["Provider client"]
  D --> E["Request mapping"]
  E --> F["Provider API"]
  F --> G["Response normalization"]
  G --> H["Usage + trace"]

Core Call Shape

Create the client once near the application boundary, then pass it into forward(), streamingForward(), agents, flows, or optimizers.

text
client = ai(provider options)
result = program.forward(client, inputs)

Common Patterns

  • Use a provider name and environment-backed API key.
  • Set a default model in provider config when the app has one obvious model.
  • Define model aliases when callers should choose fast, smart, or cheap instead of provider model IDs.
  • Use OpenAI-compatible apiURL for compatible providers.
  • Use model catalog helpers before runtime when the UI needs provider/model selectors.
  • Use routers or balancers when provider fallback is part of the product.

Provider clients

OpenAI

TypeScript
import { AxAIOpenAIModel, ai } from '@ax-llm/ax';

const openai = ai({
  name: 'openai',
  apiKey: process.env.OPENAI_APIKEY!,
  config: { model: AxAIOpenAIModel.GPT4OMini },
});

OpenAI Responses

TypeScript
const responses = ai({
  name: 'openai-responses',
  apiKey: process.env.OPENAI_APIKEY!,
  config: { model: 'gpt-4.1-mini' },
});

Claude / Anthropic

TypeScript
import { AxAIAnthropicModel, ai } from '@ax-llm/ax';

const claude = ai({
  name: 'anthropic',
  apiKey: process.env.ANTHROPIC_APIKEY!,
  config: { model: AxAIAnthropicModel.Claude48Opus },
});

Gemini

TypeScript
import { AxAIGoogleGeminiModel, ai } from '@ax-llm/ax';

const gemini = ai({
  name: 'google-gemini',
  apiKey: process.env.GOOGLE_APIKEY!,
  config: { model: AxAIGoogleGeminiModel.Gemini25Flash },
});

OpenAI-Compatible Providers

Use apiURL when a provider shares the OpenAI wire shape but uses a different host or model naming scheme.

TypeScript
const compatible = ai({
  name: 'openai',
  apiKey: process.env.PROVIDER_API_KEY!,
  apiURL: 'https://provider.example/v1',
  config: { model: 'provider/model-name' },
});

Embeddings and audio

TypeScript
const { embeddings } = await openai.embed({
  texts: ['typed LLM programs', 'runtime agents'],
  embedModel: 'text-embedding-3-small',
});
TypeScript
const transcript = await openai.transcribe({
  audio: { data: base64Wav, format: 'wav' },
  model: 'gpt-4o-mini-transcribe',
  language: 'en',
});

const speech = await openai.speak({ text: transcript.text, model: 'gpt-4o-mini-tts', voice: 'alloy' });

Practical Notes

  • Prefer provider factories over direct provider classes in new code.
  • Use model catalog and provider-scoring helpers when choosing between providers.
  • Use routers/balancers when a workflow can fall back or split traffic.
  • Keep provider-api examples separate from no-key examples.
  • Trace provider requests, token usage, estimated cost, and routing decisions in production.

See ai() API.

Docs