PHP Luminova: Anthropic AI Client Integration
Use Anthropic in Luminova for high-throughput batch processing of chat and text tasks. Includes practical PHP examples for creating, tracking, and managing batch jobs efficiently.
The Luminova Anthropic client (Luminova\AI\Client\Anthropic) provides access to the Anthropic Messages API, supporting single and multi-turn conversations, vision, web search, and large-scale asynchronous batch processing with the Claude model family. It does not support custom model creation or fine-tuning.
This guide covers Anthropic-specific methods and usage within Luminova.
For shared AI features and client implementation, see: Base AI Class (Luminova\Base\AI)
To get started, create an API key and review the docs:https://docs.anthropic.com/en/api
Usages
Instantiation
use Luminova\AI\Client\Anthropic;
$client = new Anthropic(apiKey: env('ANTHROPIC_API_KEY'));With a Specific API Version
$client = new Anthropic(
apiKey: env('ANTHROPIC_API_KEY'),
version: '2023-06-01', // Optional, defaults to 2023-06-01
);With Beta Features Enabled
$client = new Anthropic(
apiKey: env('ANTHROPIC_API_KEY'),
betaFeatures: 'interleaved-thinking-2025-05-14',
);Via Application Config
Set handler = 'Anthropic' in App\Config\AI and use the AI manager:
use Luminova\AI\AI;
$reply = AI::message('Hello from Claude!');Beta Features
Enable or disable Anthropic beta feature flags at runtime. Multiple flags are comma-separated.
$client->setBetaFeatures('interleaved-thinking-2025-05-14');
$reply = $client->message('Reason step by step: what is 17 × 43?');Clear Beta Features
$client->setBetaFeatures(null);Text Generation
Single Message
$reply = $client->message('Summarize the Stoic philosophy in two sentences.');
echo $reply[0]['text'] ?? '';Chat Completion (Multi-Turn)
$reply = $client->chat([
['role' => 'system', 'content' => 'You are a concise Python tutor.'],
['role' => 'user', 'content' => 'What is a list comprehension?'],
['role' => 'assistant', 'content' => 'A compact way to create lists using a single line...'],
['role' => 'user', 'content' => 'Show me a practical example.'],
]);
echo $reply[0]['text'] ?? '';With Explicit Options
$reply = $client->chat('Write a limerick about APIs.', [
'model' => 'claude-sonnet-4-6',
'max_tokens' => 256,
'temperature' => 0.9,
]);With a System Prompt via Options
$reply = $client->chat(
[['role' => 'user', 'content' => 'Who are you?']],
['system' => 'You are Lumi, a helpful Luminova assistant.']
);Note:
Anthropic does not accept
systemas part of the messages array. The client extracts it automatically from the messages and moves it to the request body.
Web Search
Anthropic's web search tool lets Claude retrieve live information from the web before answering.
$results = $client->webSearch('Latest Claude model releases');
print_r($results);With Domain Filtering and Location Context
$results = $client->webSearch('Current ringgit exchange rate', [
'allowed_domains' => ['bnm.gov.my', 'xe.com'],
'user_location' => [
'type' => 'approximate',
'city' => 'Kuala Lumpur',
'country' => 'MY',
'timezone' => 'Asia/Kuala_Lumpur',
],
'max_uses' => 3,
]);Restrict with Blocked Domains
$results = $client->webSearch('PHP security best practices', [
'blocked_domains' => ['w3schools.com'],
]);Use the Older ZDR-Eligible Tool Version
$results = $client->webSearch('Open source licenses compared', [
'search_tool' => 'web_search_20250305',
]);Vision (Image Understanding)
Analyze an Image by URL
$content = $client->vision(
'Describe what you see in this image.',
'https://upload.wikimedia.org/wikipedia/commons/a/a7/Camponotus_flavomarginatus_ant.jpg'
);
echo $content[0]['text'] ?? '';Analyze a Local File
$content = $client->vision(
'Is there any text in this screenshot? If so, transcribe it.',
'/tmp/screenshot.png',
['model' => 'claude-sonnet-4-6']
);
echo $content[0]['text'] ?? '';Compare Multiple Images
$content = $client->vision(
'Which of these two logos is more recognizable and why?',
[
'https://example.com/logo-a.png',
'https://example.com/logo-b.png',
]
);Supported image formats:
jpeg,png,gif,webp. Local files are base64-encoded automatically.
Batch Processing
The Message Batches API processes large volumes of requests asynchronously at 50% of the standard per-token cost. Each item in the request array must have a custom_id and a params object matching the standard Messages API body.
Submitting a Batch
$batch = $client->batch([
[
'custom_id' => 'req-001',
'params' => [
'model' => 'claude-haiku-4-5-20251001',
'max_tokens' => 128,
'messages' => [['role' => 'user', 'content' => 'Translate "hello" to French.']],
],
],
[
'custom_id' => 'req-002',
'params' => [
'model' => 'claude-haiku-4-5-20251001',
'max_tokens' => 128,
'messages' => [['role' => 'user', 'content' => 'Translate "hello" to Spanish.']],
],
],
]);
$batchId = $batch['id'];
echo 'Batch submitted: ' . $batchId;Polling Batch Status
Poll until processing_status is 'ended':
do {
$status = $client->batchStatus($batchId);
echo 'Status: ' . $status['processing_status'] . PHP_EOL;
sleep(5);
} while ($status['processing_status'] === 'in_progress');
echo 'Results available at: ' . $status['results_url'];Cancelling a Batch
$result = $client->cancelBatch('msgbatch_abc123');
echo 'Cancellation requested. Status: ' . $result['processing_status'];Listing Models
$models = $client->models();
foreach ($models as $model) {
echo $model['id'] . PHP_EOL;
}Get a Specific Model
$info = $client->model('claude-sonnet-4-6');
print_r($info);Error Handling
use Luminova\Exceptions\AIException;
try {
$reply = $client->message('Hello!');
} catch (AIException $e) {
echo 'Anthropic Error: ' . $e->getMessage();
}Class Definition
- Full namespace:
\Luminova\AI\Client\Anthropic - Parent class: Luminova\Base\AI
- Implements: \Luminova\Interface\AIClientInterface
Properties
endpoints
Anthropic API endpoints.
protected array $endpoints = [
'messages' => 'messages',
'models' => 'models',
'batches' => 'messages/batches',
];Methods
constructor
Create a new Anthropic AI client instance.
public __construct(string $apiKey, string $version = null, ?string $betaFeatures = null): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$apiKey | string | Anthropic secret API key (sk-ant-…). |
$version | string | API version header value (default: 2023-06-01 current stable). |
$betaFeatures | string|null | Optional comma-separated beta feature flags sent via anthropic-beta header.(e.g, 'interleaved-thinking-2025-05-14'). |
Example:
Basic usage:
$client = new Anthropic(apiKey: env('ANTHROPIC_API_KEY'));
$reply = $client->message('Hello, Anthropic!');With beta features enabled:
$client = new Anthropic(
apiKey: env('ANTHROPIC_API_KEY'),
betaFeatures: 'interleaved-thinking-2025-05-14',
);
$reply = $client->message('Reason carefully: what is 17 × 43?');
App\Config\AI- For default application AI configuration.
setBetaFeatures
Enable one or more Anthropic beta features for all subsequent requests.
Sets the anthropic-beta request header. Multiple flags can be passed as a comma-separated string. Passing null clears any active flags.
public setBetaFeatures(?string $features): staticParameters:
| Parameter | Type | Description |
|---|---|---|
$features | string|null | Comma-separated beta feature flag(s), ornull to disable beta headers. |
batch
Submit a batch of message requests for asynchronous processing.
The Message Batches API processes large volumes of requests at 50% of the standard per-token cost. Each item in $requests must be a ['custom_id' => string, 'params' => array] entry where params follows the standard Messages API body shape.
public batch(array $requests, array $options = []): arrayParameters:
| Parameter | Type | Description |
|---|---|---|
$requests | array<int,array<string,mixed>> | Batch items. |
$options | array | Optional additional request parameters. |
Return Value:
array - Batch creation response containing id, processing_status,request_counts, created_at, expires_at, and more.
Throws:
- \Luminova\Exceptions\RuntimeException - On network or client errors.
- \Luminova\Exceptions\JsonException - On malformed JSON in the response.
batchStatus
Poll the processing status of a message batch.
public batchStatus(string $batchId): arrayParameters:
| Parameter | Type | Description |
|---|---|---|
$batchId | string | Batch ID returned by batch(). |
Return Value:
array - Batch status object. Key processing_status is either 'in_progress' or 'ended'. When 'ended', retrieve results via the results_url field or the Anthropic console.
Throws:
- \Luminova\Exceptions\RuntimeException - On network or client errors.
cancelBatch
Cancel an in-progress message batch.
public cancelBatch(string $batchId): arrayParameters:
| Parameter | Type | Description |
|---|---|---|
$batchId | string | Batch ID to cancel. |
Return Value:
array - Updated batch object (processing_status will become 'ended' once cancellation is complete).
Throws:
- \Luminova\Exceptions\RuntimeException - On network or client errors.