PHP code example of rkzack / akit

1. Go to this page and download the library: Download rkzack/akit library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rkzack / akit example snippets



eads ANTHROPIC_API_KEY from the environment
$answer = akit_prompt('What is the capital of France?');
echo $answer; // "The capital of France is Paris."

use Akit\Client;

$client = new Client(apiKey: 'sk-ant-...');

$response = $client->prompt('Explain quantum entanglement in one sentence.');

echo $response->text();
echo 'Tokens used: ' . $response->totalTokens();

$response = $client->prompt(
    prompt: 'Write me a greeting.',
    system: 'You are a friendly pirate. Always respond in pirate speak.',
);

echo $response; // Response::__toString() returns text

$conv = $client->conversation(
    system: 'You are a helpful coding assistant. Be concise.'
);

$r1 = $conv->say('What is a closure in PHP?');
echo $r1->text();

// Claude has full context from the previous turn
$r2 = $conv->say('Show me an example.');
echo $r2->text();

echo 'Turns: ' . $conv->turns(); // 2

$client->stream(
    prompt:  'Write a short story about a robot learning to bake.',
    onChunk: function (string $chunk): void {
        echo $chunk;
        // In a web context, also call ob_flush() and flush()
    },
);

use Akit\Model;

// Use the Model enum for autocomplete / type safety
$response = $client->prompt(
    prompt: 'Analyse this complex problem...',
    model:  Model::OPUS_4_7->value,
);

// Or pass the string directly
$response = $client->prompt('Hello', model: 'claude-haiku-4-5-20251001');

use Akit\Config;

// Reads ANTHROPIC_API_KEY, optionally override model / max_tokens
$client = new Client(Config::fromEnv(
    model:     Model::HAIKU_4_5->value,
    maxTokens: 512,
));

use Akit\Message;

$messages = [
    Message::user('My lucky number is 7.'),
    Message::assistant('Noted — your lucky number is 7!'),
    Message::user('What is my lucky number times 6?'),
];

$response = $client->chat($messages, system: 'Be a helpful calculator.');
echo $response->text(); // "42"

new Client(Config|string $config)

use Akit\Exceptions\ApiException;
use Akit\Exceptions\AuthException;

try {
    $response = $client->prompt('Hello');
} catch (AuthException $e) {
    // 401 — invalid or missing API key
    echo 'Bad API key: ' . $e->getMessage();
} catch (ApiException $e) {
    // Other API errors (rate limits, overload, validation, etc.)
    echo "API error {$e->statusCode} ({$e->type}): {$e->getMessage()}";
}