Skip to main content

Cheaper Inference

还没翻译译自英文(ai-sdk.dev)本页 22 段都还没译

Cheaper Inference is an API gateway that provides access to models from multiple AI labs through an OpenAI-compatible API. You can use its Chat Completions endpoint with the AI SDK through the @ai-sdk/openai-compatible module.

Setup

Install the OpenAI Compatible provider:

pnpm add @ai-sdk/openai-compatible
npm install @ai-sdk/openai-compatible
yarn add @ai-sdk/openai-compatible
bun add @ai-sdk/openai-compatible

Create a Cheaper Inference API key and set it as a server-side environment variable:

export CHEAPER_INFERENCE_API_KEY=your-api-key

See the Cheaper Inference integration guide for instructions on creating an API key.

Provider Instance

Create a Cheaper Inference provider instance with createOpenAICompatible:

import { createOpenAICompatible } from '@ai-sdk/openai-compatible';

const cheaperInference = createOpenAICompatible({
  name: 'cheaper-inference',
  apiKey: process.env.CHEAPER_INFERENCE_API_KEY,
  baseURL: 'https://api.cheaperinference.com/v1',
});

The apiKey setting sends your key as a Bearer token in the Authorization header.

Language Models

Create a language model by passing a Cheaper Inference model ID to the provider instance:

const model = cheaperInference('gpt-5.4');

Generate Text

Use generateText to generate text with the model:

import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { generateText } from 'ai';

const cheaperInference = createOpenAICompatible({
  name: 'cheaper-inference',
  apiKey: process.env.CHEAPER_INFERENCE_API_KEY,
  baseURL: 'https://api.cheaperinference.com/v1',
});

const { text } = await generateText({
  model: cheaperInference('gpt-5.4'),
  prompt: 'Explain what an API gateway does in one sentence.',
});

console.log(text);

Stream Text

Use streamText to stream text as it is generated:

import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { streamText } from 'ai';

const cheaperInference = createOpenAICompatible({
  name: 'cheaper-inference',
  apiKey: process.env.CHEAPER_INFERENCE_API_KEY,
  baseURL: 'https://api.cheaperinference.com/v1',
});

const result = streamText({
  model: cheaperInference('gpt-5.4'),
  prompt: 'Count from 1 to 5, separated by spaces.',
});

for await (const textPart of result.textStream) {
  process.stdout.write(textPart);
}

Supported Models and Capabilities

This integration uses Cheaper Inference's OpenAI-compatible Chat Completions endpoint for text generation and streaming. Support for tools, vision, and other model-specific features depends on the selected model.

The model catalog changes over time. Use the authenticated GET /models endpoint to find current model IDs, capabilities, and pricing.

Resources