Skip to main content

OpenRouter Provider

The OpenRouter provider gives you unified access to models from multiple providers including Google, Mistral, Meta, Microsoft, and others through a single API.

Configuration

API Key Setup

Set your OpenRouter API key in your .env file:
OPENROUTER_API_KEY=your_api_key_here
The API key configuration is defined in config/llm-magic.php:
'apis' => [
    'openrouter' => [
        'token' => env('OPENROUTER_API_KEY'),
    ],
]

Available Models

LLM Magic supports the following models through OpenRouter:

xAI Models

  • x-ai/grok-beta - xAI’s Grok model

Google Gemini Models

  • google/gemini-pro-1.5 - Gemini Pro 1.5
  • google/gemini-flash-1.5-8b - Gemini Flash 1.5 8B
  • google/gemini-flash-1.5 - Gemini Flash 1.5

Mistral Models

  • mistralai/mistral-7b-instruct - Mistral 7B Instruct
  • mistralai/mistral-large - Mistral Large
  • mistralai/mistral-nemo - Mistral Nemo
  • mistralai/mistral-tiny - Mistral Tiny

Cohere Models

  • cohere/command-r-08-2024 - Cohere Command R

Meta Llama Models

  • meta-llama/llama-3.1-8b-instruct - Llama 3.1 8B Instruct
  • meta-llama/llama-3.1-70b-instruct - Llama 3.1 70B Instruct
  • meta-llama/llama-3.1-405b-instruct - Llama 3.1 405B Instruct
  • meta-llama/llama-3.1-405b-instruct:free - Llama 3.1 405B Instruct (Free)
  • meta-llama/llama-3.2-1b-instruct - Llama 3.2 1B Instruct
  • meta-llama/llama-3.2-3b-instruct - Llama 3.2 3B Instruct
  • meta-llama/llama-3.2-11b-vision-instruct - Llama 3.2 11B Vision
  • meta-llama/llama-3.2-90b-vision-instruct - Llama 3.2 90B Vision

Microsoft Models

  • microsoft/wizardlm-2-8x22b - WizardLM 2 8x22B

Nous Research Models

  • nousresearch/hermes-3-llama-3.1-405b - Hermes 3 Llama 3.1 405B
  • nousresearch/hermes-3-llama-3.1-405b:free - Hermes 3 Llama 3.1 405B (Free)

Perplexity Models

  • perplexity/llama-3.1-sonar-large-128k-chat - Llama 3.1 Sonar 70B
  • perplexity/llama-3.1-sonar-large-128k-online - Llama 3.1 Sonar 70B (Online)

Model Constants

Use these constants for type safety:
use Mateffy\Magic\Models\OpenRouter;

OpenRouter::X_AI_GROK_BETA
OpenRouter::GOOGLE_GEMINI_1_5_PRO
OpenRouter::GOOGLE_GEMINI_1_5_FLASH_8B
OpenRouter::GOOGLE_GEMINI_1_5_FLASH
OpenRouter::MISTRAL_7B_INSTRUCT
OpenRouter::MISTRAL_LARGE
OpenRouter::MISTRAL_NEMO
OpenRouter::MISTRAL_TINY
OpenRouter::COHERE_COMMAND_R
OpenRouter::META_LLAMA_3_1_8B_INSTRUCT
OpenRouter::META_LLAMA_3_1_70B_INSTRUCT
OpenRouter::META_LLAMA_3_1_405B_INSTRUCT
OpenRouter::META_LLAMA_3_1_405B_INSTRUCT_FREE
OpenRouter::META_LLAMA_3_2_1B_INSTRUCT
OpenRouter::META_LLAMA_3_2_3B_INSTRUCT
OpenRouter::META_LLAMA_3_2_11B_VISION_INSTRUCT
OpenRouter::META_LLAMA_3_2_90B_VISION_INSTRUCT
OpenRouter::MICROSOFT_WIZARDLM_2_8X22B
OpenRouter::NOUS_HERMES_3_405B_INSTRUCT_FREE
OpenRouter::NOUS_HERMES_3_405B_INSTRUCT
OpenRouter::PERPLEXITY_LLAMA_3_1_SONAR_70B
OpenRouter::PERPLEXITY_LLAMA_3_1_SONAR_70B_ONLINE

Usage

Using the Constructor

Create an OpenRouter model instance:
use Mateffy\Magic\Models\OpenRouter;
use Mateffy\Magic\Models\Options\ElElEmOptions;

$model = new OpenRouter(
    model: OpenRouter::GOOGLE_GEMINI_1_5_FLASH,
    options: new ElElEmOptions
);

Using Static Factory Methods

LLM Magic provides convenient static methods:
use Mateffy\Magic\Models\OpenRouter;

// Use any model string
$model = OpenRouter::model('anthropic/claude-3-5-sonnet');

// Use xAI Grok
$model = OpenRouter::grok();

Getting Available Models

Retrieve a list of all available OpenRouter models:
use Mateffy\Magic\Models\OpenRouter;

$models = OpenRouter::models();
// Returns a Collection with prefixed model names like 'openrouter/google/gemini-flash-1.5'

$models = OpenRouter::models(prefix: null, prefixLabels: null);
// Returns models without prefix

API Configuration

OpenRouter uses the OpenAI-compatible API with a custom base URI:
protected function getOpenAiBaseUri(): ?string
{
    return 'openrouter.ai/api/v1';
}

Organization Info

The OpenRouter provider includes organization metadata:
  • ID: openrouter
  • Name: OpenRouter
  • Website: https://openrouter.ai
  • Privacy: Data may be used for model training and abuse prevention

Benefits of OpenRouter

Unified Access

Access models from multiple providers with a single API key:
  • No need to manage separate API keys for each provider
  • Consistent API interface across all models
  • Easy model switching and comparison

Cost Optimization

OpenRouter offers competitive pricing and free tier options:
  • Free models available (look for :free suffix)
  • Pay-as-you-go pricing
  • No monthly commitments

Model Discovery

Explore and experiment with models from different providers:
  • Test different models without setup overhead
  • Compare performance across providers
  • Find the best model for your use case

Advanced Usage

Using Any Model

OpenRouter supports many more models than those listed above. Use the model() static method to access any model:
use Mateffy\Magic\Models\OpenRouter;

// Use any model available on OpenRouter
$model = OpenRouter::model('anthropic/claude-3-5-sonnet');
$model = OpenRouter::model('openai/gpt-4-turbo');
$model = OpenRouter::model('meta-llama/llama-3.3-70b-instruct');

Model Options

Customize model behavior with options:
use Mateffy\Magic\Models\OpenRouter;
use Mateffy\Magic\Models\Options\ElElEmOptions;

$options = new ElElEmOptions(
    // Configure temperature, max tokens, etc.
);

$model = new OpenRouter(OpenRouter::GOOGLE_GEMINI_1_5_FLASH, $options);

See Also

Build docs developers (and LLMs) love