Skip to content

Commit

Permalink
feat (docs): add OpenRouter AI provider documentation (#4073)
Browse files Browse the repository at this point in the history
Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
  • Loading branch information
lgrammel and devin-ai-integration[bot] authored Dec 12, 2024
1 parent e10a3ef commit 56290af
Showing 1 changed file with 129 additions and 0 deletions.
129 changes: 129 additions & 0 deletions content/providers/03-community-providers/12-openrouter.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
---
title: OpenRouter
description: OpenRouter Provider for the AI SDK
---

# OpenRouter

[OpenRouter](https://openrouter.ai/) is a unified API gateway that provides access to hundreds of AI models from leading providers like Anthropic, Google, Meta, Mistral, and more. The OpenRouter provider for the AI SDK enables seamless integration with all these models while offering unique advantages:

- **Universal Model Access**: One API key for hundreds of models from multiple providers
- **Cost-Effective**: Pay-as-you-go pricing with no monthly fees or commitments
- **Transparent Pricing**: Clear per-token costs for all models
- **High Availability**: Enterprise-grade infrastructure with automatic failover
- **Simple Integration**: Standardized API across all models
- **Latest Models**: Immediate access to new models as they're released

Learn more about OpenRouter's capabilities in the [OpenRouter Documentation](https://openrouter.ai/docs).

## Setup

The OpenRouter provider is available in the `@openrouter/ai-sdk-provider` module. You can install it with:

<Tabs items={['pnpm', 'npm', 'yarn']}>
<Tab>
<Snippet text="pnpm add @openrouter/ai-sdk-provider" dark />
</Tab>
<Tab>
<Snippet text="npm install @openrouter/ai-sdk-provider" dark />
</Tab>
<Tab>
<Snippet text="yarn add @openrouter/ai-sdk-provider" dark />
</Tab>
</Tabs>

## Provider Instance

To create an OpenRouter provider instance, use the `createOpenRouter` function:

```typescript
import { createOpenRouter } from '@openrouter/ai-sdk-provider';

const openrouter = createOpenRouter({
apiKey: 'YOUR_OPENROUTER_API_KEY',
});
```

You can obtain your OpenRouter API key from the [OpenRouter Dashboard](https://openrouter.ai/keys).

## Language Models

OpenRouter supports both chat and completion models. Use `openrouter.chatModel()` for chat models and `openrouter.completionModel()` for completion models:

```typescript
// Chat models (recommended)
const chatModel = openrouter.chatModel('anthropic/claude-3.5-sonnet');

// Completion models
const completionModel = openrouter.completionModel(
'meta-llama/llama-3.1-405b-instruct',
);
```

You can find the full list of available models in the [OpenRouter Models documentation](https://openrouter.ai/docs#models).

## Examples

Here are examples of using OpenRouter with the AI SDK:

### `generateText`

```javascript
import { createOpenRouter } from '@openrouter/ai-sdk-provider';
import { generateText } from 'ai';

const openrouter = createOpenRouter({
apiKey: 'YOUR_OPENROUTER_API_KEY',
});

const { text } = await generateText({
model: openrouter.chatModel('anthropic/claude-3.5-sonnet'),
prompt: 'What is OpenRouter?',
});

console.log(text);
```

### `streamText`

```javascript
import { createOpenRouter } from '@openrouter/ai-sdk-provider';
import { streamText } from 'ai';

const openrouter = createOpenRouter({
apiKey: 'YOUR_OPENROUTER_API_KEY',
});

const result = streamText({
model: openrouter.chatModel('meta-llama/llama-3.1-405b-instruct'),
prompt: 'Write a short story about AI.',
});

for await (const chunk of result) {
console.log(chunk);
}
```

## Advanced Features

OpenRouter offers several advanced features to enhance your AI applications:

1. **Model Flexibility**: Switch between hundreds of models without changing your code or managing multiple API keys.

2. **Cost Management**: Track usage and costs per model in real-time through the dashboard.

3. **Enterprise Support**: Available for high-volume users with custom SLAs and dedicated support.

4. **Cross-Provider Compatibility**: Use the same code structure across different model providers.

5. **Regular Updates**: Automatic access to new models and features as they become available.

For more information about these features and advanced configuration options, visit the [OpenRouter Documentation](https://openrouter.ai/docs).

## Additional Resources

- [OpenRouter Provider Repository](https://github.com/OpenRouterTeam/ai-sdk-provider)
- [OpenRouter Documentation](https://openrouter.ai/docs)
- [OpenRouter Dashboard](https://openrouter.ai/dashboard)
- [OpenRouter Discord Community](https://discord.gg/openrouter)
- [OpenRouter Status Page](https://status.openrouter.ai)

0 comments on commit 56290af

Please sign in to comment.