Send a structured list of input messages with text and/or image content, and the model will generate the next message in the conversation.The Messages API can be used for either single queries or stateless multi-turn conversations.Learn more about the Messages API in our user guide
请求参数
Header 参数
x-api-key
string
必需
Your unique API key for authentication.This key is required in the header of all API requests, to authenticate your account and access Anthropic's services. Get your API key through the Console. Each key is scoped to a Workspace.
示例值:
{{ANTHROPIC_API_KEY}}
anthropic-version
string
必需
The version of the Anthropic API you want to use.Read more about versioning and our version history here.
示例值:
2023-06-01
content-type
string
必需
示例值:
application/json
Body 参数application/json
model
string
必需
The model that will complete your prompt.See models for additional details and options.Required string length: 1 - 256
max_tokens
integer
必需
The maximum number of tokens to generate before stopping.Note that our models may stop before reaching this maximum. This parameter only specifies the absolute maximum number of tokens to generate.Different models have different maximum values for this parameter. See models for details.Required range: x > 1
messages
array [object {2}]
必需
Input messages.Our models are trained to operate on alternating user and assistant conversational turns. When creating a new Message, you specify the prior conversational turns with the messages parameter, and the model then generates the next Message in the conversation. Consecutive user or assistant turns in your request will be combined into a single turn.Each input message must be an object with a role and content. You can specify a single user-role message, or you can include multiple user and assistant messages.If the final message uses the assistant role, the response content will continue immediately from the content in that message. This can be used to constrain part of the model's response.Example with a single user message:
[{"role":"user","content":"Hello, Claude"}]
Example with multiple conversational turns:
[{"role":"user","content":"Hello there."},{"role":"assistant","content":"Hi, I'm Claude. How can I help you?"},{"role":"user","content":"Can you explain LLMs in plain English?"},]
Example with a partially-filled response from Claude:
[{"role":"user","content":"What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun"},{"role":"assistant","content":"The best answer is ("},]
Each input message content may be either a single string or an array of content blocks, where each block has a specific type. Using a string for content is shorthand for an array of one content block of type "text". The following input messages are equivalent:
Starting with Claude 3 models, you can also send image content blocks:
{"role":"user","content":[{"type":"image","source":{"type":"base64","media_type":"image/jpeg","data":"/9j/4AAQSkZJRg...",}},{"type":"text","text":"What is in this image?"}]}
We currently support the base64 source type for images, and the image/jpeg, image/png, image/gif, and image/webp media types.See examples for more input examples.Note that if you want to include a system prompt, you can use the top-level system parameter — there is no "system" role for input messages in the Messages API.
role
string
可选
Conversational role of the generated message.This will always be "assistant".Available options:
assistant
content
string
可选
Content generated by the model.This is an array of content blocks, each of which has a type that determines its shape.Example:
[{"type":"text","text":"Hi, I'm Claude."}]
If the request input messages ended with an assistant turn, then the response content will continue directly from that last turn. You can use this to constrain the model's output.For example, if the input messages were:
[{"role":"user","content":"What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun"},{"role":"assistant","content":"The best answer is ("}]
{"content":[{"text":"Hi! My name is Claude.","type":"text"}],"id":"msg_013Zva2CMHLNnXjNJJKqJ2EF","model":"claude-3-5-sonnet-20241022","role":"assistant","stop_reason":"end_turn","stop_sequence":null,"type":"message","usage":{"input_tokens":2095,"output_tokens":503}}