PHPackages                             mozex/anthropic-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. mozex/anthropic-php

ActiveLibrary[API Development](/categories/api)

mozex/anthropic-php
===================

PHP client for the Anthropic API: messages, streaming, tool use, thinking, web search, code execution, batches, and more.

1.7.0(2mo ago)49552.5k↓29.4%9[1 PRs](https://github.com/mozex/anthropic-php/pulls)10MITPHPPHP ^8.2.0CI passing

Since May 1Pushed 3w ago1 watchersCompare

[ Source](https://github.com/mozex/anthropic-php)[ Packagist](https://packagist.org/packages/mozex/anthropic-php)[ GitHub Sponsors](https://github.com/mozex)[ RSS](/packages/mozex-anthropic-php/feed)WikiDiscussions main Synced 3d ago

READMEChangelog (10)Dependencies (50)Versions (23)Used By (10)

Anthropic PHP
=============

[](#anthropic-php)

[![Latest Version on Packagist](https://camo.githubusercontent.com/7c20574b98c18a7274d70743af806a61ef2b97b704d3577a983e7d650a07c3af/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6f7a65782f616e7468726f7069632d7068702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mozex/anthropic-php)[![GitHub Checks Workflow Status](https://camo.githubusercontent.com/d3800d28a6baa5d11e52f42b4229fa3790126d0564b27f2f80cc1e0b1f6f6869/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d6f7a65782f616e7468726f7069632d7068702f636865636b732e796d6c3f6272616e63683d6d61696e266c6162656c3d636865636b73267374796c653d666c61742d737175617265)](https://github.com/mozex/anthropic-php/actions/workflows/checks.yml)[![Docs](https://camo.githubusercontent.com/6bae6cab4895bc9b3768db4e7f44e2640da7b03da9635f43f34a1ea306f23e0e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646f63732d6d6f7a65782e6465762d3130423938313f7374796c653d666c61742d737175617265)](https://mozex.dev/docs/anthropic-php/v1)[![License](https://camo.githubusercontent.com/5b2af10b66fc8e0cc1fdae23843040eb320c10bbd8bae1f90e5757f6fd3108b9/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6d6f7a65782f616e7468726f7069632d7068702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mozex/anthropic-php)[![Total Downloads](https://camo.githubusercontent.com/35741fe3b5b17c0d3dc92b0ce8a5448c57025ca190a66ca619d87374182a0b64/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d6f7a65782f616e7468726f7069632d7068702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mozex/anthropic-php)

A community-maintained PHP SDK for the [Anthropic API](https://platform.claude.com/docs/en/about-claude/models/overview). 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](https://mozex.dev/docs/anthropic-php/v1)**: searchable docs, version requirements, detailed changelog, and more.

> **Using Laravel?** Check out [Anthropic Laravel](https://github.com/mozex/anthropic-laravel), which wraps this SDK with service container integration, config-based setup, and a facade.

Table of Contents
-----------------

[](#table-of-contents)

- [Introduction](https://mozex.dev/docs/anthropic-php/v1)
- Usage
    - [Messages](https://mozex.dev/docs/anthropic-php/v1/usage/messages)
    - [Streaming](https://mozex.dev/docs/anthropic-php/v1/usage/streaming)
    - [Tool Use](https://mozex.dev/docs/anthropic-php/v1/usage/tool-use)
    - [Thinking](https://mozex.dev/docs/anthropic-php/v1/usage/thinking)
    - [Server Tools](https://mozex.dev/docs/anthropic-php/v1/usage/server-tools)
    - [Citations](https://mozex.dev/docs/anthropic-php/v1/usage/citations)
    - [Token Counting](https://mozex.dev/docs/anthropic-php/v1/usage/token-counting)
    - [Models](https://mozex.dev/docs/anthropic-php/v1/usage/models)
    - [Batches](https://mozex.dev/docs/anthropic-php/v1/usage/batches)
    - [Files](https://mozex.dev/docs/anthropic-php/v1/usage/files)
    - [Completions](https://mozex.dev/docs/anthropic-php/v1/usage/completions)
- Reference
    - [Configuration](https://mozex.dev/docs/anthropic-php/v1/reference/configuration)
    - [Error Handling](https://mozex.dev/docs/anthropic-php/v1/reference/error-handling)
    - [Meta Information](https://mozex.dev/docs/anthropic-php/v1/reference/meta-information)
    - [Testing](https://mozex.dev/docs/anthropic-php/v1/reference/testing)

Support This Project
--------------------

[](#support-this-project)

I maintain this package along with [several other open-source PHP packages](https://mozex.dev/docs) used by thousands of developers every day.

If my packages save you time or help your business, consider [**sponsoring my work on GitHub Sponsors**](https://github.com/sponsors/mozex). 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 →**](https://github.com/sponsors/mozex)

Why This Package
----------------

[](#why-this-package)

**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 →](https://mozex.dev/docs/anthropic-php/v1/reference/testing)

```
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.

Installation
------------

[](#installation)

> **Requires [PHP 8.2+](https://www.php.net/releases/)** - see [all version requirements](https://mozex.dev/docs/anthropic-php/v1/requirements)

```
composer require mozex/anthropic-php
```

The 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](https://mozex.dev/docs/anthropic-php/v1/reference/configuration).

Quick Start
-----------

[](#quick-start)

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?

```

### Streaming

[](#streaming)

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;
    }
}
```

### Tool Use

[](#tool-use)

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'
```

### Extended Thinking

[](#extended-thinking)

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.'
```

### Configuration

[](#configuration)

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](https://mozex.dev/docs/anthropic-php/v1) covers every feature in detail: [web search and code execution](https://mozex.dev/docs/anthropic-php/v1/usage/server-tools), [document citations](https://mozex.dev/docs/anthropic-php/v1/usage/citations), [token counting](https://mozex.dev/docs/anthropic-php/v1/usage/token-counting), [batch processing](https://mozex.dev/docs/anthropic-php/v1/usage/batches), [error handling](https://mozex.dev/docs/anthropic-php/v1/reference/error-handling), [rate limits](https://mozex.dev/docs/anthropic-php/v1/reference/meta-information), and [more](https://mozex.dev/docs/anthropic-php/v1).

Resources
---------

[](#resources)

Visit the [documentation site](https://mozex.dev/docs/anthropic-php/v1) for searchable docs auto-updated from this repository.

- **[AI Integration](https://mozex.dev/docs/anthropic-php/v1/ai-integration)**: Use this package with AI coding assistants via Context7 and Laravel Boost
- **[Requirements](https://mozex.dev/docs/anthropic-php/v1/requirements)**: PHP and dependency versions
- **[Changelog](https://mozex.dev/docs/anthropic-php/v1/changelog)**: Release history with linked pull requests and diffs
- **[Contributing](https://mozex.dev/docs/anthropic-php/v1/contributing)**: Development setup, code quality, and PR guidelines
- **[Questions &amp; Issues](https://mozex.dev/docs/anthropic-php/v1/questions-and-issues)**: Bug reports, feature requests, and help
- **[Security](mailto:hello@mozex.dev)**: Report vulnerabilities directly via email

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

60

—

FairBetter than 98% of packages

Maintenance91

Actively maintained with recent releases

Popularity51

Moderate usage in the ecosystem

Community23

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 98.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~45 days

Recently: every ~11 days

Total

17

Last Release

77d ago

PHP version history (2 changes)1.0.0PHP ^8.1.0

1.2.0PHP ^8.2.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/f4d33a9cb8263eab6fa251e57fc763c5c6977701e5cafa0edc538c4651bb9780?d=identicon)[mozex](/maintainers/mozex)

---

Top Contributors

[![mozex](https://avatars.githubusercontent.com/u/18025667?v=4)](https://github.com/mozex "mozex (210 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (4 commits)")

---

Tags

aianthropicapi-clientchatbotclaudegenerative-aillmphpsdkstreamingtool-usephpapiclientsdkaistreamingclaudellmanthropictext-generationgenerative-aitool-useClaude OpusClaude SonnetClaude Haikuthinking

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/mozex-anthropic-php/health.svg)

```
[![Health](https://phpackages.com/badges/mozex-anthropic-php/health.svg)](https://phpackages.com/packages/mozex-anthropic-php)
```

###  Alternatives

[telnyx/telnyx-php

Official Telnyx PHP SDK — APIs for Voice, SMS, MMS, WhatsApp, Fax, SIP Trunking, Wireless IoT, Call Control, and more. Build global communications on Telnyx's private carrier-grade network.

35789.4k2](/packages/telnyx-telnyx-php)[openai-php/client

OpenAI PHP is a supercharged PHP API client that allows you to interact with the Open AI API

5.8k28.0M318](/packages/openai-php-client)[mozex/anthropic-laravel

Laravel integration for the Anthropic API: facade, config publishing, install command, testing fakes, messages, streaming, tool use, thinking, and batches.

74331.3k1](/packages/mozex-anthropic-laravel)[getbrevo/brevo-php

Official Brevo provided RESTFul API V3 php library

1003.9M50](/packages/getbrevo-brevo-php)[deepseek-php/deepseek-php-client

deepseek PHP client is a robust and community-driven PHP client library for seamless integration with the Deepseek API, offering efficient access to advanced AI and data processing capabilities.

46688.8k5](/packages/deepseek-php-deepseek-php-client)[deeplcom/deepl-php

Official DeepL API Client Library

2607.3M114](/packages/deeplcom-deepl-php)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
