A community-maintained PHP SDK for the Anthropic API. Send messages, stream responses, call tools, use extended thinking, search the web, execute code, process batches, and more. Works with any PSR-18 HTTP client.
Read the full documentation at mozex.dev: searchable docs, version requirements, detailed changelog, and more.
Using Laravel? Check out Anthropic Laravel, which wraps this SDK with service container integration, config-based setup, and a facade.
- Introduction
- Usage
- Reference
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 →
Built-in test client. Swap Anthropic\Client with ClientFake in your tests, queue fake responses, and assert exactly which requests were sent. No HTTP mocking libraries needed, no test server to run. See the testing docs →
use Anthropic\Testing\ClientFake;
use Anthropic\Responses\Messages\CreateResponse;
$client = new ClientFake([
CreateResponse::fake([
'content' => [['type' => 'text', 'text' => 'Paris is the capital of France.']],
]),
]);
$response = $client->messages()->create([...]);
$client->assertSent(Messages::class, function (string $method, array $parameters): bool {
return $parameters['model'] === 'claude-sonnet-4-6';
});Forward-compatible. Parameters pass through to the API as-is. When Anthropic ships a new feature (a new tool type, a new thinking mode, a new parameter), it works in your code the same day. You don't wait for an SDK release.
Typed, immutable responses. Every response is a readonly PHP object with typed properties. Access $response->usage->inputTokens, not $response['usage']['input_tokens']. Full IDE autocompletion, no guessing.
Rate limits on every response. Call $response->meta() on any response (including streams and batch results) to get your current request limits, token limits, and reset times.
Any HTTP client. Built on PSR-18, so it works with Guzzle, Symfony HTTP Client, Buzz, or whatever your project already uses. No vendor lock-in.
Requires PHP 8.2+ - see all version requirements
composer require mozex/anthropic-phpThe included php-http/discovery plugin finds and installs a compatible PSR-18 HTTP client automatically. If you want to use a specific one (like Guzzle or Symfony), see Configuration.
Create a client, send a message, read the response:
$client = Anthropic::client('your-api-key');
$response = $client->messages()->create([
'model' => 'claude-sonnet-4-6',
'max_tokens' => 1024,
'messages' => [
['role' => 'user', 'content' => 'Hello!'],
],
]);
echo $response->content[0]->text; // Hello! How can I assist you today?
Print text as it arrives:
$stream = $client->messages()->createStreamed([
'model' => 'claude-sonnet-4-6',
'max_tokens' => 1024,
'messages' => [
['role' => 'user', 'content' => 'Tell me a short story.'],
],
]);
foreach ($stream as $response) {
if ($response->type === 'content_block_delta'
&& $response->delta->type === 'text_delta') {
echo $response->delta->text;
}
}Give Claude tools to call, execute them in your code, send results back:
$response = $client->messages()->create([
'model' => 'claude-sonnet-4-6',
'max_tokens' => 1024,
'tools' => [
[
'name' => 'get_weather',
'description' => 'Get the current weather in a given location',
'input_schema' => [
'type' => 'object',
'properties' => [
'location' => ['type' => 'string'],
],
'required' => ['location'],
],
],
],
'messages' => [
['role' => 'user', 'content' => 'What is the weather in San Francisco?'],
],
]);
$response->content[1]->name; // 'get_weather'
$response->content[1]->input['location']; // 'San Francisco'Let Claude reason through complex problems before answering:
$response = $client->messages()->create([
'model' => 'claude-opus-4-6',
'max_tokens' => 16000,
'thinking' => ['type' => 'adaptive'],
'messages' => [
['role' => 'user', 'content' => 'What is the GCD of 1071 and 462?'],
],
]);
// Thinking block with Claude's reasoning process
$response->content[0]->thinking; // 'Using the Euclidean algorithm...'
// Final answer
$response->content[1]->text; // 'The GCD of 1071 and 462 is 21.'For custom base URIs, timeouts, or HTTP clients, use the factory:
$client = Anthropic::factory()
->withApiKey('your-api-key')
->withBaseUri('anthropic.example.com/v1')
->withHttpClient(new \GuzzleHttp\Client(['timeout' => 120]))
->withHttpHeader('X-Custom-Header', 'value')
->make();The full documentation covers every feature in detail: web search and code execution, document citations, token counting, batch processing, error handling, rate limits, and more.
Visit the documentation site for searchable docs auto-updated from this repository.
- AI Integration: Use this package with AI coding assistants via Context7 and Laravel Boost
- Requirements: PHP and dependency versions
- Changelog: Release history with linked pull requests and diffs
- Contributing: Development setup, code quality, and PR guidelines
- Questions & Issues: Bug reports, feature requests, and help
- Security: Report vulnerabilities directly via email
The MIT License (MIT). Please see License File for more information.