Eine Nachricht erstellen
POST/v1/messages
Sende eine strukturierte Liste von Eingabenachrichten mit Text- und/oder Bildinhalten, und das Modell generiert die nächste Nachricht in der Konversation.
Die Messages API kann sowohl für einzelne Anfragen als auch für zustandslose Multi-Turn-Konversationen verwendet werden.
Mehr über die Messages API erfährst du in unserem Benutzerhandbuch
Parameters
Returns
Eine Nachricht erstellen
<?php
require_once dirname(__DIR__) . '/vendor/autoload.php';
$client = new Client(apiKey: 'my-anthropic-api-key');
$betaMessage = $client->beta->messages->create(
maxTokens: 1024,
messages: [
[
'content' => 'Hello, world',
'role' => 'user',
'clearAt' => 'next_user_message',
'outputConfig' => ['effort' => 'low'],
],
],
model: Model::CLAUDE_OPUS_5,
cacheControl: ['type' => 'ephemeral', 'ttl' => '5m'],
compaction: ['type' => 'summarize', 'instructions' => 'instructions'],
container: [
'id' => 'id',
'skills' => [
['skillID' => 'pdf', 'type' => 'anthropic', 'version' => 'latest']
],
],
contextManagement: [
'edits' => [
[
'type' => 'clear_tool_uses_20250919',
'clearAtLeast' => ['type' => 'input_tokens', 'value' => 0],
'clearToolInputs' => true,
'excludeTools' => ['string'],
'keep' => ['type' => 'tool_uses', 'value' => 0],
'trigger' => ['type' => 'input_tokens', 'value' => 1],
],
],
],
diagnostics: ['previousMessageID' => 'previous_message_id'],
fallbackCreditToken: 'x',
fallbacks: 'default',
inferenceGeo: 'inference_geo',
mcpServers: [
[
'name' => 'name',
'type' => 'url',
'url' => 'url',
'authorizationToken' => 'authorization_token',
'toolConfiguration' => ['allowedTools' => ['string'], 'enabled' => true],
],
],
metadata: ['userID' => '13803d75-b4b5-4c3e-b2a2-6f21399b021b'],
outputConfig: [
'effort' => 'low',
'format' => ['schema' => ['foo' => 'bar'], 'type' => 'json_schema'],
'taskBudget' => ['total' => 1024, 'type' => 'tokens', 'remaining' => 0],
],
outputFormat: ['schema' => ['foo' => 'bar'], 'type' => 'json_schema'],
serviceTier: 'auto',
speed: 'standard',
stopSequences: ['string'],
system: [
[
'text' => 'Today\'s date is 2024-06-01.',
'type' => 'text',
'cacheControl' => ['type' => 'ephemeral', 'ttl' => '5m'],
'citations' => [
[
'citedText' => 'The grass is green. The sky is blue.',
'documentIndex' => 0,
'documentTitle' => 'x',
'endCharIndex' => 0,
'startCharIndex' => 0,
'type' => 'char_location',
],
],
],
],
temperature: 1,
thinking: [
'type' => 'adaptive',
'blockBinding' => [
'prefixMismatchBehavior' => BetaThinkingPrefixMismatchBehavior::ERROR
],
'display' => 'summarized',
],
toolChoice: ['type' => 'auto', 'disableParallelToolUse' => true],
tools: [
[
'inputSchema' => [
'type' => 'object',
'properties' => ['location' => 'bar', 'unit' => 'bar'],
'required' => ['location'],
],
'name' => 'name',
'allowedCallers' => ['direct'],
'cacheControl' => ['type' => 'ephemeral', 'ttl' => '5m'],
'deferLoading' => true,
'description' => 'Get the current weather in a given location',
'eagerInputStreaming' => true,
'inputExamples' => [['foo' => 'bar']],
'strict' => true,
'type' => 'custom',
],
],
topK: 5,
topP: 0.7,
betas: [AnthropicBeta::MESSAGE_BATCHES_2024_09_24],
userProfileID: 'anthropic-user-profile-id',
workspaceID: 'wrkspc_011CZkZaBF1tNoB5wlCeusgy',
);
var_dump($betaMessage);Response 200
{
"id": "msg_013Zva2CMHLNnXjNJJKqJ2EF",
"container": {
"id": "container_011CpZohnwH4vuy7gazohgSP",
"expires_at": "2019-12-27T18:11:19.117Z",
"skills": [
{
"skill_id": "pdf",
"type": "anthropic",
"version": "latest"
}
]
},
"content": [
{
"citations": [
{
"cited_text": "The grass is green. The sky is blue.",
"document_index": 0,
"document_title": "My Document",
"end_char_index": 0,
"file_id": "file_011CNha8iCJcU1wXNR6q4V8w",
"start_char_index": 0,
"type": "char_location"
}
],
"text": "Hi! My name is Claude.",
"type": "text"
}
],
"context_management": {
"applied_edits": [
{
"cleared_input_tokens": 0,
"cleared_tool_uses": 0,
"type": "clear_tool_uses_20250919"
}
]
},
"diagnostics": {
"cache_miss_reason": {
"cache_missed_input_tokens": 0,
"type": "model_changed"
}
},
"model": "claude-opus-5",
"role": "assistant",
"stop_details": {
"category": "cyber",
"explanation": "This request was declined because it conflicts with Anthropic's Usage Policy.",
"fallback_credit_token": "QW50aHJvcGljL0NsYXVkZQ==",
"fallback_has_prefill_claim": true,
"recommended_model": "claude-opus-4-8",
"type": "refusal"
},
"stop_reason": "end_turn",
"stop_sequence": null,
"type": "message",
"usage": {
"cache_creation": {
"ephemeral_1h_input_tokens": 0,
"ephemeral_5m_input_tokens": 0
},
"cache_creation_input_tokens": 2051,
"cache_read_input_tokens": 2051,
"fallback_credit": {
"status": {
"type": "redeemed"
}
},
"inference_geo": "global",
"input_tokens": 2095,
"iterations": [
{
"cache_creation": {
"ephemeral_1h_input_tokens": 0,
"ephemeral_5m_input_tokens": 0
},
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 0,
"input_tokens": 0,
"model": "claude-fable-5-1",
"output_tokens": 0,
"type": "message"
}
],
"output_tokens": 503,
"output_tokens_details": {
"thinking_tokens": 0
},
"server_tool_use": {
"web_fetch_requests": 2,
"web_search_requests": 0
},
"service_tier": "standard",
"speed": "standard"
},
"input_transformations": [
{
"path": "path",
"reason": "model_binding_mismatch",
"type": "thinking_dropped"
}
]
}Returns Examples
Response 200
{
"id": "msg_013Zva2CMHLNnXjNJJKqJ2EF",
"container": {
"id": "container_011CpZohnwH4vuy7gazohgSP",
"expires_at": "2019-12-27T18:11:19.117Z",
"skills": [
{
"skill_id": "pdf",
"type": "anthropic",
"version": "latest"
}
]
},
"content": [
{
"citations": [
{
"cited_text": "The grass is green. The sky is blue.",
"document_index": 0,
"document_title": "My Document",
"end_char_index": 0,
"file_id": "file_011CNha8iCJcU1wXNR6q4V8w",
"start_char_index": 0,
"type": "char_location"
}
],
"text": "Hi! My name is Claude.",
"type": "text"
}
],
"context_management": {
"applied_edits": [
{
"cleared_input_tokens": 0,
"cleared_tool_uses": 0,
"type": "clear_tool_uses_20250919"
}
]
},
"diagnostics": {
"cache_miss_reason": {
"cache_missed_input_tokens": 0,
"type": "model_changed"
}
},
"model": "claude-opus-5",
"role": "assistant",
"stop_details": {
"category": "cyber",
"explanation": "This request was declined because it conflicts with Anthropic's Usage Policy.",
"fallback_credit_token": "QW50aHJvcGljL0NsYXVkZQ==",
"fallback_has_prefill_claim": true,
"recommended_model": "claude-opus-4-8",
"type": "refusal"
},
"stop_reason": "end_turn",
"stop_sequence": null,
"type": "message",
"usage": {
"cache_creation": {
"ephemeral_1h_input_tokens": 0,
"ephemeral_5m_input_tokens": 0
},
"cache_creation_input_tokens": 2051,
"cache_read_input_tokens": 2051,
"fallback_credit": {
"status": {
"type": "redeemed"
}
},
"inference_geo": "global",
"input_tokens": 2095,
"iterations": [
{
"cache_creation": {
"ephemeral_1h_input_tokens": 0,
"ephemeral_5m_input_tokens": 0
},
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 0,
"input_tokens": 0,
"model": "claude-fable-5-1",
"output_tokens": 0,
"type": "message"
}
],
"output_tokens": 503,
"output_tokens_details": {
"thinking_tokens": 0
},
"server_tool_use": {
"web_fetch_requests": 2,
"web_search_requests": 0
},
"service_tier": "standard",
"speed": "standard"
},
"input_transformations": [
{
"path": "path",
"reason": "model_binding_mismatch",
"type": "thinking_dropped"
}
]
}