kernl

Providers

Model providers for OpenAI, Anthropic, and other LLMs. Provider-agnostic configuration.

Reference: Protocol

A provider is a connection to an AI model service like Anthropic or OpenAI. kernl supports multiple authentication methods:

  • API Key — standard authentication using an API key from your environment
  • OAuth — authenticate with your existing ChatGPT Plus/Pro subscription (OpenAI only)

Quick start

The simplest way to use a provider is with environment variables:

import { anthropic } from "@kernl-sdk/ai/anthropic";
import { openai } from "@kernl-sdk/ai/openai";

// Uses ANTHROPIC_API_KEY from environment
const claude = anthropic("claude-sonnet-4-5");

// Uses OPENAI_API_KEY from environment
const gpt = openai("gpt-4o");

Explicit API key

Use createAnthropic or createOpenAI for explicit configuration:

import { createAnthropic } from "@kernl-sdk/ai/anthropic";
import { createOpenAI } from "@kernl-sdk/ai/openai";

const anthropic = createAnthropic({
  apiKey: "sk-ant-...",
});

const openai = createOpenAI({
  apiKey: "sk-...",
});

const agent = new Agent({
  model: anthropic("claude-sonnet-4-5"),
});

OAuth authentication (OpenAI)

OAuth lets you use your existing ChatGPT Plus/Pro subscription instead of API credits. This routes through the Codex API, which is the backend for ChatGPT.

import { createOpenAI } from "@kernl-sdk/ai/openai";

const openai = createOpenAI({
  oauth: {
    accessToken: "...",
    refreshToken: "...",
    expiresAt: Date.now() + 3600000,
    accountId: "...", // Required for organization subscriptions
    onRefresh: (tokens) => {
      // Persist refreshed tokens
      saveTokens("openai", tokens);
    },
  },
});

const agent = new Agent({
  model: openai("gpt-5.1"),
});

Token refresh

OAuth tokens expire. The onRefresh callback is called automatically when tokens are refreshed, allowing you to persist them:

interface RefreshedTokens {
  accessToken: string;
  refreshToken: string;
  expiresAt: number;
}

Store these tokens securely and restore them on app restart to avoid re-authentication.

Supported providers

ProviderPackageAuth methods
Anthropic@kernl-sdk/ai/anthropicAPI key
OpenAI@kernl-sdk/ai/openaiAPI key, OAuth
Google@kernl-sdk/ai/googleAPI key
Mistral@kernl-sdk/ai/mistralAPI key

Model IDs

Each provider supports specific model IDs. The type system provides autocomplete for known models while allowing any string:

// Known models get autocomplete
anthropic("claude-sonnet-4-5")
anthropic("claude-opus-4-1")
anthropic("claude-haiku-4-5")

openai("gpt-4o")
openai("gpt-5")
openai("o3")

// Any string works for new/custom models
anthropic("claude-future-model")

On this page