LLM SDK

Overview

Unified API for any LLM provider

Use a common generation API with OpenAI, Anthropic, Google, and other supported providers. Model parameters, native tools, caching, and audio support vary by provider. See the 2.6.0 upgrade guide for new APIs and compatibility limits.

Check package versions and compatibility when installing the SDK. The Feature Lab guide explains how to try each feature and what has been verified with real provider calls.

import { streamText } from "@yourgpt/llm-sdk";
import { openai } from "@yourgpt/llm-sdk/openai";

const result = await streamText({
  model: openai("gpt-4o"),
  prompt: "Hello!",
});

Switch Providers Instantly

Basic text generation shares the same call shape. Check feature-specific support before switching providers in workflows that use native tools or replay state.

import { openai } from '@yourgpt/llm-sdk/openai';

const model = openai('gpt-4o');
import { anthropic } from '@yourgpt/llm-sdk/anthropic';

const model = anthropic('claude-sonnet-4-20250514');
import { google } from '@yourgpt/llm-sdk/google';

const model = google('gemini-2.0-flash');
import { xai } from '@yourgpt/llm-sdk/xai';

const model = xai('grok-3-fast');

Framework Examples

Works with any HTTP framework. Returns standard Web Response objects.

app/api/chat/route.ts
import { streamText } from '@yourgpt/llm-sdk';
import { openai } from '@yourgpt/llm-sdk/openai';

export async function POST(req: Request) {
  const { messages } = await req.json();

  const result = await streamText({
    model: openai('gpt-4o'),
    messages,
  });

  return result.toTextStreamResponse();
}
server.ts
import express from 'express';
import { streamText } from '@yourgpt/llm-sdk';
import { openai } from '@yourgpt/llm-sdk/openai';

const app = express();
app.use(express.json());

app.post('/api/chat', async (req, res) => {
  const result = await streamText({
    model: openai('gpt-4o'),
    messages: req.body.messages,
  });

  const response = result.toTextStreamResponse();
  res.set(Object.fromEntries(response.headers));
  response.body?.pipeTo(new WritableStream({
    write(chunk) { res.write(chunk); },
    close() { res.end(); }
  }));
});
src/index.ts
import { Hono } from 'hono';
import { streamText } from '@yourgpt/llm-sdk';
import { openai } from '@yourgpt/llm-sdk/openai';

const app = new Hono();

app.post('/api/chat', async (c) => {
  const { messages } = await c.req.json();

  const result = await streamText({
    model: openai('gpt-4o'),
    messages,
  });

  return result.toTextStreamResponse();
});

export default app;
server.ts
import Fastify from 'fastify';
import { streamText } from '@yourgpt/llm-sdk';
import { openai } from '@yourgpt/llm-sdk/openai';

const app = Fastify();

app.post('/api/chat', async (req, reply) => {
  const result = await streamText({
    model: openai('gpt-4o'),
    messages: req.body.messages,
  });

  const response = result.toTextStreamResponse();
  reply.headers(Object.fromEntries(response.headers));
  return reply.send(response.body);
});

Core Functions

FunctionDescription
streamText()Stream responses in real-time
generateText()Generate complete responses
tool()Define AI-callable functions
Parameters and fallbackHandle reasoning settings and specific compatibility failures
ResponsesPreserve OpenAI conversation and tool state
Image tool resultsReturn native images to supported models
generateSpeech() / transcribe()Generate speech and transcribe recorded audio
experimental_decide()Classify and score state with native decision models
generateImage()Generate and edit images
Realtime voiceExperimental browser voice calls over WebRTC

Why Unified?

  • One API - Learn once, use everywhere
  • Swap providers - Change one import, keep all your code
  • Type-safe - Full TypeScript support across all providers
  • Standard responses - Web Response objects work anywhere

On this page