Create a Message

Changed on

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

post/v1/messages

Request

  • Base URL
    https://api.anthropic.com
  • Auth
    none declared

Headers

anthropic-versionstring

The version of the Claude API you want to use.

Read more about versioning and our version history here.

anthropic-user-profile-idstring

The user profile ID to attribute this request to. Use when acting on behalf of a party other than your organization.

anthropic-workspace-idstring

Optional header to select the Workspace for this request. The value is a Workspace ID (for example, wrkspc_011CZkZaBF1tNoB5wlCeusgy).

Only needed for credentials that can act on more than one Workspace. A credential that belongs to a specific Workspace may omit it; if sent, it must match that Workspace.

Request body

inference_geostring nullable

Specifies the geographic region for inference processing. If not specified, the workspace's default_inference_geo is used.

max_tokensinteger required

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.

Set to 0 to populate the prompt cache without generating a response.

Different models have different maximum values for this parameter. See models for details.

service_tier'auto' | 'standard_only'

Determines whether to use priority capacity (if available) or standard capacity for this request.

Anthropic offers different levels of service for your API requests. See service-tiers for details.

speed'standard' | 'fast'
stop_sequencesstring[]

Custom text sequences that will cause the model to stop generating.

Our models will normally stop when they have naturally completed their turn, which will result in a response stop_reason of "end_turn".

If you want the model to stop generating when it encounters custom strings of text, you can use the stop_sequences parameter. If the model encounters one of the custom sequences, the response stop_reason value will be "stop_sequence" and the response stop_sequence value will contain the matched stop sequence.

streamboolean

Whether to incrementally stream the response using server-sent events. When true, SDKs return a raw event stream.

In the TypeScript, Python and Ruby SDKs, the recommended way to stream is messages.stream(). It sets stream for you and accumulates the events into the final message. See Streaming with SDKs for an example in each language.

Example request

{
  "max_tokens": 1024,
  "messages": [
    {
      "content": "Hello, world",
      "role": "user"
    }
  ],
  "model": "claude-opus-5-5"
}

Response

Message object.

idstring required

Unique object identifier.

The format and length of IDs may change over time.

type'message' required

Object type.

For Messages, this is always "message".

role'assistant' required

Conversational role of the generated message.

This will always be "assistant".

stop_reason'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use' | 'pause_turn' | 'refusal' | 'model_context_window_exceeded' required
stop_sequencestring nullable required

Which custom stop sequence was generated, if any.

This value will be a non-null string if one of your custom stop sequences was generated.

Example response

{
  "content": [
    {
      "citations": null,
      "text": "Hi! My name is Claude.",
      "type": "text"
    }
  ],
  "diagnostics": null,
  "id": "msg_013Zva2CMHLNnXjNJJKqJ2EF",
  "model": "claude-opus-5-5",
  "role": "assistant",
  "stop_details": null,
  "stop_reason": "end_turn",
  "stop_sequence": null,
  "type": "message",
  "usage": {
    "input_tokens": 2095,
    "output_tokens": 503,
    "speed": "standard"
  }
}

Changes

    • breaking

      the response's property type changed from no type to object for status

    • info

      added the optional property / to the response with the status

    • info

      added to the / response property anyOf list for the response status

    • info

      removed discriminator from response property for the response status

    • info

      removed from the response property oneOf list for the response status

    • info

      added the required property / to the response with the status

    • info

      added the required property / to the response with the status

    • breaking

      the / request property type changed from object to no type

    • warning

      removed the request property //

    • warning

      removed the request property //

    • warning

      removed the request property

    • warning

      removed the request property

    • warning

      removed the request property

    • info

      added assistant system user discriminator mapping keys to the / request property

    • info

      added to the / request property oneOf list