@openrouter/ai-sdk-provider
Version:
The [OpenRouter](https://openrouter.ai/) provider for the [Vercel AI SDK](https://sdk.vercel.ai/docs) gives access to over 300 large language models on the OpenRouter chat and completion APIs.
411 lines (393 loc) • 14.7 kB
text/typescript
import { LanguageModelV3, LanguageModelV3CallOptions, LanguageModelV3Content, LanguageModelV3FinishReason, LanguageModelV3Usage, SharedV3Warning, LanguageModelV3ResponseMetadata, SharedV3Headers, LanguageModelV3StreamPart } from '@ai-sdk/provider';
export { LanguageModelV3, LanguageModelV3Prompt } from '@ai-sdk/provider';
import { z } from 'zod/v4';
import * as models from '@openrouter/sdk/models';
declare enum ReasoningFormat {
Unknown = "unknown",
OpenAIResponsesV1 = "openai-responses-v1",
XAIResponsesV1 = "xai-responses-v1",
AnthropicClaudeV1 = "anthropic-claude-v1",
GoogleGeminiV1 = "google-gemini-v1"
}
declare enum ReasoningDetailType {
Summary = "reasoning.summary",
Encrypted = "reasoning.encrypted",
Text = "reasoning.text"
}
declare const ReasoningDetailUnionSchema: z.ZodUnion<readonly [z.ZodObject<{
type: z.ZodLiteral<ReasoningDetailType.Summary>;
summary: z.ZodString;
id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
format: z.ZodOptional<z.ZodNullable<z.ZodEnum<typeof ReasoningFormat>>>;
index: z.ZodOptional<z.ZodNumber>;
}, z.core.$strip>, z.ZodObject<{
type: z.ZodLiteral<ReasoningDetailType.Encrypted>;
data: z.ZodString;
id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
format: z.ZodOptional<z.ZodNullable<z.ZodEnum<typeof ReasoningFormat>>>;
index: z.ZodOptional<z.ZodNumber>;
}, z.core.$strip>, z.ZodObject<{
type: z.ZodLiteral<ReasoningDetailType.Text>;
text: z.ZodOptional<z.ZodNullable<z.ZodString>>;
signature: z.ZodOptional<z.ZodNullable<z.ZodString>>;
id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
format: z.ZodOptional<z.ZodNullable<z.ZodEnum<typeof ReasoningFormat>>>;
index: z.ZodOptional<z.ZodNumber>;
}, z.core.$strip>]>;
type ReasoningDetailUnion = z.infer<typeof ReasoningDetailUnionSchema>;
type OpenRouterChatModelId = string;
type OpenRouterChatSettings = {
/**
Modify the likelihood of specified tokens appearing in the completion.
Accepts a JSON object that maps tokens (specified by their token ID in
the GPT tokenizer) to an associated bias value from -100 to 100. You
can use this tokenizer tool to convert text to token IDs. Mathematically,
the bias is added to the logits generated by the model prior to sampling.
The exact effect will vary per model, but values between -1 and 1 should
decrease or increase likelihood of selection; values like -100 or 100
should result in a ban or exclusive selection of the relevant token.
As an example, you can pass {"50256": -100} to prevent the <|endoftext|>
token from being generated.
*/
logitBias?: Record<number, number>;
/**
Return the log probabilities of the tokens. Including logprobs will increase
the response size and can slow down response times. However, it can
be useful to understand better how the model is behaving.
Setting to true will return the log probabilities of the tokens that
were generated.
Setting to a number will return the log probabilities of the top n
tokens that were generated.
*/
logprobs?: boolean | number;
/**
Whether to enable parallel function calling during tool use. Default to true.
*/
parallelToolCalls?: boolean;
/**
A unique identifier representing your end-user, which can help OpenRouter to
monitor and detect abuse. Learn more.
*/
user?: string;
/**
* Plugin configurations for enabling various capabilities
*/
plugins?: Array<{
id: models.IdWeb;
max_results?: number;
search_prompt?: string;
engine?: models.Engine;
} | {
id: models.IdFileParser;
max_files?: number;
pdf?: {
engine?: models.PdfEngine;
};
} | {
id: models.IdModeration;
}>;
/**
* Built-in web search options for models that support native web search
*/
web_search_options?: {
/**
* Maximum number of search results to include
*/
max_results?: number;
/**
* Custom search prompt to guide the search query
*/
search_prompt?: string;
/**
* Search engine to use for web search
* - "native": Use provider's built-in web search
* - "exa": Use Exa's search API
* - undefined: Native if supported, otherwise Exa
* @see https://openrouter.ai/docs/features/web-search
*/
engine?: models.Engine;
};
/**
* Debug options for troubleshooting API requests.
* Only works with streaming requests.
* @see https://openrouter.ai/docs/api-reference/debugging
*/
debug?: {
/**
* When true, echoes back the request body that was sent to the upstream provider.
* The debug data will be returned as the first chunk in the stream with a `debug.echo_upstream_body` field.
* Sensitive data like user IDs and base64 content will be redacted.
*/
echo_upstream_body?: boolean;
};
/**
* Provider routing preferences to control request routing behavior
*/
provider?: {
/**
* List of provider slugs to try in order (e.g. ["anthropic", "openai"])
*/
order?: string[];
/**
* Whether to allow backup providers when primary is unavailable (default: true)
*/
allow_fallbacks?: boolean;
/**
* Only use providers that support all parameters in your request (default: false)
*/
require_parameters?: boolean;
/**
* Control whether to use providers that may store data
*/
data_collection?: models.DataCollection;
/**
* List of provider slugs to allow for this request
*/
only?: string[];
/**
* List of provider slugs to skip for this request
*/
ignore?: string[];
/**
* List of quantization levels to filter by (e.g. ["int4", "int8"])
*/
quantizations?: Array<models.Quantization>;
/**
* Sort providers by price, throughput, or latency
*/
sort?: models.ProviderSort;
/**
* Maximum pricing you want to pay for this request
*/
max_price?: {
prompt?: number | string;
completion?: number | string;
image?: number | string;
audio?: number | string;
request?: number | string;
};
/**
* Whether to restrict routing to only ZDR (Zero Data Retention) endpoints.
* When true, only endpoints that do not retain prompts will be used.
*/
zdr?: boolean;
};
} & OpenRouterSharedSettings;
type OpenRouterCompletionModelId = string;
type OpenRouterCompletionSettings = {
/**
Modify the likelihood of specified tokens appearing in the completion.
Accepts a JSON object that maps tokens (specified by their token ID in
the GPT tokenizer) to an associated bias value from -100 to 100. You
can use this tokenizer tool to convert text to token IDs. Mathematically,
the bias is added to the logits generated by the model prior to sampling.
The exact effect will vary per model, but values between -1 and 1 should
decrease or increase likelihood of selection; values like -100 or 100
should result in a ban or exclusive selection of the relevant token.
As an example, you can pass {"50256": -100} to prevent the <|endoftext|>
token from being generated.
*/
logitBias?: Record<number, number>;
/**
Return the log probabilities of the tokens. Including logprobs will increase
the response size and can slow down response times. However, it can
be useful to better understand how the model is behaving.
Setting to true will return the log probabilities of the tokens that
were generated.
Setting to a number will return the log probabilities of the top n
tokens that were generated.
*/
logprobs?: boolean | number;
/**
The suffix that comes after a completion of inserted text.
*/
suffix?: string;
} & OpenRouterSharedSettings;
type OpenRouterCompletionConfig = {
provider: string;
compatibility: 'strict' | 'compatible';
headers: () => Record<string, string | undefined>;
url: (options: {
modelId: string;
path: string;
}) => string;
fetch?: typeof fetch;
extraBody?: Record<string, unknown>;
};
declare class OpenRouterCompletionLanguageModel implements LanguageModelV3 {
readonly specificationVersion: "v3";
readonly provider = "openrouter";
readonly modelId: OpenRouterCompletionModelId;
readonly supportsImageUrls = true;
readonly supportedUrls: Record<string, RegExp[]>;
readonly defaultObjectGenerationMode: undefined;
readonly settings: OpenRouterCompletionSettings;
private readonly config;
constructor(modelId: OpenRouterCompletionModelId, settings: OpenRouterCompletionSettings, config: OpenRouterCompletionConfig);
private getArgs;
doGenerate(options: LanguageModelV3CallOptions): Promise<Awaited<ReturnType<LanguageModelV3['doGenerate']>>>;
doStream(options: LanguageModelV3CallOptions): Promise<Awaited<ReturnType<LanguageModelV3['doStream']>>>;
}
type OpenRouterEmbeddingModelId = string;
type OpenRouterEmbeddingSettings = {
/**
* A unique identifier representing your end-user, which can help OpenRouter to
* monitor and detect abuse.
*/
user?: string;
/**
* Provider routing preferences to control request routing behavior
*/
provider?: {
/**
* List of provider slugs to try in order (e.g. ["openai", "voyageai"])
*/
order?: string[];
/**
* Whether to allow backup providers when primary is unavailable (default: true)
*/
allow_fallbacks?: boolean;
/**
* Only use providers that support all parameters in your request (default: false)
*/
require_parameters?: boolean;
/**
* Control whether to use providers that may store data
*/
data_collection?: 'allow' | 'deny';
/**
* List of provider slugs to allow for this request
*/
only?: string[];
/**
* List of provider slugs to skip for this request
*/
ignore?: string[];
/**
* Sort providers by price, throughput, or latency
*/
sort?: 'price' | 'throughput' | 'latency';
/**
* Maximum pricing you want to pay for this request
*/
max_price?: {
prompt?: number | string;
completion?: number | string;
image?: number | string;
audio?: number | string;
request?: number | string;
};
};
} & OpenRouterSharedSettings;
type OpenRouterProviderOptions = {
models?: string[];
/**
* https://openrouter.ai/docs/use-cases/reasoning-tokens
* One of `max_tokens` or `effort` is required.
* If `exclude` is true, reasoning will be removed from the response. Default is false.
*/
reasoning?: {
enabled?: boolean;
exclude?: boolean;
} & ({
max_tokens: number;
} | {
effort: 'high' | 'medium' | 'low';
});
/**
* A unique identifier representing your end-user, which can
* help OpenRouter to monitor and detect abuse.
*/
user?: string;
};
type OpenRouterSharedSettings = OpenRouterProviderOptions & {
/**
* @deprecated use `reasoning` instead
*/
includeReasoning?: boolean;
extraBody?: Record<string, unknown>;
/**
* Enable usage accounting to get detailed token usage information.
* https://openrouter.ai/docs/use-cases/usage-accounting
*/
usage?: {
/**
* When true, includes token usage information in the response.
*/
include: boolean;
};
};
/**
* Usage accounting response
* @see https://openrouter.ai/docs/use-cases/usage-accounting
*/
type OpenRouterUsageAccounting = {
promptTokens: number;
promptTokensDetails?: {
cachedTokens: number;
};
completionTokens: number;
completionTokensDetails?: {
reasoningTokens: number;
};
totalTokens: number;
cost?: number;
costDetails?: {
upstreamInferenceCost: number;
};
};
type OpenRouterChatConfig = {
provider: string;
compatibility: 'strict' | 'compatible';
headers: () => Record<string, string | undefined>;
url: (options: {
modelId: string;
path: string;
}) => string;
fetch?: typeof fetch;
extraBody?: Record<string, unknown>;
};
declare class OpenRouterChatLanguageModel implements LanguageModelV3 {
readonly specificationVersion: "v3";
readonly provider = "openrouter";
readonly defaultObjectGenerationMode: "tool";
readonly modelId: OpenRouterChatModelId;
readonly supportsImageUrls = true;
readonly supportedUrls: Record<string, RegExp[]>;
readonly settings: OpenRouterChatSettings;
private readonly config;
constructor(modelId: OpenRouterChatModelId, settings: OpenRouterChatSettings, config: OpenRouterChatConfig);
private getArgs;
doGenerate(options: LanguageModelV3CallOptions): Promise<{
content: Array<LanguageModelV3Content>;
finishReason: LanguageModelV3FinishReason;
usage: LanguageModelV3Usage;
warnings: Array<SharedV3Warning>;
providerMetadata?: {
openrouter: {
provider: string;
reasoning_details?: ReasoningDetailUnion[];
usage: OpenRouterUsageAccounting;
};
};
request?: {
body?: unknown;
};
response?: LanguageModelV3ResponseMetadata & {
headers?: SharedV3Headers;
body?: unknown;
};
}>;
doStream(options: LanguageModelV3CallOptions): Promise<{
stream: ReadableStream<LanguageModelV3StreamPart>;
warnings: Array<SharedV3Warning>;
request?: {
body?: unknown;
};
response?: LanguageModelV3ResponseMetadata & {
headers?: SharedV3Headers;
body?: unknown;
};
}>;
}
export { OpenRouterChatLanguageModel, type OpenRouterChatModelId, type OpenRouterChatSettings, OpenRouterCompletionLanguageModel, type OpenRouterCompletionModelId, type OpenRouterCompletionSettings, type OpenRouterEmbeddingModelId, type OpenRouterEmbeddingSettings, type OpenRouterProviderOptions, type OpenRouterSharedSettings, type OpenRouterUsageAccounting };