Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/phpaisdk/anthropic/llms.txt

Use this file to discover all available pages before exploring further.

This guide walks you through installing the aisdk/anthropic package, authenticating with your Anthropic API key, and running a basic text generation request against a Claude model. By the end you will have a working PHP script that calls the Anthropic API and prints the model’s response.
1

Install via Composer

Add aisdk/anthropic to your project using Composer. The aisdk/core package is pulled in automatically as a dependency.
composer require aisdk/anthropic
2

Set your API key

Export your Anthropic API key as an environment variable. The provider reads ANTHROPIC_API_KEY automatically — no extra bootstrap code needed.
export ANTHROPIC_API_KEY="sk-ant-..."
You can also set the key in a .env file and load it with your framework’s environment helper, or pass it directly via Anthropic::create() (see Configuration).
3

Generate your first response

Use Generate::text() to build a request, attach a Claude model with Anthropic::model(), add an instruction and a prompt, then call ->run() to execute.
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;
$result->text contains the plain-text response from Claude. The ->instructions() call maps to Anthropic’s top-level system field.
4

Set a default model

If every request in your application targets the same model, register it once with Generate::model(). Subsequent calls to Generate::text() will use it automatically without repeating ->model().
Generate::model(Anthropic::model('claude-sonnet-4'));

$result = Generate::text('Explain closures in PHP.')->run();
This is particularly useful in service providers or bootstrap files where you configure the SDK once at startup.

Next Steps

Now that you have a working request, explore the rest of what aisdk/anthropic supports.

Streaming

Stream Claude’s response token-by-token for real-time output in your UI.

Tool Calling

Give Claude access to your PHP functions and let it call them automatically.

Structured Output

Receive validated JSON objects matching a schema you define.

Configuration

Customise the base URL, version header, and additional HTTP headers.

Build docs developers (and LLMs) love