How to Switch Between LLM Providers in Ax: A Complete Guide

Switch between LLM providers in Ax by changing the name property in the ai factory function, which instantiates the correct provider implementation through a type-safe switch statement in src/ax/ai/wrap.ts.

The Ax library abstracts every supported LLM behind a unified interface, allowing you to switch between OpenAI, Anthropic, Google Gemini, and local models like Ollama without rewriting your application logic. This provider-agnostic architecture means you only change a string identifier and configuration object to migrate from one model to another. Understanding how to switch between LLM providers in Ax enables you to compare performance, optimize costs, or fall back to local inference with minimal code changes.

The ai Factory and AxAI Abstraction

At the core of Ax's multi-provider support is the ai factory function exported from the main package. When you invoke ai(), it forwards your options to AxAI.create(), which calls the AxAI constructor.

Inside src/ax/ai/wrap.ts, the constructor uses a private switch statement to map the name string to the appropriate concrete implementation:

  • 'openai'AxAIOpenAI
  • 'anthropic'AxAIAnthropic
  • 'google-gemini'AxAIGoogleGemini
  • 'ollama'AxAIOllama
  • 'aws-bedrock'AxAIBedrock

This design ensures that the public API methods—chat(), embed(), and others—remain identical regardless of which provider you select.

Supported Providers and Configuration Patterns

Ax supports both commercial APIs and local inference servers. Each provider accepts a standard set of options including apiKey, config for model parameters, and models for custom aliases.

OpenAI

The default provider for GPT-4 and GPT-3.5 models requires only an API key and optional model configuration.

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

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

const response = await openai.chat({
  messages: [{ role: 'user', content: 'Explain quantum entanglement.' }]
});

Anthropic

Switch to Claude models by changing the name to 'anthropic' and specifying your desired model in the config object.

const claude = ai({
  name: 'anthropic',
  apiKey: process.env.ANTHROPIC_APIKEY!,
  config: { 
    model: 'claude-3-5-sonnet-20240620', 
    temperature: 0.5 
  },
});

await claude.chat({ 
  messages: [{ role: 'user', content: 'Write a haiku about autumn.' }] 
});

Google Gemini

For Google's Gemini models, use the identifier 'google-gemini'. You can also define named model aliases to switch between fast and capable models at runtime.

const gemini = ai({
  name: 'google-gemini',
  apiKey: process.env.GOOGLE_APIKEY!,
  models: [
    { key: 'fast', model: 'gemini-2.0-flash' },
    { key: 'smart', model: 'gemini-1.5-pro' },
  ],
});

// Use the alias 'fast' instead of the full model name
await gemini.chat({ 
  model: 'fast', 
  messages: [{ role: 'user', content: 'Summarize this article.' }] 
});

Local Ollama Models

For local development or privacy-sensitive applications, switch to 'ollama' to point at your local inference server. No API key is required for local deployments.

const ollama = ai({ 
  name: 'ollama', 
  config: { model: 'llama3.2' } 
});

await ollama.chat({ 
  messages: [{ role: 'user', content: 'Give me a recipe for pancakes.' }] 
});

Complete Multi-Provider Implementation

The following example demonstrates switching between providers in the same codebase, as implemented in src/examples/image-arrays-multi-provider-test.ts. Notice how the consumer code remains identical despite underlying provider differences:

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

// Provider 1: OpenAI
const openai = ai({ 
  name: 'openai', 
  apiKey: process.env.OPENAI_APIKEY! 
});

// Provider 2: Ollama (local)
const ollama = ai({ 
  name: 'ollama', 
  config: { model: 'llama3.2' } 
});

// Identical invocation pattern
const [openaiResult, ollamaResult] = await Promise.all([
  openai.chat({ messages: [{ role: 'user', content: 'Hello' }] }),
  ollama.chat({ messages: [{ role: 'user', content: 'Hello' }] })
]);

Advanced Configuration and Model Aliases

Beyond basic provider switching, Ax supports sophisticated configuration patterns through the models array. This allows you to abstract model names behind semantic keys like 'fast', 'smart', or 'coding', making it easier to switch models without hardcoding identifiers throughout your application.

According to the source code in src/ax/ai/wrap.ts, the models parameter accepts an array of objects with key and model properties. When calling chat(), you reference the key instead of the provider-specific model string, enabling runtime model selection based on context or user preference.

Summary

  • Unified Interface: Ax abstracts all LLM providers behind the ai factory and AxAI class, ensuring consistent chat() and embed() methods regardless of backend.
  • Simple Switching: Change the name property to 'openai', 'anthropic', 'google-gemini', or 'ollama' to instantiate different providers.
  • Type Safety: The provider selection logic in src/ax/ai/wrap.ts uses a TypeScript switch statement to guarantee valid provider instantiation.
  • Flexible Configuration: Use the config object for parameters like temperature and model, and the models array for custom aliases.
  • Zero Refactoring: Switching providers requires no changes to your chat invocation logic or response handling code.

Frequently Asked Questions

What LLM providers does Ax support?

Ax supports OpenAI, Anthropic Claude, Google Gemini, AWS Bedrock, and local Ollama servers. The src/ax/ai/wrap.ts file contains the authoritative list of supported providers in its internal switch statement. Additional providers can be added by implementing the provider interface and adding a new case to the switch logic.

How do I configure model aliases in Ax?

Pass a models array to the ai factory function with objects containing key (your alias) and model (the provider's model identifier). For example, { key: 'fast', model: 'gemini-2.0-flash' } allows you to call chat({ model: 'fast' }) instead of remembering the full provider-specific model name.

Can I use multiple LLM providers in the same Ax application?

Yes. You can instantiate multiple AI instances with different name values in the same file or application. Each instance maintains its own configuration and API credentials, allowing you to route specific tasks to specific providers or implement fallback strategies between commercial and local models.

Where is the provider switching logic implemented in the Ax source code?

The switching logic is implemented in src/ax/ai/wrap.ts within the AxAI class constructor. This file contains the switch statement that maps the name parameter to concrete provider classes like AxAIOpenAI or AxAIAnthropic. The static AxAI.create method and the ai factory function both delegate to this constructor.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →