mozex / anthropic-laravel
Anthropic PHP for Laravel is a supercharged PHP API client that allows you to interact with the Anthropic API
Fund package maintenance!
Requires
- php: ^8.2.0
- guzzlehttp/guzzle: ^7.9.3
- laravel/framework: ^11.29|^12.12|^13.0
- mozex/anthropic-php: ^1.3.2
Requires (Dev)
- laravel/pint: ^1.22.0
- pestphp/pest: ^3.8.2|^4.0.0
- pestphp/pest-plugin-arch: ^3.1.1|^4.0.0
- phpstan/phpstan: ^2.1
- spatie/ray: ^1.41
- symfony/var-dumper: ^7.2.6
README
Anthropic Laravel is a community-maintained PHP API client that allows you to interact with the Anthropic API.
Note: This repository contains the integration code of the Anthropic PHP for Laravel. If you want to use the Anthropic PHP client in a framework-agnostic way, take a look at the mozex/anthropic-php repository.
Why Anthropic Laravel?
With the official Anthropic SDK and Laravel's own AI SDK available, you might wonder which to use. Here's how they compare:
| Anthropic Laravel | Laravel AI SDK | Official Anthropic SDK | |
|---|---|---|---|
| Anthropic API coverage | Full — messages, streaming, tool use, vision, batches, models, extended thinking, token counting | Unified API across providers — covers core features | Full |
| Multi-provider support | Anthropic only | OpenAI, Anthropic, Gemini, Groq, xAI | Anthropic only |
| Laravel integration | Facade, config publishing, service provider, testing fakes | Native — agents, queuing, conversation memory | None — framework-agnostic |
| Laravel version support | 11+ | 12+ | Any (no Laravel dependency) |
| PHP version | 8.2+ | 8.3+ | 8.1+ |
| New Anthropic features | Same-day support | Follows unified release cycle | Same-day support |
Choose Anthropic Laravel when you:
- Need full access to every Anthropic API feature — including batches, extended thinking, token counting, and model management
- Want a Laravel-native experience (Facades, config, testing) without sacrificing API depth
- Are on Laravel 11 (Laravel AI SDK requires 12+)
- Want same-day support when Anthropic ships new features
- Prefer a thin, focused wrapper over a multi-provider abstraction
Choose Laravel AI SDK when you:
- Need to switch between AI providers (OpenAI, Gemini, etc.) with one codebase
- Want built-in agent architecture with conversation memory and provider failover
- Are building a new Laravel 12+ project and don't need Anthropic-specific features
Both packages can coexist — use Laravel AI SDK for multi-provider features and Anthropic Laravel for deep Anthropic integration.
Table of Contents
- Why Anthropic Laravel?
- Support This Project
- Get Started
- Configuration
- Usage
- Testing
- Changelog
- Contributing
- Security Vulnerabilities
- Credits
- License
Support This Project
I maintain this package along with several other open-source PHP packages used by thousands of developers every day.
If my packages save you time or help your business, consider sponsoring my work on GitHub Sponsors. Your support lets me keep these packages updated, respond to issues quickly, and ship new features.
Business sponsors get logo placement in package READMEs. See sponsorship tiers →
Get Started
Requires PHP 8.2+
First, install Anthropic via the Composer package manager:
composer require mozex/anthropic-laravel
Next, execute the install command:
php artisan anthropic:install
This will create a config/anthropic.php configuration file in your project, which you can modify to your needs
using environment variables.
Blank environment variable for the Anthropic API key is already appended to your .env file.
ANTHROPIC_API_KEY=sk-...
Finally, you may use the Anthropic facade to access the Anthropic API:
use Anthropic\Laravel\Facades\Anthropic; $result = Anthropic::messages()->create([ 'model' => 'claude-sonnet-4-6', 'max_tokens' => 1024, 'messages' => [ ['role' => 'user', 'content' => 'Hello!'], ], ]); echo $result->content[0]->text; // Hello! How can I assist you today?
Configuration
Configuration is done via environment variables or directly in the configuration file (config/anthropic.php).
Anthropic API Key
Specify your Anthropic API Key. This will be used to authenticate with the Anthropic API - you can find your API key on your Anthropic dashboard, at https://console.anthropic.com/settings/keys.
ANTHROPIC_API_KEY=
Request Timeout
The timeout may be used to specify the maximum number of seconds to wait for a response. By default, the client will time out after 30 seconds.
ANTHROPIC_REQUEST_TIMEOUT=
Usage
For detailed usage examples, take a look at the mozex/anthropic-php repository.
The following resources are available through the Anthropic facade:
use Anthropic\Laravel\Facades\Anthropic; // Messages (primary API) Anthropic::messages()->create([...]); Anthropic::messages()->createStreamed([...]); Anthropic::messages()->countTokens([...]); // Models Anthropic::models()->list(); Anthropic::models()->retrieve('claude-sonnet-4-6'); // Message Batches Anthropic::batches()->create([...]); Anthropic::batches()->retrieve('msgbatch_...'); Anthropic::batches()->list(); Anthropic::batches()->cancel('msgbatch_...'); Anthropic::batches()->delete('msgbatch_...'); Anthropic::batches()->results('msgbatch_...'); // Legacy Completions Anthropic::completions()->create([...]);
Testing
The Anthropic facade comes with a fake() method that allows you to fake the API responses.
The fake responses are returned in the order they are provided to the fake() method.
All responses have a fake() method that allows you to easily create a response object by only providing the parameters relevant for your test case.
use Anthropic\Laravel\Facades\Anthropic; use Anthropic\Resources\Messages; use Anthropic\Responses\Messages\CreateResponse; Anthropic::fake([ CreateResponse::fake([ 'id' => 'msg_test', ]), ]); $result = Anthropic::messages()->create([ 'model' => 'claude-sonnet-4-6', 'max_tokens' => 1024, 'messages' => [ ['role' => 'user', 'content' => 'Hello!'], ], ]); expect($result)->id->toBe('msg_test');
After the requests have been sent there are various methods to ensure that the expected requests were sent:
// assert a messages create request was sent Anthropic::assertSent(Messages::class, function (string $method, array $parameters): bool { return $method === 'create' && $parameters['model'] === 'claude-sonnet-4-6'; });
You can also assert on specific resources:
Anthropic::messages()->assertSent(function (string $method, array $parameters): bool { return $method === 'create'; });
Other available assertion methods:
// assert that nothing was sent Anthropic::assertNothingSent(); // assert that a specific resource was not called Anthropic::assertNotSent(Messages::class);
For more testing examples, take a look at the mozex/anthropic-php repository.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Mozex
- Nuno Maduro and Sandro Gehri for their work on openai-php, which inspired this package
- All Contributors
License
The MIT License (MIT). Please see License File for more information.