papi-ai / anthropic
Anthropic Claude provider for PapiAI
Requires
- php: ^8.2
- ext-curl: *
- papi-ai/papi-core: ^0.15.6
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- pestphp/pest: ^3.0
- vimeo/psalm: ^6.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Anthropic Claude provider for PapiAI - A simple but powerful PHP library for building AI agents.
Installation
composer require papi-ai/anthropic
Usage
use PapiAI\Core\Agent; use PapiAI\Anthropic\AnthropicProvider; $provider = new AnthropicProvider( apiKey: $_ENV['ANTHROPIC_API_KEY'], ); $agent = new Agent( provider: $provider, model: AnthropicProvider::MODEL_CLAUDE_SONNET_5_5, instructions: 'You are a helpful assistant.', ); $response = $agent->run('Hello!'); echo $response->text;
Available Models
AnthropicProvider::MODEL_CLAUDE_FABLE_5_2 // 'claude-fable-5-2' (most capable) AnthropicProvider::MODEL_CLAUDE_OPUS_5_5 // 'claude-opus-5-5' AnthropicProvider::MODEL_CLAUDE_SONNET_5_5 // 'claude-sonnet-5-5' (default) AnthropicProvider::MODEL_CLAUDE_HAIKU_5_5 // 'claude-haiku-5-5' (fastest) AnthropicProvider::MODEL_CLAUDE_FABLE_5_1 // 'claude-fable-5-1' AnthropicProvider::MODEL_CLAUDE_OPUS_5 // 'claude-opus-5' AnthropicProvider::MODEL_CLAUDE_SONNET_5 // 'claude-sonnet-5' AnthropicProvider::MODEL_CLAUDE_HAIKU_4_5 // 'claude-haiku-4-5' AnthropicProvider::MODEL_CLAUDE_FABLE_5 // 'claude-fable-5' AnthropicProvider::MODEL_CLAUDE_OPUS_4_8 // 'claude-opus-4-8' AnthropicProvider::MODEL_CLAUDE_OPUS_4_7 // 'claude-opus-4-7' AnthropicProvider::MODEL_CLAUDE_OPUS_4_6 // 'claude-opus-4-6' AnthropicProvider::MODEL_CLAUDE_SONNET_4_6 // 'claude-sonnet-4-6'
All of these are active. Haiku 4.5 has passed its retirement floor (15 October 2026) and may go at any time.
The constants alias a ClaudeModel enum, which is the source of truth: it knows each model's thinking API, effort levels, whether a tool can be forced, and its retirement floor. Pass the enum's ->value or the constant, both are the same string.
use PapiAI\Anthropic\ClaudeModel; ClaudeModel::Haiku45->retiresNotBefore(); // '2026-10-15' ClaudeModel::Fable51->acceptsForcedTools(); // false ClaudeModel::tryFrom('claude-opus-6'); // null: unknown IDs are assumed newest, never rejected
Reasoning effort
effort maps to whichever shape the model's generation accepts. Everything from 4.6 on takes adaptive thinking plus an effort level (low to max); Haiku 4.5 takes a token budget carved out of maxTokens. Turning thinking off differs by model: disabled on most, between_tools on Sonnet 5.5, and not at all on Opus 5.5 or Fable, where none narrows to low.
Fable 5.1 and later, Opus 5.5 and Sonnet 5.5 do not accept a forced tool choice. toolChoice of required or a named tool throws a ProviderException before any request is made; auto and none work as normal.
Every model from 4.7 on rejects temperature, so passing one throws a ProviderException before any request is made.
Long sessions
Three options keep a long tool-using session coherent. Pass them to the constructor:
use PapiAI\Anthropic\AnthropicProvider; use PapiAI\Anthropic\Compaction; use PapiAI\Anthropic\ThinkingReplay; use PapiAI\Anthropic\ToolResultClearing; $provider = new AnthropicProvider( apiKey: $_ENV['ANTHROPIC_API_KEY'], thinkingReplay: ThinkingReplay::DropOnEdit, contextEdits: [new ToolResultClearing(), new Compaction()], ); $response = $provider->chat($messages, ['effort' => 'high']); $messages[] = $response->toMessage(); // carries the thinking and any compaction summary
Thinking replay. Within a tool-use turn Anthropic wants the model's thinking back. Each block is
tied to the system prompt, the tools and every earlier message as they were when it was written, so
only a history that grows by appending can send it back. ThinkingReplay::Off, the default, leaves
it out. FailOnEdit sends it, and an edited history fails with a 400. DropOnEdit sends it, and the
API drops whatever an edit invalidated. Both need adaptive thinking to tell the API which to do: on a
request without it (none on Sonnet 5.5, or a model that does not think by default) FailOnEdit
sends the thinking anyway and DropOnEdit leaves it out. Blocks are also tied to the model, and on
Sonnet and Haiku 5.5 to the account, so the API drops any it cannot read.
Changing instructions mid-session. Append a Message::system() rather than editing the first
one. On Opus 4.8 and later, Fable, Mythos, and Sonnet and Haiku 5.5 it stays where it is, so nothing
already sent changes. It must follow a user message and be the last message or come just before an
assistant one; anywhere else throws a ProviderException before any request is made. On older
models it replaces the system prompt.
Server-side context. Compaction has the API summarise the conversation once its input passes
the trigger (150,000 tokens by default, 50,000 at least), and ToolResultClearing has it clear old
tool results (from 100,000 tokens, keeping the latest three). Neither edits the history you send, so
neither invalidates thinking. The token counts include what compaction spent.
Features
- Tool/function calling
- Vision/multimodal support
- Streaming support
Creating It by Name
An application that picks its provider from configuration can build this one without naming its class:
use PapiAI\Core\Factory\ComposerDiscovery; use PapiAI\Core\Factory\ProviderRegistry; $registry = new ProviderRegistry(new ComposerDiscovery()); $provider = $registry->create('anthropic', [ 'api_key' => $_ENV['ANTHROPIC_API_KEY'], ]);
It takes api_key (required), model, max_tokens, effort, replay_thinking (off,
fail_on_edit or drop_on_edit), compact_at and clear_tool_results_at (both in input tokens).
Anything it is not given falls back to the constructor's defaults, and a missing, unknown or invalid
setting throws ConfigurationException before any request is made.
License
MIT