PHPackages                             llm-speak/open-router - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. llm-speak/open-router

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

llm-speak/open-router
=====================

A Laravel package for integrating OpenRouter into LLMSpeak

0.4.1(9mo ago)016MITPHPPHP ^8.2

Since Jul 30Pushed 9mo agoCompare

[ Source](https://github.com/projectsaturnstudios/llm-speak-open-router)[ Packagist](https://packagist.org/packages/llm-speak/open-router)[ RSS](/packages/llm-speak-open-router/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (3)Used By (0)

LLMSpeak OpenRouter
===================

[](#llmspeak-openrouter)

[![License: MIT](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)](https://opensource.org/licenses/MIT)[![PHP](https://camo.githubusercontent.com/0f16581d1180dbfd4c0e13166ec1267d4ad2f2fab8281ea6d6b284cf5c65d921/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e322532422d626c75652e737667)](https://php.net/releases/)[![Laravel](https://camo.githubusercontent.com/906dea2eb7060f4769da2f105c19b13c8e426284048c7217e23102fb572d64a8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d31302e7825374331312e7825374331322e782d7265642e737667)](https://laravel.com)[![Latest Version on Packagist](https://camo.githubusercontent.com/092f765242323e4b68a2b7a5adc1e40efac62bef7fd1a4b086df1e573699abc8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c6c6d2d737065616b2f6f70656e2d726f757465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/llm-speak/open-router)[![Total Downloads](https://camo.githubusercontent.com/cba7e073df4feac29317899579380d7a9f4366e5970e0ea97ffa458c3b16d944/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c6c6d2d737065616b2f6f70656e2d726f757465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/llm-speak/open-router)

**LLMSpeak OpenRouter** is a Laravel package that provides a fluent, Laravel-native interface for integrating with OpenRouter's unified AI model gateway. Built as part of the LLMSpeak ecosystem, it offers seamless access to 100+ AI models from providers like Anthropic, OpenAI, Google, Meta, and more through a single, consistent API.

> **Note:** This package is part of the larger [LLMSpeak ecosystem](https://github.com/projectsaturnstudios/llm-speak). For universal provider switching and standardized interfaces, check out the [LLMSpeak Core](https://github.com/projectsaturnstudios/llm-speak-core) package.

Table of Contents
-----------------

[](#table-of-contents)

- [Features](#features)
- [Get Started](#get-started)
- [Usage](#usage)
    - [Basic Request](#basic-request)
    - [Model Selection](#model-selection)
    - [Fluent Request Building](#fluent-request-building)
    - [Tool Calling](#tool-calling)
    - [Reasoning Mode](#reasoning-mode)
    - [Advanced Sampling](#advanced-sampling)
    - [Log Probabilities](#log-probabilities)
    - [Streaming Responses](#streaming-responses)
    - [Advanced Configuration](#advanced-configuration)
- [Response Handling](#response-handling)
- [Testing](#testing)
- [Credits](#credits)
- [License](#license)

Features
--------

[](#features)

- **🌐 Multi-Provider Access**: Access 100+ models from Anthropic, OpenAI, Google, Meta, Mistral, and more
- **🚀 Laravel Native**: Full Laravel integration with automatic service discovery
- **🔧 Fluent Interface**: Expressive request builders with method chaining
- **📊 Laravel Data**: Powered by Spatie Laravel Data for robust data validation
- **🛠️ Tool Support**: Complete function calling capabilities with parallel execution
- **🧠 Reasoning Mode**: OpenRouter's unique "thinking" tokens for enhanced reasoning
- **📈 Log Probabilities**: Advanced probability analysis and token confidence scoring
- **🎛️ Advanced Sampling**: Fine-grained control over model behavior with multiple sampling methods
- **💨 Streaming**: Real-time streaming responses
- **🎯 Type Safety**: Full PHP 8.2+ type declarations and IDE support
- **🔐 Secure**: Built-in API key management and request validation

Get Started
-----------

[](#get-started)

> **Requires [PHP 8.2+](https://php.net/releases/) and Laravel 10.x/11.x/12.x**

Install the package via [Composer](https://getcomposer.org/):

```
composer require llm-speak/open-router
```

The package will automatically register itself via Laravel's package discovery.

### Environment Configuration

[](#environment-configuration)

Add your OpenRouter API key to your `.env` file:

```
OPENROUTER_API_KEY=your_openrouter_api_key_here
```

Get your API key from [OpenRouter.ai](https://openrouter.ai/keys).

Usage
-----

[](#usage)

### Basic Request

[](#basic-request)

The simplest way to chat with any AI model through OpenRouter:

```
use LLMSpeak\OpenRouter\OpenRouterCompletionsRequest;

$request = new OpenRouterCompletionsRequest(
    model: 'anthropic/claude-3.5-sonnet',
    messages: [
        ['role' => 'user', 'content' => 'Hello! What can you help me with?']
    ]
);

$response = $request->post();

echo $response->getTextContent(); // "Hello! I'm here to help..."
```

### Model Selection

[](#model-selection)

OpenRouter provides access to 100+ models. Choose the right model for your use case:

```
// High-quality reasoning models
$request = new OpenRouterCompletionsRequest(
    model: 'anthropic/claude-3.5-sonnet',
    messages: $messages
);

// Fast, cost-effective models
$request = new OpenRouterCompletionsRequest(
    model: 'anthropic/claude-3.5-haiku',
    messages: $messages
);

// Cutting-edge experimental models
$request = new OpenRouterCompletionsRequest(
    model: 'openai/gpt-4o',
    messages: $messages
);

// Open-source models
$request = new OpenRouterCompletionsRequest(
    model: 'meta-llama/llama-3.1-70b-instruct',
    messages: $messages
);

// Specialized models
$request = new OpenRouterCompletionsRequest(
    model: 'google/gemini-pro-1.5',
    messages: $messages
);
```

### Fluent Request Building

[](#fluent-request-building)

Build complex requests using the fluent interface:

```
use LLMSpeak\OpenRouter\OpenRouterCompletionsRequest;

$request = new OpenRouterCompletionsRequest(
    model: 'anthropic/claude-3.5-sonnet',
    messages: [
        ['role' => 'user', 'content' => 'Write a creative story about time travel']
    ]
)
->setMaxTokens(2000)
->setTemperature(0.8)
->setTopP(0.9)
->setTopK(50)
->setFrequencyPenalty(0.1)
->setPresencePenalty(0.1);

$response = $request->post();

// Access response properties
echo $response->id;                    // chat-completion-abc123
echo $response->model;                 // anthropic/claude-3.5-sonnet
echo $response->getTotalTokens();      // 1850
echo $response->getTextContent();      // Generated story content
```

### Batch Configuration

[](#batch-configuration)

Set multiple parameters at once:

```
$request = new OpenRouterCompletionsRequest(
    model: 'openai/gpt-4o',
    messages: $conversation
)->setMultiple([
    'maxTokens' => 1500,
    'temperature' => 0.7,
    'topP' => 0.95,
    'frequencyPenalty' => 0.2,
    'presencePenalty' => 0.1,
    'stop' => ['Human:', 'Assistant:'],
    'seed' => 12345,
    'user' => 'user_123'
]);
```

### Tool Calling

[](#tool-calling)

Enable models to use external functions and tools:

```
$tools = [
    [
        'type' => 'function',
        'function' => [
            'name' => 'get_stock_price',
            'description' => 'Get the current stock price for a given symbol',
            'parameters' => [
                'type' => 'object',
                'properties' => [
                    'symbol' => [
                        'type' => 'string',
                        'description' => 'Stock symbol (e.g., AAPL, GOOGL)'
                    ],
                    'currency' => [
                        'type' => 'string',
                        'enum' => ['USD', 'EUR'],
                        'description' => 'Currency for the price'
                    ]
                ],
                'required' => ['symbol']
            ]
        ]
    ]
];

$request = new OpenRouterCompletionsRequest(
    model: 'openai/gpt-4o',
    messages: [
        ['role' => 'user', 'content' => 'What\'s the current price of Apple stock?']
    ]
)
->setTools($tools)
->setToolChoice('auto')
->setParallelFunctionCalling(true);

$response = $request->post();

// Check for tool usage
if ($response->usedTools()) {
    $toolCalls = $response->getToolCalls();
    foreach ($toolCalls as $call) {
        echo "Function: {$call['function']['name']}\n";
        echo "Arguments: " . json_encode($call['function']['arguments']) . "\n";
    }
}
```

### Reasoning Mode

[](#reasoning-mode)

Enable OpenRouter's unique reasoning capabilities for enhanced problem-solving:

```
$request = new OpenRouterCompletionsRequest(
    model: 'anthropic/claude-3.5-sonnet',
    messages: [
        [
            'role' => 'user',
            'content' => 'Solve this step-by-step: A train leaves Station A at 2 PM traveling at 60 mph. Another train leaves Station B at 3 PM traveling at 80 mph toward Station A. If the stations are 300 miles apart, when do the trains meet?'
        ]
    ]
)
->setReasoning(['effort' => 'high'])
->setMaxTokens(3000);

$response = $request->post();

// Access reasoning content
$reasoning = $response->getReasoningContent();
$finalAnswer = $response->getTextContent();

echo "Reasoning process:\n" . $reasoning . "\n\n";
echo "Final answer:\n" . $finalAnswer;

// Check reasoning efficiency
$reasoningTokens = $response->getReasoningTokens();
$efficiency = $response->getReasoningEfficiency();
echo "Used {$reasoningTokens} reasoning tokens ({$efficiency}% of output)";
```

### Advanced Sampling

[](#advanced-sampling)

Fine-tune model behavior with advanced sampling parameters:

```
$request = new OpenRouterCompletionsRequest(
    model: 'meta-llama/llama-3.1-70b-instruct',
    messages: $messages
)
->setTemperature(0.8)           // Creativity level (0.0-2.0)
->setTopP(0.9)                  // Nucleus sampling (0.0-1.0)
->setTopK(40)                   // Top-K sampling (0+)
->setMinP(0.05)                 // Minimum probability threshold
->setTopA(0.2)                  // Top-A sampling
->setRepetitionPenalty(1.1)     // Prevent repetition
->setFrequencyPenalty(0.1)      // Frequency-based penalty
->setPresencePenalty(0.1)       // Presence-based penalty
->setSeed(42);                  // Deterministic output

$response = $request->post();
```

### Log Probabilities

[](#log-probabilities)

Analyze token probabilities and model confidence:

```
$request = new OpenRouterCompletionsRequest(
    model: 'openai/gpt-4o',
    messages: [
        ['role' => 'user', 'content' => 'Is this statement true or false: The Earth is flat?']
    ]
)
->setLogprobs(true)
->setTopLogprobs(5)  // Get top 5 token probabilities
->setMaxTokens(100);

$response = $request->post();

// Analyze confidence
$logProbs = $response->getLogProbs();
$avgConfidence = $response->getAverageLogProb();
$tokenConfidence = $response->getTokenConfidence();

echo "Average confidence: " . ($avgConfidence * 100) . "%\n";
echo "High confidence tokens: " . count($tokenConfidence['high']) . "\n";
echo "Low confidence tokens: " . count($tokenConfidence['low']) . "\n";
```

### Streaming Responses

[](#streaming-responses)

Enable real-time streaming for long responses:

```
$request = new OpenRouterCompletionsRequest(
    model: 'anthropic/claude-3.5-sonnet',
    messages: [
        ['role' => 'user', 'content' => 'Write a detailed essay about renewable energy']
    ]
)
->setStream(true)
->setMaxTokens(4000);

$response = $request->post();

// Stream handling will be processed by the CompletionsEndpoint
// Response contains streaming data format
```

### Advanced Configuration

[](#advanced-configuration)

Configure advanced parameters for optimal performance:

```
$request = new OpenRouterCompletionsRequest(
    model: 'google/gemini-pro-1.5',
    messages: $conversationHistory
)
->setMaxTokens(8000)
->setTemperature(0.7)
->setTopP(0.95)
->setFrequencyPenalty(0.3)
->setPresencePenalty(0.2)
->setRepetitionPenalty(1.05)
->setStop(['[END]', '###', 'Human:'])
->setSeed(12345)
->setUser('analytics_user_456')
->setLogitBias([50256 => -100])  // Suppress specific tokens
->setResponseFormat(['type' => 'json_object']);

$response = $request->post();
```

Response Handling
-----------------

[](#response-handling)

Access comprehensive response data:

```
$response = $request->post();

// Basic response info
$responseId = $response->id;
$modelUsed = $response->model;
$timestamp = $response->created;

// Content access
$textContent = $response->getTextContent();
$allChoices = $response->choices;
$firstChoice = $response->getFirstChoice();

// Token usage analysis
$totalTokens = $response->getTotalTokens();
$inputTokens = $response->getInputTokens();
$outputTokens = $response->getOutputTokens();
$reasoningTokens = $response->getReasoningTokens();

// Completion analysis
$finishReason = $response->getFinishReason();
$completedNaturally = $response->completedNaturally();
$hitTokenLimit = $response->reachedTokenLimit();
$wasStopped = $response->stoppedBySequence();

// Tool usage
$usedTools = $response->usedTools();
$toolCalls = $response->getToolCalls();

// Reasoning analysis (if enabled)
$hasReasoning = $response->hasReasoning();
$reasoningContent = $response->getReasoningContent();
$reasoningEfficiency = $response->getReasoningEfficiency();

// Log probability analysis (if enabled)
$logProbs = $response->getLogProbs();
$avgLogProb = $response->getAverageLogProb();
$tokenConfidence = $response->getTokenConfidence();

// Quality metrics
$responseQuality = $response->calculateQualityScore();
$confidenceLevel = $response->getConfidenceLevel();

// Convert to array for storage
$responseArray = $response->toArray();
```

Testing
-------

[](#testing)

The package provides testing utilities for mocking OpenRouter responses:

```
use LLMSpeak\OpenRouter\OpenRouterCompletionsRequest;
use LLMSpeak\OpenRouter\OpenRouterCompletionsResponse;

// Create a mock response
$mockResponse = new OpenRouterCompletionsResponse(
    id: 'chatcmpl-test123',
    model: 'anthropic/claude-3.5-sonnet',
    created: time(),
    choices: [
        [
            'index' => 0,
            'message' => [
                'role' => 'assistant',
                'content' => 'Test response content'
            ],
            'finish_reason' => 'stop'
        ]
    ],
    usage: [
        'prompt_tokens' => 10,
        'completion_tokens' => 15,
        'total_tokens' => 25
    ]
);

// Test your application logic
$this->assertEquals('Test response content', $mockResponse->getTextContent());
$this->assertEquals(25, $mockResponse->getTotalTokens());
$this->assertTrue($mockResponse->completedNaturally());
```

Credits
-------

[](#credits)

- [Project Saturn Studios](https://github.com/projectsaturnstudios)
- [OpenRouter.ai](https://openrouter.ai) for providing the unified AI model gateway

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

---

**Part of the LLMSpeak Ecosystem** - Built with ❤️ by [Project Saturn Studios](https://projectsaturnstudios.com)

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance56

Moderate activity, may be stable

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

291d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2169021b88d520bd58d9a37f51fa55058af7adbf7362c8cade65b261d644874c?d=identicon)[projectsaturnstudios](/maintainers/projectsaturnstudios)

---

Top Contributors

[![projectsaturnstudios](https://avatars.githubusercontent.com/u/10563160?v=4)](https://github.com/projectsaturnstudios "projectsaturnstudios (2 commits)")

### Embed Badge

![Health badge](/badges/llm-speak-open-router/health.svg)

```
[![Health](https://phpackages.com/badges/llm-speak-open-router/health.svg)](https://phpackages.com/packages/llm-speak-open-router)
```

###  Alternatives

[fotografde/cakephp-ftp

FTP/SFTP Plugin for CakePHP

6062.3k](/packages/fotografde-cakephp-ftp)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
