anthropic-client
Integrate Anthropic Claude API with Laravel. HTTP client, message format, error handling. Use when calling Claude models from Laravel applications.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Integrate Anthropic Claude API with Laravel. HTTP client, message format, error handling. Use when calling Claude models from Laravel applications.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
PHP Artisan CLI patterns including make commands, flags, custom commands, and AI-friendly usage.
Advanced Eloquent ORM patterns including relationships, eager loading, factories, and query optimization.
Laravel development best practices covering service providers, dependency injection, facades, and the Laravel Way.
Laravel Tinker best practices for debugging, testing ideas, and data exploration. When and how to use safely.
Laravel-native error handling patterns. Renderable exceptions, Livewire traits strategies, and user-facing notifications.
API security patterns for Laravel. Rate limiting, headers, CORS, Sanctum tokens, input validation. Use when building or securing API endpoints.
| name | anthropic-client |
| description | Integrate Anthropic Claude API with Laravel. HTTP client, message format, error handling. Use when calling Claude models from Laravel applications. |
Call Anthropic Claude APIs from Laravel using native HTTP client.
ANTHROPIC_API_KEY=sk-ant-...
ANTHROPIC_BASE_URL=https://api.anthropic.com
// config/services.php
'anthropic' => [
'api_key' => env('ANTHROPIC_API_KEY'),
'base_url' => env('ANTHROPIC_BASE_URL', 'https://api.anthropic.com'),
'timeout' => 60,
'version' => '2023-06-01',
],
namespace App\Services;
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Response;
class AnthropicClient
{
private PendingRequest $http;
public function __construct()
{
$this->http = Http::baseUrl(config('services.anthropic.base_url'))
->timeout(config('services.anthropic.timeout', 60))
->withHeaders([
'x-api-key' => config('services.anthropic.api_key'),
'anthropic-version' => config('services.anthropic.version', '2023-06-01'),
'content-type' => 'application/json',
])
->retry(3, 100, function ($exception) {
return $exception instanceof \Illuminate\Http\Client\RequestException
&& $exception->response?->status() === 429;
});
}
/**
* Create a message (chat completion)
*/
public function message(
array $messages,
string $model = 'claude-3-5-sonnet-20241022',
int $maxTokens = 4096,
?string $system = null,
float $temperature = 1.0,
): array {
$payload = [
'model' => $model,
'max_tokens' => $maxTokens,
'messages' => $this->formatMessages($messages),
'temperature' => $temperature,
];
if ($system) {
$payload['system'] = $system;
}
$response = $this->http->post('/v1/messages', $payload);
$this->handleErrors($response);
return $response->json();
}
/**
* Simple prompt helper
*/
public function prompt(
string $prompt,
string $model = 'claude-3-5-sonnet-20241022',
?string $systemPrompt = null,
): string {
$messages = [
['role' => 'user', 'content' => $prompt],
];
$response = $this->message(
messages: $messages,
model: $model,
system: $systemPrompt,
);
return $response['content'][0]['text'];
}
/**
* Format messages for Anthropic API
* Anthropic requires alternating user/assistant messages
*/
private function formatMessages(array $messages): array
{
$formatted = [];
foreach ($messages as $message) {
// Skip system messages (handled separately)
if ($message['role'] === 'system') {
continue;
}
$formatted[] = [
'role' => $message['role'],
'content' => $message['content'],
];
}
return $formatted;
}
/**
* Handle API errors
*/
private function handleErrors(Response $response): void
{
if ($response->successful()) {
return;
}
$error = $response->json('error');
match ($response->status()) {
401 => throw new \Illuminate\Auth\AuthenticationException('Invalid API key'),
default => throw new \Illuminate\Http\Client\RequestException($response),
};
}
}
$claude = app(AnthropicClient::class);
$response = $claude->prompt(
prompt: 'Explain Laravel queues in 3 sentences.',
systemPrompt: 'You are a Laravel expert. Be concise.'
);
$messages = [
['role' => 'user', 'content' => 'What is Laravel?'],
['role' => 'assistant', 'content' => 'Laravel is a PHP web framework...'],
['role' => 'user', 'content' => 'How do I install it?'],
];
$response = $claude->message(
messages: $messages,
system: 'You are a helpful Laravel assistant.',
);
$answer = $response['content'][0]['text'];
// Claude 3 supports 200K tokens
$longDocument = file_get_contents('large_document.txt');
$response = $claude->prompt(
prompt: "Summarize this document:\n\n{$longDocument}",
model: 'claude-3-5-sonnet-20241022',
);
| Model | Context | Speed | Use Case |
|---|---|---|---|
claude-3-5-sonnet-20241022 | 200K | Fast | Best balance |
claude-3-opus-20240229 | 200K | Slow | Complex tasks |
claude-3-haiku-20240307 | 200K | Fastest | Simple tasks, cheap |
| Feature | OpenAI | Anthropic |
|---|---|---|
| Auth header | Authorization: Bearer | x-api-key |
| System prompt | In messages array | Separate system param |
| Response format | choices[0].message.content | content[0].text |
| Max tokens | Optional | Required |
namespace App\Providers;
use App\Services\AnthropicClient;
use Illuminate\Support\ServiceProvider;
class AnthropicServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->singleton(AnthropicClient::class, function () {
return new AnthropicClient();
});
}
}
Remember: Claude requires
max_tokensto be set. System prompts are passed separately, not in the messages array.