anthropic-ai / sdk
Anthropic PHP SDK
Requires
- php: ^8.1
- open-telemetry/api: ^1.0
- php-http/discovery: ^1
- psr/http-client: ^1
- psr/http-client-implementation: ^1
- psr/http-factory-implementation: ^1
- psr/http-message: ^1|^2
- standard-webhooks/standard-webhooks: ^1
Requires (Dev)
- aws/aws-sdk-php: ^3.368
- friendsofphp/php-cs-fixer: ^3
- google/auth: ^1.49
- guzzlehttp/guzzle: ^7
- mcp/sdk: ^0.7.1
- nyholm/psr7: ^1
- open-telemetry/sdk: ^1.0
- pestphp/pest: ^3
- php-http/mock-client: ^1
- phpstan/extension-installer: ^1
- phpstan/phpstan: 2.2.13
- phpstan/phpstan-phpunit: ^2
- phpunit/phpunit: ^11
- symfony/http-client: ^7
Suggests
- aws/aws-sdk-php: Allows using the AWS Bedrock, AWS gateway, and Bedrock Mantle SDKs
- google/auth: Allows using the Google Vertex and Google Cloud gateway SDKs
- mcp/sdk: Required to use the BetaMcp helpers for integrating MCP servers with the Anthropic SDK
Provides
None
Conflicts
None
Replaces
None
- dev-main
- v1.3.0
- v1.2.0
- v1.1.0
- v1.0.0
- v0.61.0
- v0.60.0
- v0.59.0
- v0.58.0
- v0.57.0
- v0.56.0
- v0.55.0
- v0.54.0
- v0.53.0
- v0.52.0
- v0.51.0
- v0.50.0
- v0.49.0
- v0.48.0
- v0.47.0
- v0.46.0
- v0.45.0
- v0.44.0
- v0.43.0
- v0.42.0
- v0.41.0
- v0.40.0
- v0.39.0
- v0.38.0
- v0.37.1
- v0.37.0
- v0.36.0
- v0.35.1
- v0.35.0
- v0.34.0
- v0.33.0
- v0.32.0
- v0.31.0
- v0.30.0
- v0.29.2
- v0.29.1
- v0.29.0
- v0.28.0
- v0.27.0
- v0.26.0
- v0.25.0
- v0.24.0
- v0.23.0
- v0.22.0
- v0.21.0
- v0.20.0
- v0.19.0
- v0.18.0
- v0.17.1
- v0.17.0
- v0.16.0
- v0.15.0
- v0.14.0
- v0.13.0
- v0.12.0
- v0.11.0
- v0.10.0
- v0.9.0
- v0.8.0
- v0.7.0
- v0.6.0
- v0.5.0
- v0.4.0
- v0.3.0
- v0.2.0
- v0.1.0
- dev-meorphis-anthropic-patch-1
- dev-release-please--branches--main--changes--next
- dev-tomer/files-skills-changelog-details
- dev-generated
- dev-cameron/middleware
- dev-devsec/pin-actions
- dev-packy/remove-claude-code-review
- dev-fix/packagist-url
This package is auto-updated.
Last update: 2026-10-23 23:17:20 UTC
README
The Claude SDK for PHP provides access to the Claude API from PHP applications.
Documentation
Full documentation is available at platform.claude.com/docs/en/api/sdks/php.
Installation
composer require "anthropic-ai/sdk:^1.3.0"
The SDK needs a PSR-18 HTTP client. If you don't have one installed, add Guzzle:
composer require guzzlehttp/guzzle
Getting started
<?php use Anthropic\Client; use Anthropic\Messages\Model; $client = new Client( apiKey: getenv('ANTHROPIC_API_KEY') ?: 'my-anthropic-api-key' ); $message = $client->messages->create( maxTokens: 1024, messages: [['role' => 'user', 'content' => 'Hello, Claude']], model: Model::CLAUDE_OPUS_5_5, ); var_dump($message->content);
OpenTelemetry
The SDK reports each API call as an OpenTelemetry span once your application has registered a tracer provider with the OpenTelemetry API (open-telemetry/api), which the SDK requires. Spans are on by default, so there is nothing to enable on the client.
The openTelemetry client option selects another tracer provider with tracerProvider, turns the trace context headers of the requests off with propagation, and configures the spans with traces:
enabled:falseturns the spans off. The other settings are then unused.contentMode:'content'adds the content of requests and responses to the spans.'metadata_only', the default, leaves it out.maxContentBytes: the most bytes of content that one span holds, in place of the SDK's own bound of 0.75 MB. Content over the bound loses its oldest parts, and the span getsanthropic.content.truncated.
'traces' => false is short for 'traces' => ['enabled' => false], and openTelemetry: false is short for that. A key that the option does not take, such as a misspelt one, or a value it does not accept throws an InvalidArgumentException, as an unknown key or a conversationId that is no string does in the openTelemetry request option.
Each request carries the trace context of its span in the headers that your registered propagator writes. 'propagation' => false leaves them out, for an application whose HTTP client instrumentation already propagates the trace context, so that a request does not carry it twice. A span that the sampler dropped is then still the current span while its requests are sent, so that the instrumentation continues the trace with that sampling decision. The spans are the same either way.
The environment variables ANTHROPIC_OPEN_TELEMETRY, ANTHROPIC_OPEN_TELEMETRY_PROPAGATION, ANTHROPIC_OPEN_TELEMETRY_TRACES_CONTENT_MODE and ANTHROPIC_OPEN_TELEMETRY_TRACES_MAX_CONTENT_BYTES set traces.enabled, propagation, traces.contentMode and traces.maxContentBytes for a client that is given no openTelemetry option. It reads them once, when it is built, so a later change to them does not reach it. A value they do not accept is ignored. A client that is given the option does not read them: what the option leaves unset has its default.
<?php use Anthropic\Client; // a tracer provider other than the registered one $client = new Client(openTelemetry: ['tracerProvider' => $tracerProvider]); // no spans $quiet = new Client(openTelemetry: false); // spans, and no trace context headers on the requests $unpropagated = new Client(openTelemetry: ['propagation' => false]); // spans with the content of requests and responses, up to 64 KiB for each span $verbose = new Client(openTelemetry: ['traces' => ['contentMode' => 'content', 'maxContentBytes' => 65536]]); // sets gen_ai.conversation.id on the call's span; without it, only a call to a Session thread has one: the thread's ID $client->messages->create( maxTokens: 1024, messages: [['role' => 'user', 'content' => 'Hello, Claude']], model: 'claude-sonnet-5', requestOptions: ['openTelemetry' => ['conversationId' => $conversationId]], );
The openTelemetry request option of a call replaces the one in the client's requestOptions whole, as every request option does.
Requirements
PHP 8.1.0+
Autoloading note
This SDK includes class aliases for backwards compatibility, but these are not available when using authoritative classmaps in Composer.
If you use composer --classmap-authoritative and you rely on any of the following class names:
UnwrapWebhookEvent
Then update your code to use the true class name.
Contributing
See CONTRIBUTING.md.
License
This project is licensed under the Apache License, Version 2.0. See the LICENSE file for details. Versions released before the change from the MIT License, and third-party contributions merged before it, remain under the MIT License. See LICENSE-MIT.