aisdk/anthropic

Official Anthropic provider for the PHP AI SDK.

Maintainers

Package info

github.com/phpaisdk/anthropic

pkg:composer/aisdk/anthropic

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.1.0-alpha1 2026-06-30 05:22 UTC

This package is auto-updated.

Last update: 2026-06-30 05:42:02 UTC


README

Official Anthropic provider for the PHP AI SDK.

Installation

composer require aisdk/anthropic

Basic Usage

use AiSdk\Anthropic;
use AiSdk\Generate;

$result = Generate::text()
    ->model(Anthropic::model('claude-sonnet-4'))
    ->instructions('Write short, clear answers.')
    ->prompt('Explain closures in PHP.')
    ->run();

echo $result->text;

Default model shorthand:

Generate::model(Anthropic::model('claude-sonnet-4'));

$result = Generate::text('Explain closures in PHP.')->run();

Configuration

Environment Variables

Variable Description Default
ANTHROPIC_API_KEY API key for authentication Required
ANTHROPIC_BASE_URL Base URL for API requests https://api.anthropic.com/v1
ANTHROPIC_VERSION API version header 2023-06-01

Programmatic Configuration

$provider = Anthropic::create([
    'apiKey' => 'sk-ant-...',
    'baseUrl' => 'https://api.anthropic.com/v1',
    'version' => '2023-06-01',
    'headers' => ['anthropic-beta' => 'extended-thinking-2025-05-14'],
]);

Supported Capabilities

Capability Support
Text generation Native
Streaming Native
Tool calling Native
Structured output Adapted (forced tool use)
Reasoning Native (thinking blocks)
Text input Native
Image input Native
File input Native (documents)

Streaming

use AiSdk\Anthropic;
use AiSdk\Generate;

$stream = Generate::text('Tell me a story.')
    ->model(Anthropic::model('claude-sonnet-4'))
    ->stream();

foreach ($stream->chunks() as $chunk) {
    echo $chunk;
}

$result = $stream->run();

Structured Output

Anthropic does not natively support json_schema response format. Structured output is adapted through forced tool use:

use AiSdk\Anthropic;
use AiSdk\Generate;
use AiSdk\Schema;

$result = Generate::text()
    ->model(Anthropic::model('claude-sonnet-4'))
    ->prompt('Extract the city and country from: Lahore, Pakistan.')
    ->output(Schema::object(
        name: 'address',
        properties: [
            Schema::string(name: 'city')->required(),
            Schema::string(name: 'country')->required(),
        ],
    ))
    ->run();

Reasoning

use AiSdk\Anthropic;
use AiSdk\Generate;
use AiSdk\Reasoning;

$result = Generate::text('Solve: what is 2+2?')
    ->model(Anthropic::model('claude-sonnet-4'))
    ->reasoning(Reasoning::effort('high'))
    ->run();

Tools

use AiSdk\Anthropic;
use AiSdk\Generate;
use AiSdk\Schema;
use AiSdk\Tool;

$weather = Tool::make('weather', 'Get current weather')
    ->input(Schema::string(name: 'city')->required())
    ->run(fn (string $city): string => "Sunny in {$city}");

$result = Generate::text()
    ->model(Anthropic::model('claude-sonnet-4'))
    ->prompt('What is the weather in Lahore?')
    ->tool($weather)
    ->run();

Custom Model Registration

Register new Anthropic models without waiting for a package release:

use AiSdk\Anthropic;
use AiSdk\Capability;

Anthropic::registerModel('claude-5-sonnet', capabilities: [
    Capability::TextGeneration,
    Capability::Streaming,
    Capability::ToolCalling,
    Capability::StructuredOutput,
    Capability::Reasoning,
    Capability::TextInput,
    Capability::ImageInput,
]);

$result = Generate::text('Hello')
    ->model(Anthropic::model('claude-5-sonnet'))
    ->run();

Use ModelDefinition only when you need metadata or adapted-capability details.

Provider-Specific Options

Raw provider options can be passed as an escape hatch:

$result = Generate::text('Hello')
    ->model(Anthropic::model('claude-sonnet-4'))
    ->providerOptions('anthropic', [
        'raw' => ['top_k' => 40],
    ])
    ->run();

Testing

composer test

Links