Anthropic Laravel
Laravel integration for the Anthropic API: facade, config publishing, install command, testing fakes, messages, streaming, tool use, thinking, and batches.
Last verified:
What is Anthropic Laravel?
Anthropic Laravel is a community-maintained PHP API client that allows Laravel developers to interact with the Anthropic API. It is a Laravel wrapper around the mozex/anthropic-php package, providing a first-class Laravel integration with a facade, publishable configuration file, and Laravel-specific testing helpers. The package enables seamless access to Claude models including Claude-3 Opus, Claude-3 Sonnet, and Claude-3 Haiku directly within Laravel applications.
Key features include full support for the Messages API, streaming responses via server-sent events, Claude's extended thinking capability for complex reasoning, tool use definition and handling (including while streaming), message batches for high-volume workloads with 50% cost savings, token counting to know request consumption upfront, and model listing/retrieval. The package also handles rate limit information, provides dedicated RateLimitException for HTTP 429 responses, and supports PSR-18/PSR-7 compliance for custom HTTP clients.
The package is designed for Laravel developers who need to integrate Claude AI into their applications. It requires PHP 8.1+ and supports Laravel 9, 10, 11, and 12. It is ideal for building AI chat applications, automated content generation, AI-powered assistants, and any Laravel project requiring Anthropic's Claude models. With over 230,000 Laravel wrapper installs and 370,000+ base PHP package downloads, it is the most installed dedicated Anthropic SDK in the PHP ecosystem.
Anthropic Laravel pricing
Pricing model: Freemium
The package itself is completely free and open-source under the MIT license. There are no paid plans or subscription tiers for the library. Users only pay for Anthropic API usage costs directly to Anthropic through their console at https://console.anthropic.com/settings/keys. The package enables access to all Claude models (Claude-3 Opus, Claude-3 Sonnet, Claude-3 Haiku, and newer models like Claude-sonnet-4) but API usage is billed by Anthropic based on tokens consumed. GitHub Sponsors program is available for those who want to support the maintainer Mozex, with any amount appreciated including $1 contributions.
Anthropic Laravel pros
- Over 230,000 Laravel wrapper installs with 600,000+ combined installs
- Built-in test fake eliminates need for HTTP mocking
- Full streaming support with proper server-sent event parsing
- Extended thinking capability for complex problem reasoning
- Tool use support including while streaming
- Message batches up to 100,000 messages with 50% cost savings
- Token counting before sending requests
- Clean interfaces with proper value objects for responses
- Factory pattern allows configuration without magic
- Familiar API pattern matching openai-php/client
- Dedicated RateLimitException for HTTP 429 responses
- PSR-18/PSR-7 compliant for custom HTTP client support
- Configurable base URI for proxy support
- Publishable config file with environment variable support
- Stable 1.x version with no breaking changes since stable release
- Model listing and retrieval functionality
- Laravel facade for simple access without dependency injection
Anthropic Laravel cons
- Community-maintained rather than official Anthropic SDK
- Requires PHP 8.1 or higher
- Default 30-second request timeout may need adjustment
- Laravel-specific (vanilla PHP users need base anthropic-php package)
- Still has fewer daily downloads than official SDK since official release
- Only 57 GitHub stars compared to official SDK growth
- No native multi-provider support (only Anthropic models)
- Requires manual API key configuration in .env file
Frequently asked questions about Anthropic Laravel
How do I install Anthropic Laravel?
Install via Composer by running 'composer require mozex/anthropic-laravel', then execute 'php artisan anthropic:install' to publish the config file and add ANTHROPIC_API_KEY to your .env file.
What PHP and Laravel versions are required?
The package requires PHP 8.1 or higher and supports Laravel 9.46.0+, Laravel 10.34.2+, Laravel 11.29.0+, and Laravel 12.0+.
How do I use the Anthropic facade in Laravel?
Import the facade with 'use Anthropic\Laravel\Facades\Anthropic;' then call Anthropic::messages()->create() with your model, max_tokens, and messages array containing role and content.
Does the package support streaming responses?
Yes, streaming is fully supported via createStreamed() method. It handles Anthropic's server-sent events with proper partial JSON chunk parsing for real-time responses.
How do I test code using Anthropic Laravel without hitting the real API?
Use Anthropic::fake() with CreateResponse::fake() to swap the real client for a fake in Laravel's service container. Use Anthropic::assertSent() to verify requests were sent.
Can I use Claude's extended thinking capability?
Yes, enable thinking by adding 'thinking' => ['type' => 'enabled', 'budget_tokens' => 10000] to your request parameters for models that support it.
How do I implement tool use with Claude?
Define tools in the 'tools' array with name, description, and input_schema containing type, properties, and required fields. The package handles tool-use responses including while streaming.
What is message batching and how does it save costs?
Message batches let you send up to 100,000 messages at once using Anthropic::batches()->create(). Batches provide 50% cost savings compared to individual message requests.
How do I count tokens before sending a request?
Use Anthropic::messages()->countTokens() with your model and messages to get exact input token consumption before sending the actual request.
How does this compare to Anthropic's official PHP SDK?
Anthropic Laravel has 370,000+ downloads vs official SDK's 144,000+, is stable at 1.x while official SDK is pre-1.0 (v0.8.0), has 11 dependents vs 4, but official SDK has higher daily downloads now due to Anthropic's name recognition.