mozex/anthropic-php
PHP client for Anthropic’s Claude API. Provides a simple, typed interface for sending messages and handling responses, with support for streaming, models, and common request options—easy to integrate into Laravel or any PHP app.
Upload a document once and reference it by file_id on later Messages calls. Useful for repeated PDFs and images, and for reading outputs produced by the code execution tool and Skills.
Five methods on $client->files():
upload(array $parameters): multipart upload, returns a FileResponselist(array $parameters = []): cursor-paginated listingretrieveMetadata(string $fileId): fetch metadata for a single filedownload(string $fileId): raw bytes for files produced by code execution or Skills (user-uploaded files are not downloadable)delete(string $fileId): returns a DeletedFileResponse$file = $client->files()->upload([
'file' => fopen('/path/to/doc.pdf', 'r'),
]);
$response = $client->messages()->create([
'model' => 'claude-opus-4-6',
'max_tokens' => 1024,
'betas' => ['files-api-2025-04-14'],
'messages' => [[
'role' => 'user',
'content' => [
['type' => 'text', 'text' => 'Summarise this.'],
['type' => 'document', 'source' => ['type' => 'file', 'file_id' => $file->id]],
],
]],
]);
Anthropic currently flags the Files endpoints as beta. The SDK auto-injects the required anthropic-beta: files-api-2025-04-14 header on every $client->files() call, so you don't type the version string. When you reference a file_id inside a Messages call, pass 'betas' => ['files-api-2025-04-14'] on that call too; the Messages endpoint also needs the header when a file is referenced.
New Files usage guide covers upload, list, retrieve, download, delete, and Messages integration, including the per-call betas pattern for referencing uploaded files.
Every Files response DTO has a fake() method for use with ClientFake:
FileResponse::fake()FileListResponse::fake()DeletedFileResponse::fake()Full Changelog: https://github.com/mozex/anthropic-php/compare/1.6.0...1.7.0
betas parameter for per-request beta feature opt-in by @mozex in https://github.com/mozex/anthropic-php/pull/16Full Changelog: https://github.com/mozex/anthropic-php/compare/1.5.0...1.6.0
Models API
maxInputTokens and maxTokens properties to RetrieveResponse (and every item returned by ListResponse)capabilities tree on model responses: batch, citations, codeExecution, imageInput, pdfInput, structuredOutputs, thinking (with types.adaptive and types.enabled), and effort (with low, medium, high, max levels)capabilities.contextManagement as a map keyed by the raw API strategy name (clear_thinking_20251015, clear_tool_uses_20250919, compact_20260112, and any future version), so new strategies Anthropic ships are captured automatically without a package updateMessages API
stop_details on CreateResponse with type, category, and explanation — populated when stop_reason is 'refusal'caller on tool_use and server_tool_use content blocks (type and tool_id) so direct model calls can be distinguished from calls made inside a code execution sandboxcontainer_upload content block support with file_idinferenceGeo to CreateResponseUsage and CreateStreamedResponseUsage, surfacing which region handled the request ('us', 'eu', or null)pause_turn and refusal stop reasons and the idiom for resuming paused turnsMeta information
priorityInputTokenLimit and priorityOutputTokenLimit on MetaInformation, parsing the six anthropic-priority-* headers into typed properties instead of dropping them into the generic custom bucketDocumentation
platform.claude.com/docs/tool_use / server_tool_use toArray() output now includes the caller object when present, so response round-trips stay losslessMetaInformation rate-limit headers (including the new priority tier) round-trip cleanly through toArray()Full Changelog: https://github.com/mozex/anthropic-php/compare/1.4.0...1.5.0
thinking.type: "adaptive") for Claude Opus 4.6 and Sonnet 4.6display option on thinking config (summarized / omitted) to control thinking output in responsesoutput_config.effort parameter for guiding thinking depth (max, high, medium, low)server_tool_use content block type for server-side tools (web search, web fetch, code execution, tool search)web_search_tool_result content block type with search results arrayweb_fetch_tool_result content block typecode_execution_tool_result, bash_code_execution_tool_result, and text_editor_code_execution_tool_result content block typestool_search_tool_result content block typetool_use_id and content properties on CreateResponseContent for all server tool result blockscontainer field on CreateResponse for code execution sandbox persistencecitations array on text content blocks, supporting all 5 citation location types: char_location, page_location, content_block_location, web_search_result_location, search_result_locationcitations_delta streaming support with citation property on CreateStreamedResponseDeltawebFetchRequests, codeExecutionRequests, and toolSearchRequests to CreateResponseUsageServerToolUseserver_tool_use and server tool result blocks in CreateStreamedResponseContentBlockStartFull Changelog: https://github.com/mozex/anthropic-php/compare/1.3.3...1.4.0
Full Changelog: https://github.com/mozex/anthropic-php/compare/1.3.2...1.3.3
Full Changelog: https://github.com/mozex/anthropic-php/compare/1.3.1...1.3.2
inputTokenLimit and outputTokenLimit rate limit properties to MetaInformationcache_creation breakdown to usage (CreateResponseUsageCacheCreation with ephemeral5mInputTokens and ephemeral1hInputTokens)serviceTier field to usage (standard, priority, or batch)serverToolUse field to usage (CreateResponseUsageServerToolUse with webSearchRequests)custom bucketFull Changelog: https://github.com/mozex/anthropic-php/compare/1.3.0...1.3.1
BatchResponse, BatchListResponse, DeletedBatchResponse, and BatchResultResponse response DTOsBatchIndividualResponse, BatchResult, and BatchResultError for parsing JSONL batch resultsBatchResponseRequestCounts for tracking processing, succeeded, errored, canceled, and expired countsBatchResultResponse::fake() for consumer testing with ClientFakeinputTokenLimit and outputTokenLimit rate limit properties to MetaInformationcache_creation breakdown to usage (CreateResponseUsageCacheCreation with ephemeral5mInputTokens and ephemeral1hInputTokens)serviceTier field to usage (standard, priority, or batch)serverToolUse field to usage (CreateResponseUsageServerToolUse with webSearchRequests)custom bucketFull Changelog: https://github.com/mozex/anthropic-php/compare/1.2.3...1.3.0
Full Changelog: https://github.com/mozex/anthropic-php/compare/1.2.2...1.2.3
Full Changelog: https://github.com/mozex/anthropic-php/compare/1.2.1...1.2.2
cache_creation_input_tokens and cache_read_input_tokens to CreateStreamedResponseUsageFull Changelog: https://github.com/mozex/anthropic-php/compare/1.2.0...1.2.1
RateLimitException for HTTP 429 responses (extends ErrorException — no breaking changes)ErrorException and UnserializableResponse via $e->responseMetaInformationCustom for capturing non-standard response headers via $meta->customOverrideStrategy enum for controlling fake() override merging behavioraddHeader() method to TransporterContract for runtime header injection(string) $response->getBody() instead of getContents() for reliable stream readingJSON_UNESCAPED_UNICODE flag for proper Unicode handling in request payloadsFakeable trait using array_replace_recursiveFakeable str_replace callStreamable trait methods for flexibilitySee UPGRADING.md for details. All changes are backwards compatible.
Full Changelog: https://github.com/mozex/anthropic-php/compare/1.1.0...1.2.0
Full Changelog: https://github.com/mozex/anthropic-php/compare/1.0.3...1.1.0
Full Changelog: https://github.com/mozex/anthropic-php/compare/1.0.1...1.0.2
Initial Release
How can I help you explore Laravel packages today?