mozex/anthropic-php
Community-maintained PHP SDK for the Anthropic API. Send messages, stream responses, call tools, use extended thinking, web search, code execution, files, and batches. PSR-18 compatible, works with any HTTP client; Laravel wrapper available.
HttpClient facade). This avoids vendor lock-in and aligns with Laravel’s modern architecture.mozex/anthropic-laravel package provides a Laravel-specific facade, service provider, and config-based setup, reducing boilerplate for Laravel applications.betas parameter. This ensures future-proofing as Anthropic introduces new capabilities.CreateResponse, FileResponse) improve type safety and IDE support, reducing runtime errors in Laravel’s dependency-injected ecosystem..env for API keys (e.g., ANTHROPIC_API_KEY) and config files for base URIs/timeouts.ClientFake enables unit testing without external dependencies, aligning with Laravel’s testing tools (e.g., Http facade mocking).betas parameter may introduce instability if Anthropic’s backend changes. Monitor the changelog for deprecations.$response->meta()) to avoid redundant API calls, but this requires custom logic.createStreamed) may conflict with Laravel’s request lifecycle (e.g., middleware, middleware groups). Use Laravel’s SynchronousQueue or custom event listeners to handle streams.API Key Management:
env() or a secrets manager (e.g., AWS Secrets Manager)?ANTHROPIC_API_KEY_STAGING)?Error Handling:
Anthropic\Exceptions\RateLimitExceeded) be mapped to Laravel’s Illuminate\Support\Facades\Response for consistent HTTP error responses?Illuminate\Support\Facades\Retry or a custom decorator?Performance:
$response->usage) be logged/audited? Integrate with Laravel’s logging or a dedicated observability tool (e.g., Datadog)?Security:
server_tool_use) that require additional validation? Use Laravel’s policy system or middleware.Anthropic\Client with custom middleware.Monitoring:
telemetry package or integrate with Prometheus via laravel-prometheus.queue:failed events or a dedicated monitoring tool.Laravel Core:
$this->app->singleton(AnthropicClient::class, function ($app) {
return Anthropic::client(config('services.anthropic.api_key'));
});
config/services.php:
'anthropic' => [
'api_key' => env('ANTHROPIC_API_KEY'),
'base_uri' => env('ANTHROPIC_BASE_URI', 'https://api.anthropic.com/v1'),
'timeout' => env('ANTHROPIC_TIMEOUT', 30),
],
Anthropic::client()) or create a custom facade for brevity.HTTP Client:
HttpClient (PSR-18 compliant) is auto-discovered by mozex/anthropic-php. No additional setup is needed unless using a custom client (e.g., Symfony’s HttpClient).$client = new \Anthropic\Client(
config('services.anthropic.api_key'),
new \GuzzleHttp\Client(['timeout' => config('services.anthropic.timeout')])
);
Queues:
dispatch(new UploadFileJob($filePath))->onQueue('anthropic');
Evaluation Phase:
composer require mozex/anthropic-php mozex/anthropic-laravel
messages()->create()) with the ClientFake for unit tests.Incremental Rollout:
config('app.debug') to toggle Anthropic features during development.Deprecation Strategy:
config('services.anthropic.enabled') to toggle between old/new implementations.conflict-resolution or platform-check.Schema::create('anthropic_files', function (Blueprint $table) {
$table->id();
$table->string('file_id')->unique();
$table->string('purpose')->comment('e.g., "document", "code_output"');
$table->timestamps();
});
Core Setup:
.env, secrets manager).Basic Functionality:
try-catch blocks for AnthropicException).Advanced Features:
HandleToolUseJob).Storage::disk('anthropic')->put()).Observability:
Monolog channel).telemetry.Security:
mozex/anthropic-php for breaking changes via GitHub releases orHow can I help you explore Laravel packages today?