### Example Usage of OpenRouterProviderOptions Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/types.md Demonstrates how to configure reasoning and model routing options when initializing a chat model. ```typescript const model = openrouter.chat('openai/gpt-4o', { reasoning: { enabled: true, max_tokens: 10000, }, models: ['openai/gpt-4o', 'anthropic/claude-3.5-sonnet'], }); ``` -------------------------------- ### Install OpenRouter AI SDK Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/README.md Install the necessary packages for OpenRouter AI SDK provider and AI functionality using npm. ```bash npm install @openrouter/ai-sdk-provider ai zod ``` -------------------------------- ### Install Dependencies Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/CONTRIBUTING.md Install project dependencies using pnpm, the package manager for this project. ```bash pnpm install ``` -------------------------------- ### Install OpenRouter Provider for AI SDK v5 (Legacy) Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/README.md Install a specific legacy version of the OpenRouter provider package for AI SDK v5. This is for users on older SDK versions. ```bash # For pnpm pnpm add @openrouter/ai-sdk-provider@1.5.4 # For npm npm install @openrouter/ai-sdk-provider@1.5.4 # For yarn yarn add @openrouter/ai-sdk-provider@1.5.4 ``` -------------------------------- ### Stream Text with Basic Setup Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/examples.md Illustrates how to stream text responses from a model. This is useful for real-time applications where immediate feedback is needed. ```typescript import { streamText } from 'ai'; import { openrouter } from '@openrouter/ai-sdk-provider'; const result = await streamText({ model: openrouter('openai/gpt-4o'), prompt: 'Write a short story about a robot.', }); for await (const chunk of result.textStream) { process.stdout.write(chunk); } ``` -------------------------------- ### Tool Use (Function Calling) Example Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/examples.md Demonstrates how to use the `generateText` function with tools for function calling. The tool 'weather' is defined to get weather information for a specified location. ```typescript import { generateText } from 'ai'; import { openrouter } from '@openrouter/ai-sdk-provider'; const model = openrouter('openai/gpt-4o'); const result = await generateText({ model, tools: { weather: { description: 'Get the weather for a location', parameters: { type: 'object' as const, properties: { location: { type: 'string', description: 'City name', }, }, required: ['location'], }, execute: async ({ location }: { location: string }) => { return `The weather in ${location} is sunny and 72°F.`; }, }, }, prompt: 'What is the weather in New York?', maxToolRoundtrips: 2, }); console.log('Final response:', result.text); ``` -------------------------------- ### Example Usage of OpenRouterChatSettings Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/configuration.md Demonstrates how to configure chat model settings with various options including plugins, caching, provider routing, and reasoning. ```typescript const model = openrouter.chat('openai/gpt-4o', { temperature: 0.7, maxTokens: 2000, topP: 0.9, parallelToolCalls: true, plugins: [ { id: 'web', max_results: 5, engine: 'exa' }, { id: 'response-healing' }, ], cache_control: { type: 'ephemeral', ttl: '1h' }, provider: { order: ['anthropic', 'openai'], allow_fallbacks: true, max_price: { prompt: '0.01', completion: '0.05', }, }, reasoning: { enabled: true, max_tokens: 5000, }, usage: { include: true }, }); ``` -------------------------------- ### Install OpenRouter Provider for AI SDK v6 (Legacy) Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/README.md Install a specific legacy version of the OpenRouter provider package for AI SDK v6. Use this if you are on an older SDK version. ```bash # For pnpm pnpm add @openrouter/ai-sdk-provider@2.9.1 # For npm npm install @openrouter/ai-sdk-provider@2.9.1 # For yarn yarn add @openrouter/ai-sdk-provider@2.9.1 ``` -------------------------------- ### Generate Text with Simple Prompt Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/examples.md Demonstrates basic text generation using a simple prompt with the OpenRouter provider. Ensure the 'ai' and '@openrouter/ai-sdk-provider' packages are installed. ```typescript import { generateText } from 'ai'; import { openrouter } from '@openrouter/ai-sdk-provider'; const { text } = await generateText({ model: openrouter('openai/gpt-4o'), prompt: 'Write a haiku about AI.', }); console.log(text); ``` -------------------------------- ### Initialize Legacy OpenRouter Provider Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/api-reference.md Example of how to initialize the legacy OpenRouter provider using an API key. This class is deprecated; prefer createOpenRouter(). ```typescript import { OpenRouter } from '@openrouter/ai-sdk-provider'; const openrouter = new OpenRouter({ apiKey: process.env.OPENROUTER_API_KEY, }); const model = openrouter.chat('openai/gpt-4o'); ``` -------------------------------- ### Configure Image Model with Provider Preferences Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/configuration.md Example of initializing an image model with specific provider routing preferences. This configures the model to prioritize 'black-forest-labs' and fall back to 'openai', with a maximum image price of '0.10'. ```typescript const model = openrouter.imageModel('black-forest-labs/flux-1.1-pro', { provider: { order: ['black-forest-labs', 'openai'], max_price: { image: '0.10' }, }, }); ``` -------------------------------- ### Install OpenRouter Provider for AI SDK v7 Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/README.md Install the OpenRouter provider package for AI SDK v7 using your preferred package manager. This version requires Node.js 22 or newer and is ESM-only. ```bash # For pnpm pnpm add @openrouter/ai-sdk-provider # For npm npm install @openrouter/ai-sdk-provider # For yarn yarn add @openrouter/ai-sdk-provider ``` -------------------------------- ### Configure Provider Routing Order and Fallbacks Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/plugins-and-features.md Define the order in which providers are tried and whether to allow fallbacks. This example prioritizes Anthropic then OpenAI, denying data collection. ```typescript const model = openrouter.chat('gpt-4-turbo', { provider: { order: ['anthropic', 'openai'], // Try Anthropic first, then OpenAI allow_fallbacks: true, // Allow other providers if primary unavailable data_collection: 'deny', // Only use providers that don't store data max_price: { prompt: '0.01', completion: '0.05', }, }, }); ``` -------------------------------- ### Generate Text with OpenRouter Provider Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/README.md Example of generating text using the OpenRouter provider with the Vercel AI SDK. Specify the model and provide a prompt to get a text response. ```typescript import { openrouter } from '@openrouter/ai-sdk-provider'; import { generateText } from 'ai'; const { text } = await generateText({ model: openrouter('openai/gpt-4o'), prompt: 'Write a vegetarian lasagna recipe for 4 people.', }); ``` -------------------------------- ### Multi-turn Conversation Example Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/examples.md Demonstrates how to maintain context across multiple turns in a conversation. This is useful for follow-up questions and maintaining a coherent dialogue. ```typescript import { generateText } from 'ai'; import { openrouter } from '@openrouter/ai-sdk-provider'; const model = openrouter('openai/gpt-4o', { temperature: 0.5, }); // First turn const response1 = await generateText({ model, messages: [ { role: 'user', content: 'What is the capital of France?', }, ], }); console.log('Assistant:', response1.text); // Second turn with context const response2 = await generateText({ model, messages: [ { role: 'user', content: 'What is the capital of France?', }, { role: 'assistant', content: response1.text, }, { role: 'user', content: 'What is its population?', }, ], }); console.log('Assistant:', response2.text); ``` -------------------------------- ### Accessing Provider Metadata and Usage Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/types.md Example of how to access provider name, usage, and reasoning details from the response metadata after generating text. Ensure the `openrouter` property exists before accessing its members. ```typescript const result = await generateText({ model: openrouter.chat('openai/gpt-4o'), prompt: 'Hello!', }); if (result.providerMetadata?.openrouter) { console.log('Provider:', result.providerMetadata.openrouter.provider); console.log('Usage:', result.providerMetadata.openrouter.usage); if (result.providerMetadata.openrouter.reasoning_details) { console.log('Reasoning:', result.providerMetadata.openrouter.reasoning_details); } } ``` -------------------------------- ### Import Default Provider Instance Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/README.md Import the default OpenRouter provider instance named 'openrouter' from the installed package. This is the entry point for using OpenRouter with the AI SDK. ```typescript import { openrouter } from '@openrouter/ai-sdk-provider'; ``` -------------------------------- ### Web Search using Plugin Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/examples.md Performs a web search to gather information for text generation using a configured plugin. Ensure the 'ai' and '@openrouter/ai-sdk-provider' packages are installed and the plugin is correctly set up. ```typescript import { generateText } from 'ai'; import { openrouter } from '@openrouter/ai-sdk-provider'; const model = openrouter('openai/gpt-4o', { plugins: [ { id: 'web', max_results: 5, engine: 'exa', }, ], }); const { text } = await generateText({ model, prompt: 'What are the latest developments in AI?', }); console.log(text); ``` -------------------------------- ### Example Streaming Response Sequence Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/message-formats.md Illustrates the typical sequence of chunks received during a streaming chat completion. Note the initial role assignment, content delivery, and final stop signal. ```json First chunk: { choices: [{ delta: { role: 'assistant', content: null } }] } Content chunks: { choices: [{ delta: { content: 'Hello' }, finish_reason: null }] } More content: { choices: [{ delta: { content: ' world' }, finish_reason: null }] } Final chunk: { choices: [{ delta: {}, finish_reason: 'stop' }], usage: {...} } ``` -------------------------------- ### Configure OpenRouter Embedding Model with Provider Routing Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/configuration.md This example demonstrates how to configure an embedding model, specifying a user ID, provider routing preferences (order, fallbacks, sort strategy), and enabling usage accounting. The source file is src/types/openrouter-embedding-settings.ts. ```typescript const model = openrouter.textEmbeddingModel('openai/text-embedding-3-small', { user: 'user-123', provider: { order: ['openai', 'voyageai'], allow_fallbacks: true, sort: 'price', }, usage: { include: true }, }); ``` -------------------------------- ### Create OpenRouter Instance with Configuration Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/configuration.md Demonstrates how to create an OpenRouter instance using various configuration options. This includes setting the API key, base URL, app name and URL for attribution, custom headers, and provider-specific API keys. ```typescript import { createOpenRouter } from '@openrouter/ai-sdk-provider'; const openrouter = createOpenRouter({ apiKey: process.env.OPENROUTER_API_KEY, baseURL: 'https://openrouter.ai/api/v1', appName: 'MyApp', appUrl: 'https://myapp.com', headers: { 'Custom-Header': 'value', }, extraBody: { // Features supported by OpenRouter }, api_keys: { anthropic: process.env.ANTHROPIC_API_KEY, openai: process.env.OPENAI_API_KEY, }, }); ``` -------------------------------- ### Build Project Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/CONTRIBUTING.md Build the project. Use 'pnpm build' for a single build or 'pnpm dev' for continuous development with watch mode. ```bash # Build once pnpm build # Build in watch mode for continuous development pnpm dev ``` -------------------------------- ### Extracted Error Message Example Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/message-formats.md An example of an extracted error message from an upstream provider, indicating an authentication error. ```string "[openai] Upstream provider error" ``` -------------------------------- ### Basic Text Generation with OpenRouter Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/INDEX.md Demonstrates basic text generation using a specified OpenRouter model. Ensure the '@openrouter/ai-sdk-provider' package is imported. ```typescript // Basic usage import { openrouter } from '@openrouter/ai-sdk-provider'; const { text } = await generateText({ model: openrouter('openai/gpt-4o'), prompt: 'Hello', }); ``` -------------------------------- ### Basic OpenRouter Import and Usage Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/imports-and-exports.md Import `createOpenRouter` and `generateText` for basic text generation. Requires an API key from environment variables. ```typescript import { createOpenRouter } from '@openrouter/ai-sdk-provider'; import { generateText } from 'ai'; const openrouter = createOpenRouter({ apiKey: process.env.OPENROUTER_API_KEY, }); const model = openrouter('openai/gpt-4o'); const { text } = await generateText({ model, prompt: 'Hello', }); ``` -------------------------------- ### Build and Test Commands Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/CLAUDE.md Common pnpm commands for building, testing, and checking code style. ```bash pnpm build # Build with tsup (ESM + CJS outputs) pnpm dev # Watch mode development build pnpm test # Run all tests (node + edge environments) pnpm test:node # Run Node.js tests only pnpm test:edge # Run Edge runtime tests only pnpm test:e2e # Run E2E tests (requires OPENROUTER_API_KEY in .env.e2e) pnpm typecheck # TypeScript type checking pnpm stylecheck # Biome linting + formatting check pnpm format # Auto-fix formatting issues pnpm changeset # Create a changeset for release ``` -------------------------------- ### Project Source Structure Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/CLAUDE.md Overview of the project's directory structure. ```tree src/ ├── provider.ts # Factory: createOpenRouter() and default openrouter instance ├── facade.ts # Deprecated OpenRouter class (legacy support) ├── chat/index.ts # OpenRouterChatLanguageModel - main chat implementation ├── completion/index.ts # OpenRouterCompletionLanguageModel - completions ├── embedding/index.ts # OpenRouterEmbeddingModel - embeddings ├── types/ # TypeScript interfaces for settings ├── schemas/ # Zod schemas for request/response validation └── internal/ # Conditional export: @openrouter/ai-sdk-provider/internal ``` -------------------------------- ### Generate Single Embedding Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/examples.md Generates a single embedding vector for a given text value using a specified text embedding model. Ensure the 'ai' and '@openrouter/ai-sdk-provider' packages are installed. ```typescript import { embed } from 'ai'; import { openrouter } from '@openrouter/ai-sdk-provider'; const model = openrouter.textEmbeddingModel('openai/text-embedding-3-small'); const { embedding } = await embed({ model, value: 'The quick brown fox jumps over the lazy dog.', }); console.log('Embedding vector:', embedding); console.log('Dimension:', embedding.length); ``` -------------------------------- ### createOpenRouter() Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/MANIFEST.txt Creates an instance of the OpenRouter client. This is the primary function for initializing the SDK. ```APIDOC ## createOpenRouter() ### Description Initializes and returns an instance of the OpenRouter client, which is used to interact with the AI models. ### Method `createOpenRouter` ### Parameters This function accepts an optional `OpenRouterProviderSettings` object for configuration. ### Request Example ```typescript import { createOpenRouter } from '@openrouter/ai-sdk-provider'; const openrouter = createOpenRouter({ apiKey: 'YOUR_API_KEY', }); ``` ### Response Returns an instance of the OpenRouter client. ``` -------------------------------- ### Enable Reasoning with Max Tokens Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/plugins-and-features.md Enable reasoning tokens for models that support it to improve accuracy by allowing them more time to think. This example uses 'max_tokens' to limit the reasoning effort. ```typescript const model = openrouter.chat('openai/gpt-4o', { reasoning: { enabled: true, max_tokens: 10000, }, }); const { text } = await generateText({ model, prompt: 'Solve this complex problem...', }); ``` -------------------------------- ### Type-Safe Configuration with OpenRouter Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/examples.md Demonstrates how to create a type-safe OpenRouter client with custom chat settings, including temperature, max tokens, and plugins. Ensure the OPENROUTER_API_KEY environment variable is set. ```typescript import { generateText } from 'ai'; import { createOpenRouter } from '@openrouter/ai-sdk-provider'; import type { OpenRouterProvider, OpenRouterChatSettings, } from '@openrouter/ai-sdk-provider'; const openrouter: OpenRouterProvider = createOpenRouter({ apiKey: process.env.OPENROUTER_API_KEY, }); const settings: OpenRouterChatSettings = { temperature: 0.7, maxTokens: 1000, plugins: [ { id: 'web', max_results: 5, }, ], }; const model = openrouter('openai/gpt-4o', settings); const result = await generateText({ model, prompt: 'What is the weather today?', }); console.log(result.text); ``` -------------------------------- ### Generate Object with Strict Structured Outputs Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/plugins-and-features.md Example of generating a structured object using `generateObject` with strict mode enabled for better validation. Ensure the model and schema are correctly configured. ```typescript import { generateObject } from 'ai'; import { z } from 'zod'; const model = openrouter.chat('openai/gpt-4o', { structuredOutputs: { strict: true, // Use strict mode for better validation }, }); const { object } = await generateObject({ model, schema: z.object({ name: z.string(), age: z.number(), }), prompt: 'Generate a person', }); ``` -------------------------------- ### Create OpenRouter Provider Instance Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/api-reference.md Use `createOpenRouter` to instantiate a provider. Configure with API key, base URL, and application details. This is the entry point for interacting with OpenRouter models. ```typescript import { createOpenRouter } from '@openrouter/ai-sdk-provider'; import { generateText } from 'ai'; const openrouter = createOpenRouter({ apiKey: process.env.OPENROUTER_API_KEY, baseURL: 'https://openrouter.ai/api/v1', appName: 'MyApp', appUrl: 'https://myapp.com', }); const model = openrouter('openai/gpt-4o'); const { text } = await generateText({ model, prompt: 'Hello, world!', }); ``` -------------------------------- ### Configured Text Generation with OpenRouter Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/README.md Shows how to configure the OpenRouter provider with API key and application details, then use it for text generation with specific model parameters like temperature and maxTokens. ```typescript import { createOpenRouter } from '@openrouter/ai-sdk-provider'; import { generateText } from 'ai'; const openrouter = createOpenRouter({ apiKey: process.env.OPENROUTER_API_KEY, appName: 'MyApp', appUrl: 'https://myapp.com', }); const { text } = await generateText({ model: openrouter('openai/gpt-4o', { temperature: 0.7, maxTokens: 500, }), prompt: 'Explain quantum computing.', }); console.log(text); ``` -------------------------------- ### Enable Reasoning with Effort Levels Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/plugins-and-features.md Enable reasoning tokens for models that support it to improve accuracy by allowing them more time to think. This example uses 'effort' levels to control the reasoning process. ```typescript const model = openrouter.chat('openai/gpt-4o', { reasoning: { enabled: true, effort: 'high', }, }); const { text } = await generateText({ model, prompt: 'Solve this complex problem...', }); ``` -------------------------------- ### Add a Changeset Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/CONTRIBUTING.md Use this command to add a changeset file describing your changes. This is crucial for automated release notes and version bumping. ```bash pnpm changeset ``` -------------------------------- ### Configure OpenRouter Model with Options Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/INDEX.md Illustrates how to configure an OpenRouter model with specific options like temperature and maxTokens. These options customize the model's behavior. ```typescript // Chat with options const model = openrouter('openai/gpt-4o', { temperature: 0.7, maxTokens: 1000, }); ``` -------------------------------- ### Handle API Errors with OpenRouter SDK Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/INDEX.md Provides an example of how to handle potential API errors when using the OpenRouter SDK. It checks if the caught error is an instance of APICallError for specific error handling. ```typescript // Error handling try { // ... } catch (error) { if (error instanceof APICallError) { console.error(error.message); } } ``` -------------------------------- ### Create Video Generation Model Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/api-reference.md Instantiates an OpenRouter video generation model. Requires the model ID and optional configuration settings like poll interval. ```typescript const model = openrouter.videoModel('openai/gpt-4-vision', { pollIntervalMs: 1000, maxPollTimeMs: 300000, }); ``` -------------------------------- ### Use Tools/Functions with Text Generation Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/README.md Integrate tools or functions into your text generation prompts. This allows the model to call external tools to get information. Set maxToolRoundtrips to control the number of tool calls. ```typescript const result = await generateText({ model: openrouter('openai/gpt-4o'), tools: { search: { description: 'Search the web', parameters: { type: 'object' as const, properties: { query: { type: 'string' } }, required: ['query'], }, execute: async ({ query }) => '...results...', }, }, prompt: 'Search for AI news', maxToolRoundtrips: 2, }); ``` -------------------------------- ### Clone Repository Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/CONTRIBUTING.md Clone the OpenRouter AI SDK Provider repository locally after forking it on GitHub. ```bash git clone https://github.com/YOUR-USERNAME/ai-sdk-provider.git cd ai-sdk-provider ``` -------------------------------- ### Configure Built-in Web Search Options Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/plugins-and-features.md Alternative configuration for models with native web search support. Specify options like max_results, search_prompt, and engine. ```typescript const model = openrouter.chat('openai/gpt-4o', { web_search_options: { max_results: 10, search_prompt: 'Search for the most recent information', engine: 'native', }, }); ``` -------------------------------- ### Use Server Tool for Web Search Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/plugins-and-features.md Alternatively, use the server tool for web search for more control. This requires importing generateText and openrouter. ```typescript import { generateText } from 'ai'; import { openrouter } from '@openrouter/ai-sdk-provider'; const result = await generateText({ model: openrouter('openai/gpt-4o'), tools: { web_search: openrouter.tools.webSearch({ maxResults: 5, engine: 'exa', }), }, prompt: 'What are the latest news about AI?', }); ``` -------------------------------- ### Configure Prompt Caching for Anthropic Models Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/examples.md Utilize 'cache_control' to manage prompt caching for models like Anthropic's Claude. Specify the 'type' (e.g., 'ephemeral') and 'ttl' for the cache. This example also includes usage accounting. ```typescript import { generateText } from 'ai'; import { openrouter } from '@openrouter/ai-sdk-provider'; const largeDocument = '...'; // Large content const model = openrouter('anthropic/claude-3.5-sonnet', { cache_control: { type: 'ephemeral', ttl: '1h' }, usage: { include: true }, }); const result = await generateText({ model, messages: [ { role: 'user', content: [ { type: 'text', text: 'Analyze this document:', }, { type: 'text', text: largeDocument, providerOptions: { openrouter: { cacheControl: { type: 'ephemeral' }, }, }, }, { type: 'text', text: 'What are the main points?', }, ], }, ], }); console.log('Analysis:', result.text); // Check cache effectiveness const usage = result.providerMetadata?.openrouter?.usage; if (usage?.promptTokensDetails?.cachedTokens) { console.log('Cached tokens:', usage.promptTokensDetails.cachedTokens); console.log('New tokens:', usage.promptTokens - usage.promptTokensDetails.cachedTokens); } ``` -------------------------------- ### Create Custom OpenRouter Provider Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/INDEX.md Shows how to create a custom OpenRouter provider instance, typically used for setting API keys or other configurations. Requires importing 'createOpenRouter'. ```typescript // Custom provider import { createOpenRouter } from '@openrouter/ai-sdk-provider'; const provider = createOpenRouter({ apiKey: process.env.OPENROUTER_API_KEY, }); ``` -------------------------------- ### Stream Large Component Generation with Fine-grained Tool Streaming Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/README.md Use fine-grained tool streaming to generate and stream large, nested JSON structures like UI component trees. This example demonstrates streaming a component schema using Zod for validation. The 'anthropic-beta' header is configured at the provider level. ```typescript import { createOpenRouter } from '@openrouter/ai-sdk-provider'; import { streamObject } from 'ai'; import { z } from 'zod'; const componentSchema = z.object({ type: z.string(), props: z.record(z.any()), children: z.array(z.lazy(() => componentSchema)).optional(), }); const provider = createOpenRouter({ apiKey: process.env.OPENROUTER_API_KEY, headers: { 'anthropic-beta': 'fine-grained-tool-streaming-2025-05-14', }, }); const model = provider.chat('anthropic/claude-sonnet-4'); const result = await streamObject({ model, schema: componentSchema, prompt: 'Create a responsive dashboard layout', }); for await (const partialComponent of result.partialObjectStream) { console.log('Partial component:', partialComponent); } ``` -------------------------------- ### Complete Chat Application with OpenRouter Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/examples.md Builds an interactive command-line chat application using the OpenRouter SDK. It maintains conversation history and allows users to interact with the AI. Requires the 'ai' and '@openrouter/ai-sdk-provider' packages. ```typescript import { generateText } from 'ai'; import { openrouter } from '@openrouter/ai-sdk-provider'; import * as readline from 'readline'; const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); const model = openrouter('openai/gpt-4o', { temperature: 0.7, }); const messages: Array<{ role: 'user' | 'assistant'; content: string }> = [ { role: 'user', content: 'You are a helpful assistant.', }, ]; async function chat(userInput: string) { messages.push({ role: 'user', content: userInput, }); const result = await generateText({ model, messages, }); messages.push({ role: 'assistant', content: result.text, }); return result.text; } async function main() { console.log('Chat started. Type "exit" to quit.'); const askQuestion = () => { rl.question('You: ', async (input) => { if (input.toLowerCase() === 'exit') { rl.close(); return; } const response = await chat(input); console.log('Assistant:', response); askQuestion(); }); }; askQuestion(); } main().catch(console.error); ``` -------------------------------- ### Configure Web Search Plugin Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/plugins-and-features.md Enable native web search for models that support it. Configure the plugin with options like max_results, search_prompt, and engine. ```typescript const model = openrouter.chat('openai/gpt-4o', { plugins: [ { id: 'web', max_results: 5, search_prompt: 'Find the latest news about AI', engine: 'exa', }, ], }); const { text } = await generateText({ model, prompt: 'What are the latest AI developments?', }); ``` -------------------------------- ### Create OpenRouter Function Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/imports-and-exports.md Creates an instance of the OpenRouter provider. Use this to initialize the SDK with optional settings. ```typescript export function createOpenRouter( options?: OpenRouterProviderSettings ): OpenRouterProvider ``` -------------------------------- ### Create OpenRouter Completion Model Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/api-reference.md Instantiate a completion model using `openrouter.completion()`. Configure with model ID and max tokens. Use for text generation without message structure. ```typescript const model = openrouter.completion('openai/gpt-3.5-turbo-instruct', { maxTokens: 500, }); const { text } = await generateText({ model, prompt: 'Once upon a time', }); ``` -------------------------------- ### Using the Default OpenRouter Instance Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/imports-and-exports.md Import the default `openrouter` instance for simplified model instantiation. Useful when API key is configured globally or not needed for the specific call. ```typescript import { openrouter } from '@openrouter/ai-sdk-provider'; import { generateText } from 'ai'; const { text } = await generateText({ model: openrouter('openai/gpt-4o'), prompt: 'Hello', }); ``` -------------------------------- ### Create an Empty Changeset Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/CONTRIBUTING.md If your changes do not require a release (e.g., documentation, tests, CI configuration), create an empty changeset using this command. ```bash pnpm changeset --empty ``` -------------------------------- ### openrouter (default instance) Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/MANIFEST.txt Provides a default instance of the OpenRouter client, pre-configured for immediate use. ```APIDOC ## openrouter (default instance) ### Description Access a pre-configured default instance of the OpenRouter client. This is useful for quick setup and common use cases. ### Method `openrouter` (default export) ### Parameters This is a default instance and does not require parameters for access. ### Request Example ```typescript import openrouter from '@openrouter/ai-sdk-provider'; // Use the default instance directly const result = await openrouter.chat.completions.create({ model: 'openrouter/auto', messages: [{ role: 'user', content: 'Hello!' }], }); ``` ### Response Returns the default initialized OpenRouter client instance. ``` -------------------------------- ### OpenRouter Legacy Class Constructor Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/api-reference.md Initializes the legacy OpenRouter provider with optional settings. Use createOpenRouter() for new projects. ```typescript constructor(options?: OpenRouterProviderSettings) ``` -------------------------------- ### convertToOpenRouterCompletionPrompt Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/message-formats.md Converts AI SDK prompt format to a plain text prompt suitable for the completion API. It extracts text from the prompt structure and concatenates messages. ```APIDOC ## convertToOpenRouterCompletionPrompt() ### Description Converts AI SDK prompt format to completion prompt format. Extracts text from the prompt structure, concatenates system and user messages, and returns a plain text prompt for the completion API. ### Function Signature ```typescript function convertToOpenRouterCompletionPrompt( options: { prompt: LanguageModelV3Prompt; inputFormat: 'prompt' } ): { prompt: string } ``` ``` -------------------------------- ### Code Style Checks and Formatting Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/CONTRIBUTING.md Utilize Biome for code linting and formatting. Run 'pnpm stylecheck' to check, and 'pnpm format' to automatically fix formatting issues. ```bash # Check both linting and formatting pnpm stylecheck # Fix formatting issues automatically pnpm format # Run linting and formatting checks pnpm stylecheck ``` -------------------------------- ### Basic Text Generation with OpenRouter Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/README.md Demonstrates basic text generation using the `generateText` function with an OpenRouter model. Ensure the model identifier is correctly specified. ```typescript import { openrouter } from '@openrouter/ai-sdk-provider'; import { generateText } from 'ai'; const { text } = await generateText({ model: openrouter('openai/gpt-4o'), prompt: 'Write a haiku about programming.', }); console.log(text); ``` -------------------------------- ### OpenRouterVideoModel Constructor Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/imports-and-exports.md Constructor signature for OpenRouterVideoModel. It takes model ID, settings, and configuration as arguments. ```typescript constructor( modelId: OpenRouterVideoModelId, settings: OpenRouterVideoSettings, config: OpenRouterVideoConfig ) ``` -------------------------------- ### OpenRouter Provider Factory Usage Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/README.md Illustrates how to create an OpenRouter provider instance using the factory pattern and then use it to instantiate different types of models like chat, completion, embedding, image, and video models. ```typescript const openrouter = createOpenRouter(options); const chatModel = openrouter.chat('openai/gpt-4o', settings); const completionModel = openrouter.completion('openai/gpt-3.5-turbo-instruct', settings); const embeddingModel = openrouter.textEmbeddingModel('openai/text-embedding-3-small', settings); const imageModel = openrouter.imageModel('black-forest-labs/flux-1.1-pro', settings); const videoModel = openrouter.videoModel('model-id', settings); ``` -------------------------------- ### Create OpenRouter Chat Model Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/api-reference.md Instantiate a chat model using `openrouter.chat()`. Configure with model ID, temperature, max tokens, and plugins. Suitable for multi-turn conversations. ```typescript const model = openrouter.chat('openai/gpt-4o', { temperature: 0.7, maxTokens: 1000, plugins: [{ id: 'web' }], }); const { text } = await generateText({ model, messages: [{ role: 'user', content: 'What is AI?' }], }); ``` -------------------------------- ### Enable Anthropic Fine-grained Tool Streaming via Provider Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/README.md Configure the OpenRouter provider with the 'anthropic-beta' header to enable fine-grained tool streaming. This is a beta feature from Anthropic and is useful for streaming tool parameters without buffering, reducing latency for large schemas. It's specific to Anthropic models and will be ignored for others. ```typescript import { createOpenRouter } from '@openrouter/ai-sdk-provider'; import { streamObject } from 'ai'; const provider = createOpenRouter({ apiKey: process.env.OPENROUTER_API_KEY, headers: { 'anthropic-beta': 'fine-grained-tool-streaming-2025-05-14', }, }); const model = provider.chat('anthropic/claude-sonnet-4'); const result = await streamObject({ model, schema: yourLargeSchema, prompt: 'Generate a complex object...', }); for await (const partialObject of result.partialObjectStream) { console.log(partialObject); } ``` -------------------------------- ### OpenRouterProviderSettings Interface Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/imports-and-exports.md Configuration options for initializing the OpenRouter provider. Includes settings for base URL, API key, compatibility, and fetch options. ```typescript export interface OpenRouterProviderSettings { baseURL?: string; baseUrl?: string; apiKey?: string; headers?: Record; compatibility?: 'strict' | 'compatible'; fetch?: typeof fetch; extraBody?: Record; api_keys?: Record; appName?: string; appUrl?: string; } ``` -------------------------------- ### Web Search using Server Tool Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/examples.md Integrates web search functionality directly into text generation using server-side tools. This method requires the 'ai' and '@openrouter/ai-sdk-provider' packages. ```typescript import { generateText } from 'ai'; import { openrouter } from '@openrouter/ai-sdk-provider'; const model = openrouter('openai/gpt-4o'); const result = await generateText({ model, tools: { web_search: openrouter.tools.webSearch({ maxResults: 5, engine: 'exa', }), }, prompt: 'Search for the latest AI news.', }); console.log(result.text); ``` -------------------------------- ### Configure Video Generation Settings Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/configuration.md Set specific options for video generation models like audio generation, polling intervals, and maximum wait times. Defaults are often derived from the endpoint's capabilities. ```typescript const model = openrouter.videoModel('openai/gpt-4-vision', { generateAudio: true, pollIntervalMs: 1000, maxPollTimeMs: 300000, extraBody: { // Custom parameters }, }); ``` -------------------------------- ### createOpenRouter() Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/imports-and-exports.md Creates an instance of the OpenRouter provider, which can be used to interact with OpenRouter services. It accepts optional settings for configuration. ```APIDOC ## createOpenRouter() ### Description Creates an instance of the OpenRouter provider. ### Signature ```typescript export function createOpenRouter(options?: OpenRouterProviderSettings): OpenRouterProvider ``` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters * **options** (OpenRouterProviderSettings) - Optional - Settings for configuring the OpenRouter provider, such as baseURL, apiKey, and fetch implementation. ``` -------------------------------- ### Configure OpenRouter Completion Model Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/configuration.md Use this snippet to set parameters for completion models like max tokens, temperature, suffix, and log probabilities. The source file is src/types/openrouter-completion-settings.ts. ```typescript const model = openrouter.completion('openai/gpt-3.5-turbo-instruct', { maxTokens: 500, temperature: 0.5, suffix: '\n\nEnd of text.', logprobs: true, }); ``` -------------------------------- ### Exporting from Main Entry Point Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/imports-and-exports.md Exports all public members from the facade, provider, and types modules. This is the main entry point for the package. ```typescript export * from './facade'; export * from './provider'; export * from './types'; ``` -------------------------------- ### Use Web Search Tool Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/api-reference.md Configures and uses the server-side web search tool with a specified model. Allows setting search parameters like max results and engine. ```typescript import { generateText } from 'ai'; const model = openrouter('openai/gpt-4o'); const result = await generateText({ model, tools: { web_search: openrouter.tools.webSearch({ maxResults: 5, engine: 'exa', }), }, prompt: 'What are the latest news about AI?', }); ``` -------------------------------- ### Configure Anthropic Prompt Caching Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/plugins-and-features.md Set up ephemeral prompt caching for Anthropic models to reduce token usage. Specify a time-to-live (TTL) of 5 minutes or 1 hour. ```typescript type CacheControl = { type: 'ephemeral'; ttl?: '5m' | '1h'; // 5 minutes (default) or 1 hour }; const model = openrouter.chat('anthropic/claude-3.5-sonnet', { cache_control: { type: 'ephemeral', ttl: '1h' }, }); ``` -------------------------------- ### Run All Tests Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/CONTRIBUTING.md Execute all project tests, including Node.js and Edge environment tests, using 'pnpm test'. Specific environments can be targeted with 'pnpm test:node' or 'pnpm test:edge'. ```bash # Run all tests pnpm test # Run only Node.js tests pnpm test:node # Run only Edge environment tests pnpm test:edge ``` -------------------------------- ### OpenRouterProvider.completion Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/api-reference.md Creates an OpenRouter completion model for text generation without message structure. ```APIDOC ## completion() ### Description Creates an OpenRouter completion model for text generation without message structure. ### Signature ```typescript completion( modelId: OpenRouterCompletionModelId, settings?: OpenRouterCompletionSettings ): OpenRouterCompletionLanguageModel ``` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **modelId** (`string`) - Required - Model ID (e.g., `'openai/gpt-3.5-turbo-instruct'`) - **settings** (`OpenRouterCompletionSettings`) - Optional - Model-specific configuration ### Returns `OpenRouterCompletionLanguageModel` - A language model configured for completions. ### Example ```typescript const model = openrouter.completion('openai/gpt-3.5-turbo-instruct', { maxTokens: 500, }); const { text } = await generateText({ model, prompt: 'Once upon a time', }); ``` ``` -------------------------------- ### Route by Cost Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/examples.md Routes AI model requests to the cheapest available provider. Specify providers to include in the sorting. ```typescript import { generateText } from 'ai'; import { openrouter } from '@openrouter/ai-sdk-provider'; const model = openrouter('openai/gpt-4o', { provider: { sort: 'price', // Use cheapest provider only: ['anthropic', 'openai', 'google'], }, }); const { text } = await generateText({ model, prompt: 'Hello', }); ``` -------------------------------- ### videoModel() Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/api-reference.md Creates an OpenRouter video generation model. This model can be configured with polling intervals for asynchronous video generation tasks. ```APIDOC ## videoModel() ### Description Creates an OpenRouter video generation model. ### Method ```typescript videoModel(modelId: OpenRouterVideoModelId, settings?: OpenRouterVideoSettings): OpenRouterVideoModel ``` ### Parameters #### Path Parameters - **modelId** (string) - required - Model ID for video generation - **settings** (OpenRouterVideoSettings) - optional - Model-specific configuration ### Request Example ```typescript const model = openrouter.videoModel('openai/gpt-4-vision', { pollIntervalMs: 1000, maxPollTimeMs: 300000, }); ``` ### Response #### Success Response - **OpenRouterVideoModel** - A video generation model. ``` -------------------------------- ### Generate Text with Custom Configuration Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/examples.md Shows how to generate text with custom model configurations like temperature and max tokens. Requires setting the OPENROUTER_API_KEY environment variable. ```typescript import { generateText } from 'ai'; import { createOpenRouter } from '@openrouter/ai-sdk-provider'; const openrouter = createOpenRouter({ apiKey: process.env.OPENROUTER_API_KEY, appName: 'MyApp', appUrl: 'https://myapp.com', }); const { text } = await generateText({ model: openrouter('openai/gpt-4o', { temperature: 0.7, maxTokens: 500, }), prompt: 'Explain quantum computing in simple terms.', }); console.log(text); ``` -------------------------------- ### OpenRouter Import with Type Annotations Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/imports-and-exports.md Import `createOpenRouter` and specific types for typed OpenRouter instances and chat settings. Allows for explicit type definitions. ```typescript import { createOpenRouter } from '@openrouter/ai-sdk-provider'; import type { OpenRouterProvider, OpenRouterChatSettings, } from '@openrouter/ai-sdk-provider'; const openrouter: OpenRouterProvider = createOpenRouter(); const settings: OpenRouterChatSettings = { temperature: 0.7, maxTokens: 1000, }; const model = openrouter('openai/gpt-4o', settings); ``` -------------------------------- ### OpenRouter AI SDK Provider Repository Structure Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/README.md Provides an overview of the directory structure for the OpenRouter AI SDK Provider project. Helps in understanding the organization of source files. ```plaintext src/ provider.ts # Factory and main provider facade.ts # Deprecated class chat/ # Chat model implementation completion/ # Completion model embedding/ # Embedding model image/ # Image generation video/ # Video generation types/ # TypeScript type definitions schemas/ # Zod validation schemas utils/ # Utility functions tool/ # Server tools (web search) ``` -------------------------------- ### Streaming Structured Output (JSON) with streamObject Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/examples.md Demonstrates how to use `streamObject` to receive structured data incrementally as a stream. This is ideal for generating longer content like blog posts where partial results are useful. ```typescript import { streamObject } from 'ai'; import { openrouter } from '@openrouter/ai-sdk-provider'; import { z } from 'zod'; const model = openrouter('openai/gpt-4o'); const { partialObjectStream } = await streamObject({ model, schema: z.object({ title: z.string(), content: z.string(), }), prompt: 'Generate a blog post about AI.', }); for await (const partialObject of partialObjectStream) { console.log(partialObject); } ``` -------------------------------- ### Enable Anthropic Fine-grained Tool Streaming via Request Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/README.md Enable fine-grained tool streaming for a specific request by including the 'anthropic-beta' header. This method is useful for applying the beta feature on a per-request basis. Note that this header is only effective for Anthropic models. ```typescript import { createOpenRouter } from '@openrouter/ai-sdk-provider'; import { generateText } from 'ai'; const provider = createOpenRouter({ apiKey: process.env.OPENROUTER_API_KEY, }); const model = provider.chat('anthropic/claude-sonnet-4'); await generateText({ model, prompt: 'Hello', headers: { 'anthropic-beta': 'fine-grained-tool-streaming-2025-05-14', }, }); ``` -------------------------------- ### OpenRouterImageModel doGenerate Method Signature Source: https://github.com/openrouterteam/ai-sdk-provider/blob/main/_autodocs/api-reference.md Generates images based on a prompt. Returns generated images, warnings, provider metadata, response details, and usage information. ```typescript async doGenerate(options: ImageModelV3CallOptions): Promise<{ images: Array; warnings: Array; providerMetadata?: ImageModelV3ProviderMetadata; response: { timestamp: Date; modelId: string; headers: Record | undefined }; usage?: ImageModelV3Usage; }> ```