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.

Generate::text() sends a request to POST /messages on the Anthropic Messages API and returns a TextModelResponse containing the generated text, token usage, finish reason, and the raw API payload. The provider handles authentication, serialization, and response parsing automatically.

Basic Usage

Attach a model, optional instructions, and a prompt, then call ->run() to receive the complete response.
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;

System Instructions

There are two ways to supply system-level instructions and both are merged into the Anthropic top-level system field before the request is sent. Via .instructions() — a convenience string attached directly to the generation request:
$result = Generate::text()
    ->model(Anthropic::model('claude-sonnet-4'))
    ->instructions('You are a concise technical assistant.')
    ->prompt('What is a PHP trait?')
    ->run();
Via Message::system() in multi-turn history — system entries placed inside the messages array are extracted and appended to the same system field:
use AiSdk\Message;

$result = Generate::text()
    ->model(Anthropic::model('claude-sonnet-4'))
    ->instructions('Be terse')
    ->messages([
        Message::system('Use metric units'),
        Message::user('What is the weather in Lahore?'),
    ])
    ->run();
In the example above the Anthropic request receives system: "Be terse\n\nUse metric units". The Message::system() entry is removed from the messages array before it is sent — Anthropic does not accept system-role messages inside the messages field.

Response Fields

FieldTypeDescription
$result->textstringThe generated text content.
$result->usage->inputTokensintNumber of tokens in the prompt and system instructions.
$result->usage->outputTokensintNumber of tokens in the generated response.
$result->finishReasonFinishReasonOne of Stop, Length, ToolCalls, or Unknown.
$result->rawResponsearrayThe full, unmodified response payload returned by the Anthropic API.
The finishReason values map from Anthropic stop reasons as follows:
Anthropic stop_reasonSDK FinishReason
end_turn, stop_sequenceStop
max_tokensLength
tool_useToolCalls
(any other value)Unknown

Setting a Default Model

To avoid specifying a model on every call, register a default model once at application bootstrap using Generate::model(). All subsequent Generate::text() calls will use it automatically.
Generate::model(Anthropic::model('claude-sonnet-4'));

$result = Generate::text('Your prompt here')->run();
Set a default model once at application bootstrap using Generate::model() to avoid passing the model on every call. This is especially useful in frameworks where you configure services in a service provider or bootstrap file.

Build docs developers (and LLMs) love