Search by

kipchak / driver-anthropic

meezaan

An Anthropic driver for the Kipchak API Development Kit (ADK)

Package info

1x.ax/mamluk/kipchak/drivers/anthropic

Homepage

pkg:composer/kipchak/driver-anthropic

Statistics

Installs: 31

Dependents: 1

Suggesters: 0

2.10 2026-10-08 05:55 UTC

This package is auto-updated.

Last update: 2026-10-08 05:58:09 UTC


README

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 or 8.5
  • Kipchak ^2 || ^3
  • kipchak/driver-config ^1
  • anthropic-ai/sdk ^0.36

Versions

  • 2.10 — requests go through Guzzle from the Kipchak HTTP driver (kipchak/driver-http ^2.10) instead of Symfony's HTTP client, which is no longer installed. A connection can name a psr18 client from config/kipchak.http.php with http_client; without it, it gets a Guzzle client of its own with a 10-second connect timeout, no limit on the whole request, and a 600-second idle timeout. Streaming is unchanged. If your own code used symfony/http-client, require it in your project.
  • 2.0 — upgrades anthropic-ai/sdk to ^0.36 and adds PHP 8.5 support. The driver's config (including max_retries) and message API are unchanged; if you use raw() to call the SDK directly, review the SDK changes. Requires kipchak/core: ^2 || ^3.
  • 1.x — anthropic-ai/sdk ^0.6, kipchak/core ^2.

See the Kipchak upgrade guide for details.

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
http_clientNoName of a psr18 client in config/kipchak.http.php, for a proxy or other timeouts. Without it, a Guzzle client of its own: 10-second connect timeout, no limit on the whole request, 600-second idle timeout

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.