-
-
Notifications
You must be signed in to change notification settings - Fork 82
feat: Introduce OpenRouter adapter #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LuggaPugga
wants to merge
2
commits into
TanStack:main
Choose a base branch
from
LuggaPugga:feat/openrouter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,231 @@ | ||
| --- | ||
| title: OpenRouter Adapter | ||
| id: openrouter-adapter | ||
| --- | ||
|
|
||
| The OpenRouter adapter provides access to 300+ AI models from various providers through a single unified API, including models from OpenAI, Anthropic, Google, Meta, Mistral, and many more. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| npm install @tanstack/ai-openrouter | ||
| ``` | ||
|
|
||
| ## Basic Usage | ||
|
|
||
| ```typescript | ||
| import { chat } from "@tanstack/ai"; | ||
| import { openrouter } from "@tanstack/ai-openrouter"; | ||
|
|
||
| const adapter = openrouter(); | ||
|
|
||
| const stream = chat({ | ||
| adapter, | ||
| messages: [{ role: "user", content: "Hello!" }], | ||
| model: "openai/gpt-4o", | ||
| }); | ||
| ``` | ||
|
|
||
| ## Configuration | ||
|
|
||
| ```typescript | ||
| import { createOpenRouter, type OpenRouterConfig } from "@tanstack/ai-openrouter"; | ||
|
|
||
| const config: OpenRouterConfig = { | ||
| apiKey: process.env.OPENROUTER_API_KEY!, | ||
| baseURL: "https://openrouter.ai/api/v1", // Optional | ||
| httpReferer: "https://your-app.com", // Optional, for rankings | ||
| xTitle: "Your App Name", // Optional, for rankings | ||
| }; | ||
|
|
||
| const adapter = createOpenRouter(config.apiKey, config); | ||
| ``` | ||
|
|
||
| ## Available Models | ||
|
|
||
| OpenRouter provides access to 300+ models from various providers. Models use the format `provider/model-name`: | ||
|
|
||
| ```typescript | ||
| model: "openai/gpt-5.1" | ||
| model: "anthropic/claude-sonnet-4.5" | ||
| model: "google/gemini-3-pro-preview" | ||
| model: "meta-llama/llama-4-maverick" | ||
| model: "deepseek/deepseek-v3.2" | ||
| ``` | ||
|
|
||
| See the full list at [openrouter.ai/models](https://openrouter.ai/models). | ||
|
|
||
| ## Example: Chat Completion | ||
|
|
||
| ```typescript | ||
| import { chat, toStreamResponse } from "@tanstack/ai"; | ||
| import { openrouter } from "@tanstack/ai-openrouter"; | ||
|
|
||
| const adapter = openrouter(); | ||
|
|
||
| export async function POST(request: Request) { | ||
| const { messages } = await request.json(); | ||
|
|
||
| const stream = chat({ | ||
| adapter, | ||
| messages, | ||
| model: "openai/gpt-4o", | ||
| }); | ||
|
|
||
| return toStreamResponse(stream); | ||
| } | ||
| ``` | ||
|
|
||
| ## Example: With Tools | ||
|
|
||
| ```typescript | ||
| import { chat, toolDefinition } from "@tanstack/ai"; | ||
| import { openrouter } from "@tanstack/ai-openrouter"; | ||
| import { z } from "zod"; | ||
|
|
||
| const adapter = openrouter(); | ||
|
|
||
| const getWeatherDef = toolDefinition({ | ||
| name: "get_weather", | ||
| description: "Get the current weather", | ||
| inputSchema: z.object({ | ||
| location: z.string(), | ||
| }), | ||
| }); | ||
|
|
||
| const getWeather = getWeatherDef.server(async ({ location }) => { | ||
| return { temperature: 72, conditions: "sunny" }; | ||
| }); | ||
|
|
||
| const stream = chat({ | ||
| adapter, | ||
| messages, | ||
| model: "openai/gpt-4o", | ||
| tools: [getWeather], | ||
| }); | ||
| ``` | ||
|
|
||
| ## Web Search | ||
|
|
||
| OpenRouter supports web search through the `plugins` configuration. This enables real-time web search capabilities for any model: | ||
|
|
||
| ```typescript | ||
| const stream = chat({ | ||
| adapter, | ||
| messages: [{ role: "user", content: "What's the latest AI news?" }], | ||
| model: "openai/gpt-4o-mini", | ||
| providerOptions: { | ||
| plugins: [ | ||
| { | ||
| id: "web", | ||
| engine: "exa", // "native" or "exa" | ||
| max_results: 5, // default: 5 | ||
| }, | ||
| ], | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| Alternatively, use the `:online` model suffix: | ||
|
|
||
| ```typescript | ||
| const stream = chat({ | ||
| adapter, | ||
| messages, | ||
| model: "openai/gpt-4o-mini:online", | ||
| }); | ||
| ``` | ||
|
|
||
| ## Provider Options | ||
|
|
||
| OpenRouter supports extensive provider-specific options: | ||
|
|
||
| ```typescript | ||
| const stream = chat({ | ||
| adapter, | ||
| messages, | ||
| model: "openai/gpt-4o", | ||
| providerOptions: { | ||
| temperature: 0.7, | ||
| max_tokens: 1000, | ||
| top_p: 0.9, | ||
| top_k: 40, | ||
| frequency_penalty: 0.5, | ||
| presence_penalty: 0.5, | ||
| repetition_penalty: 1.1, | ||
| seed: 42, | ||
| tool_choice: "auto", | ||
| response_format: { type: "json_object" }, | ||
| // Routing options | ||
| models: ["openai/gpt-4o", "anthropic/claude-3.5-sonnet"], // Fallback models | ||
| route: "fallback", | ||
| // Provider preferences | ||
| provider: { | ||
| order: ["OpenAI", "Anthropic"], | ||
| allow_fallbacks: true, | ||
| }, | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| ## Environment Variables | ||
|
|
||
| Set your API key in environment variables: | ||
|
|
||
| ```bash | ||
| OPENROUTER_API_KEY=sk-or-... | ||
| ``` | ||
|
|
||
| ## Model Routing | ||
|
|
||
| OpenRouter can automatically route requests to the best available provider: | ||
|
|
||
| ```typescript | ||
| const stream = chat({ | ||
| adapter, | ||
| messages, | ||
| model: "openrouter/auto", // Automatic model selection | ||
| providerOptions: { | ||
| models: [ | ||
| "openai/gpt-4o", | ||
| "anthropic/claude-3.5-sonnet", | ||
| "google/gemini-pro", | ||
| ], | ||
| route: "fallback", // Use fallback if primary fails | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| ## API Reference | ||
|
|
||
| ### `openrouter(config?)` | ||
|
|
||
| Creates an OpenRouter adapter with automatic API key detection from `OPENROUTER_API_KEY`. | ||
|
|
||
| **Parameters:** | ||
|
|
||
| - `config.baseURL?` - Custom base URL (optional) | ||
| - `config.httpReferer?` - HTTP Referer header for rankings (optional) | ||
| - `config.xTitle?` - X-Title header for rankings (optional) | ||
|
|
||
| **Returns:** An OpenRouter adapter instance. | ||
|
|
||
| ### `createOpenRouter(apiKey, config?)` | ||
|
|
||
| Creates an OpenRouter adapter with explicit API key. | ||
|
|
||
| **Parameters:** | ||
|
|
||
| - `apiKey` - OpenRouter API key (required) | ||
| - `config.baseURL?` - Custom base URL (optional) | ||
| - `config.httpReferer?` - HTTP Referer header (optional) | ||
| - `config.xTitle?` - X-Title header (optional) | ||
|
|
||
| **Returns:** An OpenRouter adapter instance. | ||
|
|
||
| ## Next Steps | ||
|
|
||
| - [Getting Started](../getting-started/quick-start) - Learn the basics | ||
| - [Tools Guide](../guides/tools) - Learn about tools | ||
| - [Other Adapters](./openai) - Explore other providers | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,12 +38,39 @@ | |
| A powerful, type-safe AI SDK for building AI-powered applications. | ||
|
|
||
| - Provider-agnostic adapters (OpenAI, Anthropic, Gemini, Ollama, etc.) | ||
| - **Multimodal content support** - Send images, audio, video, and documents | ||
| - Chat completion, streaming, and agent loop strategies | ||
| - Headless chat state management with adapters (SSE, HTTP stream, custom) | ||
| - Type-safe tools with server/client execution | ||
| - Isomorphic type-safe tools with server/client execution | ||
| - **Enhanced integration with TanStack Start** - Share implementations between AI tools and server functions | ||
|
|
||
| ### <a href="https://tanstack.com/ai">Read the docs →</b></a> | ||
|
|
||
| ## Bonus: TanStack Start Integration | ||
|
|
||
| TanStack AI works with **any** framework (Next.js, Express, Remix, etc.). | ||
|
|
||
| **With TanStack Start**, you get a bonus: share implementations between AI tools and server functions with `createServerFnTool`: | ||
|
|
||
| ```typescript | ||
| import { createServerFnTool } from '@tanstack/ai-react' | ||
|
|
||
| // Define once, get AI tool AND server function (TanStack Start only) | ||
| const getProducts = createServerFnTool({ | ||
| name: 'getProducts', | ||
| inputSchema: z.object({ query: z.string() }), | ||
| execute: async ({ query }) => db.products.search(query), | ||
| }) | ||
|
|
||
| // Use in AI chat | ||
| chat({ tools: [getProducts.server] }) | ||
|
|
||
| // Call directly from components (no API endpoint needed!) | ||
| const products = await getProducts.serverFn({ query: 'laptop' }) | ||
| ``` | ||
|
|
||
| No duplicate logic, full type safety, automatic validation. The `serverFn` feature requires TanStack Start. See [docs](https://tanstack.com/ai) for details. | ||
|
|
||
|
Comment on lines
+49
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix markdown heading level + code sample missing
-### <a href="https://tanstack.com/ai">Read the docs →</b></a>
+## <a href="https://tanstack.com/ai">Read the docs →</b></a>
@@
```typescript
import { createServerFnTool } from '@tanstack/ai-react'
+import { z } from 'zod'🤖 Prompt for AI Agents |
||
| ## Get Involved | ||
|
|
||
| - We welcome issues and pull requests! | ||
|
|
@@ -88,7 +115,7 @@ We're looking for TanStack AI Partners to join our mission! Partner with us to p | |
|
|
||
| - <a href="https://github.com/tanstack/config"><b>TanStack Config</b></a> – Tooling for JS/TS packages | ||
| - <a href="https://github.com/tanstack/db"><b>TanStack DB</b></a> – Reactive sync client store | ||
| - <a href="https://github.com/tanstack/devtools">TanStack Devtools</a> – Unified devtools panel | ||
| - <a href="https://github.com/tanstack/devtools"><b>TanStack Devtools</b></a> – Unified devtools panel | ||
| - <a href="https://github.com/tanstack/form"><b>TanStack Form</b></a> – Type‑safe form state | ||
| - <a href="https://github.com/tanstack/pacer"><b>TanStack Pacer</b></a> – Debouncing, throttling, batching | ||
| - <a href="https://github.com/tanstack/query"><b>TanStack Query</b></a> – Async state & caching | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: TanStack/ai
Length of output: 809
🏁 Script executed:
Repository: TanStack/ai
Length of output: 600
🏁 Script executed:
Repository: TanStack/ai
Length of output: 3031
Fix the
createOpenRoutercall in the example—removeapiKeyfrom the config object.The function signature is
createOpenRouter(apiKey: string, config?: Omit<OpenRouterConfig, 'apiKey'>). The second parameter must excludeapiKey, so either:apiKey:createOpenRouter(config.apiKey, { baseURL: "...", httpReferer: "...", xTitle: "..." })createOpenRouter(process.env.OPENROUTER_API_KEY!, { baseURL: "...", httpReferer: "...", xTitle: "..." })The current example passes the full config (with
apiKey), which violates the type signature.🤖 Prompt for AI Agents