src Loading last commit info...
tests
.gitignore
.phpunit.result.cache
README.md
composer.json
composer.lock
phpunit.xml
README.md

Kipchak Anthropic Driver

A fully typed Anthropic driver for the Kipchak API Development Kit. Built on the official Anthropic PHP SDK, it provides multiple named connections, automatic default model injection, and clean access to the Messages API including streaming and tool use.

Requirements

  • PHP 8.4+
  • Kipchak ^2
  • kipchak/driver-config ^1
  • anthropic-ai/sdk ^0.6

Installation

composer require kipchak/driver-anthropic

Initialise

Add to your drivers/drivers.php:

\Kipchak\Driver\Anthropic\Anthropic::initialise($container);

Configuration

Create config/kipchak.anthropic.php:

<?php

use function Kipchak\Core\env;

return [
    'connections' => [
        'default' => [
            'api_key'       => env('ANTHROPIC_API_KEY', ''),
            'default_model' => env('ANTHROPIC_DEFAULT_MODEL', 'claude-sonnet-4-6'),
        ],
    ],
];
KeyRequiredDescription
api_keyYesAnthropic API key
default_modelNoModel injected automatically when none is specified on a call
max_retriesNoOverride the SDK default of 2 automatic retries

Usage

use Kipchak\Driver\Anthropic\Anthropic;

$anthropic = Anthropic::get();           // 'default' connection
$anthropic = Anthropic::get('secondary'); // named connection

Creating a Message

$response = $anthropic->messages()->create(
    messages: [
        ['role' => 'user', 'content' => 'What is the capital of France?'],
    ],
);

echo $response->content[0]->text; // 'The capital of France is Paris.'

The configured default_model is applied automatically. Pass model: explicitly to override:

$response = $anthropic->messages()->create(
    messages:  [['role' => 'user', 'content' => 'Hello']],
    model:     'claude-haiku-4-5-20251001',
    maxTokens: 512,
    system:    'You are a concise assistant. Reply in one sentence.',
);

Multi-Turn Conversations

$response = $anthropic->messages()->create(
    messages: [
        ['role' => 'user',      'content' => 'My name is Alice.'],
        ['role' => 'assistant', 'content' => 'Hello Alice, how can I help?'],
        ['role' => 'user',      'content' => 'What is my name?'],
    ],
);

Streaming

$stream = $anthropic->messages()->createStream(
    messages: [['role' => 'user', 'content' => 'Tell me a short story.']],
);

foreach ($stream as $event) {
    // handle streaming events
}

Tool Use

$tools = [
    [
        'name'         => 'get_weather',
        'description'  => 'Get the current weather for a location.',
        'input_schema' => [
            'type'       => 'object',
            'properties' => [
                'location' => ['type' => 'string', 'description' => 'City and country'],
            ],
            'required'   => ['location'],
        ],
    ],
];

$response = $anthropic->messages()->create(
    messages: [['role' => 'user', 'content' => 'What is the weather in Dubai?']],
    tools:    $tools,
);

foreach ($response->content as $block) {
    if ($block->type === 'tool_use') {
        $result = callTool($block->name, $block->input);

        // Send the tool result back
        $followup = $anthropic->messages()->create(
            messages: [
                ['role' => 'user', 'content' => 'What is the weather in Dubai?'],
                ['role' => 'assistant', 'content' => $response->content],
                ['role' => 'user', 'content' => [
                    ['type' => 'tool_result', 'tool_use_id' => $block->id, 'content' => $result],
                ]],
            ],
            tools: $tools,
        );
    }
}

Raw SDK Access

For anything not covered by the driver wrapper, use raw() to access the full Anthropic SDK client directly:

$client = $anthropic->raw();

Running Tests

composer install
./vendor/bin/phpunit

License

MIT — see LICENSE!!missing!!.

Please wait...
Connection lost or session expired, reload to recover
Page is in error, reload to recover