Skip to content

Quick Start

Get up and running with the Unified Provider Protocol in minutes.

Terminal window
npm install @providerprotocol/ai
  1. Import the library and a provider

    import { llm } from '@providerprotocol/ai';
    import { anthropic } from '@providerprotocol/ai/anthropic';
  2. Create an LLM instance

    const claude = llm({
    model: anthropic('claude-sonnet-4-20250514'),
    system: 'You are a helpful assistant.',
    });
  3. Generate a response

    const turn = await claude.generate('What is the capital of France?');
    console.log(turn.response.text);
    // "The capital of France is Paris."

Stream tokens as they arrive:

const stream = claude.stream('Write a haiku about coding.');
for await (const event of stream) {
if (event.type === 'text_delta') {
process.stdout.write(event.delta.text ?? '');
}
}
// Get the complete turn after streaming
const turn = await stream.turn;

Maintain conversation history:

import { Message } from '@providerprotocol/ai';
const history: Message[] = [];
// First turn
const turn1 = await claude.generate(history, 'My name is Alice.');
history.push(...turn1.messages);
// Second turn - model remembers context
const turn2 = await claude.generate(history, 'What is my name?');
history.push(...turn2.messages);
console.log(turn2.response.text);
// "Your name is Alice."

Switch providers by changing the import:

import { llm } from '@providerprotocol/ai';
import { openai } from '@providerprotocol/ai/openai';
import { google } from '@providerprotocol/ai/google';
import { anthropic } from '@providerprotocol/ai/anthropic';
// OpenAI
const gpt = llm({ model: openai('gpt-4o') });
// Google
const gemini = llm({ model: google('gemini-2.0-flash') });
// Anthropic
const claude = llm({ model: anthropic('claude-sonnet-4-20250514') });

Generate vector embeddings:

import { embedding } from '@providerprotocol/ai';
import { openai } from '@providerprotocol/ai/openai';
const embedder = embedding({
model: openai('text-embedding-3-small'),
});
const result = await embedder.embed('Hello, world!');
console.log(result.vector.length); // 1536

Generate images from text:

import { image } from '@providerprotocol/ai';
import { openai } from '@providerprotocol/ai/openai';
const dalle = image({
model: openai('dall-e-3'),
params: { size: '1024x1024' },
});
const result = await dalle.generate('A sunset over mountains');
const imageData = result.images[0].image.toBytes();