Search by

illuma-law / laravel-llm-router

miguelenes

A resilient AI routing and circuit-breaker system for LLM requests in Laravel.

Package info

github.com/eloquage/laravel-llm-router

pkg:composer/illuma-law/laravel-llm-router

Statistics

Installs: 64

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 3

v0.1.5 2026-10-23 14:02 UTC

README

Laravel LLM Router fallback paths

Route Laravel AI requests through ordered provider/model chains with retries, fallbacks, and tenant-aware overrides.

Tests Latest Version License

Requires PHP 8.3+ and Laravel 11, 12, or 13. Install Laravel AI and configure its provider credentials to use the built-in prompt() method. You can use the router without Laravel AI by providing your own request closure.

Install

composer require illuma-law/laravel-llm-router
php artisan vendor:publish --tag=llm-router-config

For the built-in prompt integration:

composer require laravel/ai

Edit config/llm-router.php to set the provider/model order for each tier.

Route a request

Use a closure to run each provider/model attempt. The router returns the result and the provider/model that succeeded:

use IllumaLaw\LlmRouter\Enums\DefaultTier;
use IllumaLaw\LlmRouter\Facades\LLMRouter;
use Laravel\Ai\Facades\Ai;

$response = LLMRouter::tier(DefaultTier::Large)
    ->run(fn (string $provider, string $model) => Ai::provider($provider)
        ->model($model)
        ->prompt('Summarize this report.'));

$answer = $response['result']->text;
$usedProvider = $response['provider'];

To pass a Laravel AI agent, provide its class name to forAgent() and the instance to agent():

$response = LLMRouter::forAgent($agent::class)
    ->agent($agent)
    ->tier(DefaultTier::Large)
    ->run(fn (string $provider, string $model) => $agent->prompt(
        'Review this report.',
        provider: $provider,
        model: $model,
    ));

Tenant and operation routing

Use forTenant() to apply the configured priority model when your resolver marks that tenant as requiring it. Use operation() for a chain defined under operations in the config:

LLMRouter::resolvePriorityUsing(
    fn ($tenant): bool => $tenant->requires_local_models,
);

$response = LLMRouter::operation('image_generation')
    ->forTenant($tenant)
    ->run(fn (string $provider, string $model) => "Generated by {$provider}/{$model}");

Other reusable services include ProviderAvailability, ChainRowValidator, and CooldownStore.

Test

composer test
composer analyse
vendor/bin/pint --test

MIT licensed. See LICENSE.