Providers

OpenRouter

AISDK provides first-class support for OpenRouter with fully typed model APIs. Model capabilities are enforced at compile time using Rust's type system. This prevents model capability mismatches and guarantees the selected model is valid for the task (e.g. tool calling).

Installation

Enable the OpenRouter provider feature:

cargo add aisdk --features openrouter

This installs AISDK with the OpenRouter provider enabled. Once you have enabled the OpenRouter provider, you can use all aisdk features with it.

Create a Provider Instance

To create a provider instance, call OpenRouter::model_name(), where model_name is the OpenRouter model you want to use. Model names are exposed as snake-case methods.

use aisdk::providers::OpenRouter;

let openrouter = OpenRouter::openai_gpt_4o_mini();

This initializes the provider with:

  • Model: "openai/gpt-4o-mini"
  • API key from environment (if set with OPENROUTER_API_KEY)
  • OpenRouter's default base URL (https://openrouter.ai/api/)

Basic Text Generation

Example using LanguageModelRequest for text generation.

use aisdk::{
    core::LanguageModelRequest,
    providers::OpenRouter,
};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {

    let openrouter = OpenRouter::openai_gpt_4o_mini();

    let response = LanguageModelRequest::builder()
        .model(openrouter)
        .prompt("Write a short poem about Rust.")
        .build()
        .generate_text()
        .await?;

    println!("Response text: {:?}", response.text());
    Ok(())
}

Provider Settings

You can customize provider configuration using OpenRouter::builder()

API Key

let openrouter = OpenRouter::<OpenaiGpt4oMini>::builder()
    .api_key("your-api-key")
    .build()?;

If not specified, AISDK uses the OPENROUTER_API_KEY environment variable.

Base URL

Useful when routing through a proxy, gateway, or self-hosted compatible endpoint.

let openrouter = OpenRouter::<OpenaiGpt4oMini>::builder()
    .base_url("https://openrouter.ai/api/")
    .build()?;

Provider Name

For logging, analytics, and observability.

let openrouter = OpenRouter::<OpenaiGpt4oMini>::builder()
    .provider_name("OpenRouter")
    .build()?;

Full Custom Configuration Example

let openrouter = OpenRouter::<OpenaiGpt4oMini>::builder()
    .api_key("your-api-key")
    .base_url("https://openrouter.ai/api/")
    .provider_name("OpenRouter")
    .build()?;

Dynamic Model Selection

For runtime model selection (e.g., loading models from config files), use DynamicModel:

Using model_name() Method with Default Settings

use aisdk::providers::OpenRouter;

// Specify model as a string at runtime
let openrouter = OpenRouter::model_name("openai/gpt-4o-mini");

Using Builder Pattern with Custom Settings

use aisdk::{
    core::DynamicModel,
    providers::OpenRouter,
};

let openrouter = OpenRouter::<DynamicModel>::builder()
    .model_name("openai/gpt-4o-mini")
    .api_key("your-api-key")
		.base_url("https://openrouter.ai/api/")
    .build()?;

Warning: When using DynamicModel, model capabilities are not validated at compile time. This means there's no guarantee the model supports requested features (e.g., tool calls, structured output). For compile-time safety, use the typed methods like OpenRouter::openai_gpt_4o_mini().

Next Steps