Class MessageCreateParams.Body
-
- All Implemented Interfaces:
public final class MessageCreateParams.BodyThe parameters of a request for the next message in a conversation.
See the Messages API guide to learn more about requests and responses.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public final classMessageCreateParams.Body.BuilderA builder for Body.
-
Method Summary
Modifier and Type Method Description final LongmaxTokens()The maximum number of tokens to generate before stopping. final List<MessageParam>messages()Input messages. final Modelmodel()The model that will complete your prompt. final Optional<CacheControlEphemeral>cacheControl()Top-level cache control automatically applies a cache_control marker to the last cacheable block in the request. final Optional<MessageCreateParamsContainer>container()Container identifier for reuse across requests. final Optional<DiagnosticsParam>diagnostics()Request-level diagnostics. final Optional<String>inferenceGeo()Specifies the geographic region for inference processing. final Optional<Metadata>metadata()An object describing metadata about the request. final Optional<OutputConfig>outputConfig()Configuration options for the model's output, such as the output format. final Optional<MessageCreateParams.ServiceTier>serviceTier()Determines whether to use priority capacity (if available) or standard capacity for this request. final Optional<MessageCreateParams.Speed>speed()The inference speed mode for this request. final Optional<List<String>>stopSequences()Custom text sequences that will cause the model to stop generating. final Optional<MessageCreateParams.System>system()System prompt. final Optional<ThinkingConfigParam>thinking()Configuration for Claude's thinking. final Optional<ToolChoice>toolChoice()How the model should use the provided tools. final Optional<List<ToolUnion>>tools()Definitions of tools that the model may use. final JsonField<Long>_maxTokens()Returns the raw JSON value of maxTokens. final JsonField<List<MessageParam>>_messages()Returns the raw JSON value of messages. final JsonField<Model>_model()Returns the raw JSON value of model. final JsonField<CacheControlEphemeral>_cacheControl()Returns the raw JSON value of cacheControl. final JsonField<MessageCreateParamsContainer>_container()Returns the raw JSON value of container. final JsonField<DiagnosticsParam>_diagnostics()Returns the raw JSON value of diagnostics. final JsonField<String>_inferenceGeo()Returns the raw JSON value of inferenceGeo. final JsonField<Metadata>_metadata()Returns the raw JSON value of metadata. final JsonField<OutputConfig>_outputConfig()Returns the raw JSON value of outputConfig. final JsonField<MessageCreateParams.ServiceTier>_serviceTier()Returns the raw JSON value of serviceTier. final JsonField<MessageCreateParams.Speed>_speed()Returns the raw JSON value of speed. final JsonField<List<String>>_stopSequences()Returns the raw JSON value of stopSequences. final JsonField<MessageCreateParams.System>_system()Returns the raw JSON value of system. final JsonField<ThinkingConfigParam>_thinking()Returns the raw JSON value of thinking. final JsonField<ToolChoice>_toolChoice()Returns the raw JSON value of toolChoice. final JsonField<List<ToolUnion>>_tools()Returns the raw JSON value of tools. final Map<String, JsonValue>_additionalProperties()final MessageCreateParams.Body.BuildertoBuilder()final MessageCreateParams.Bodyvalidate()Validates that the types of all values in this object match their expected types recursively. final BooleanisValid()Booleanequals(Object other)IntegerhashCode()StringtoString()final static MessageCreateParams.Body.Builderbuilder()Returns a mutable builder for constructing an instance of Body. -
-
Method Detail
-
maxTokens
final Long maxTokens()
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
0to populate the prompt cache without generating a response.Different models have different maximum values for this parameter. See models for details.
-
messages
final List<MessageParam> messages()
Input messages.
Our models are trained to operate on alternating
userandassistantconversational turns. When creating a newMessage, you specify the prior conversational turns with themessagesparameter, and the model then generates the nextMessagein the conversation. Consecutiveuserorassistantturns in your request will be combined into a single turn.Each input message must be an object with a
roleandcontent. You can specify a singleuser-role message, or you can include multipleuserandassistantmessages.If the final message uses the
assistantrole, the response content will continue immediately from the content in that message. This can be used to constrain part of the model's response. This is called prefill. On models that don't support prefill, creating a message that ends with a partialassistantresponse returns a 400 error. See Prefill not supported.Example with a single
usermessage:[{"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, for models that support prefill:
[ {"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
contentmay be either a singlestringor an array of content blocks, where each block has a specifictype. Using astringforcontentis shorthand for an array of one content block of type"text". The following input messages are equivalent:{"role": "user", "content": "Hello, Claude"}{"role": "user", "content": [{"type": "text", "text": "Hello, Claude"}]}See input examples.
Note that if you want to include a system prompt, you can use the top-level
systemparameter.There is a limit of 100,000 messages in a single request.
-
model
final Model model()
The model that will complete your prompt.
See models for additional details and options.
-
cacheControl
final Optional<CacheControlEphemeral> cacheControl()
Top-level cache control automatically applies a cache_control marker to the last cacheable block in the request.
-
container
final Optional<MessageCreateParamsContainer> container()
Container identifier for reuse across requests.
-
diagnostics
final Optional<DiagnosticsParam> diagnostics()
Request-level diagnostics. Supply
previous_message_idto have the response includediagnostics.cache_miss_reasonexplaining any prompt-cache divergence from that prior request.
-
inferenceGeo
final Optional<String> inferenceGeo()
Specifies the geographic region for inference processing. If not specified, the workspace's
default_inference_geois used.
-
outputConfig
final Optional<OutputConfig> outputConfig()
Configuration options for the model's output, such as the output format.
-
serviceTier
final Optional<MessageCreateParams.ServiceTier> serviceTier()
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
final Optional<MessageCreateParams.Speed> speed()
The inference speed mode for this request.
"fast"enables high output-tokens-per-second inference.Some models require the
fast-mode-2026-02-01beta header for anyspeedvalue. See fast mode for which models.
-
stopSequences
final Optional<List<String>> stopSequences()
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_reasonof"end_turn".If you want the model to stop generating when it encounters custom strings of text, you can use the
stop_sequencesparameter. If the model encounters one of the custom sequences, the responsestop_reasonvalue will be"stop_sequence"and the responsestop_sequencevalue will contain the matched stop sequence.
-
system
final Optional<MessageCreateParams.System> system()
System prompt.
A system prompt is a way of providing context and instructions to Claude, such as specifying a particular goal or role. See our guide to system prompts.
-
thinking
final Optional<ThinkingConfigParam> thinking()
Configuration for Claude's thinking.
With
{"type": "adaptive"}, Claude decides when and how much to think. With{"type": "enabled"}(manual extended thinking), you set abudget_tokensof at least 1,024. Thinking tokens count toward yourmax_tokenslimit.Which
typevalues are accepted, and what happens when you omitthinking, depend on the model. See thinking for each model's behavior.
-
toolChoice
final Optional<ToolChoice> toolChoice()
How the model should use the provided tools. The model can use a specific tool, any available tool, decide by itself, or not use tools at all.
-
tools
final Optional<List<ToolUnion>> tools()
Definitions of tools that the model may use.
If you include
toolsin your API request, the model may returntool_usecontent blocks that represent the model's use of those tools. You can then run those tools using the tool input generated by the model and then optionally return results back to the model usingtool_resultcontent blocks.There are two types of tools: client tools and server tools. The behavior described below applies to client tools. For server tools, see their individual documentation as each has its own behavior (e.g., the web search tool).
Each tool definition includes:
name: Name of the tool.description: Optional, but strongly-recommended description of the tool.input_schema: JSON schema for the toolinputshape that the model will produce intool_useoutput content blocks.
For example, if you defined
toolsas:[ { "name": "get_stock_price", "description": "Get the current stock price for a given ticker symbol.", "input_schema": { "type": "object", "properties": { "ticker": { "type": "string", "description": "The stock ticker symbol, e.g. AAPL for Apple Inc." } }, "required": ["ticker"] } } ]And then asked the model "What's the S&P 500 at today?", the model might produce
tool_usecontent blocks in the response like this:[ { "type": "tool_use", "id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV", "name": "get_stock_price", "input": { "ticker": "^GSPC" } } ]You might then run your
get_stock_pricetool with{"ticker": "^GSPC"}as an input, and return the following back to the model in a subsequentusermessage:[ { "type": "tool_result", "tool_use_id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV", "content": "259.75 USD" } ]Tools can be used for workflows that include running client-side tools and functions, or more generally whenever you want the model to produce a particular JSON structure of output.
See our guide for more details.
-
_maxTokens
final JsonField<Long> _maxTokens()
Returns the raw JSON value of maxTokens.
Unlike maxTokens, this method doesn't throw if the JSON field has an unexpected type.
-
_messages
final JsonField<List<MessageParam>> _messages()
Returns the raw JSON value of messages.
Unlike messages, this method doesn't throw if the JSON field has an unexpected type.
-
_model
final JsonField<Model> _model()
Returns the raw JSON value of model.
Unlike model, this method doesn't throw if the JSON field has an unexpected type.
-
_cacheControl
final JsonField<CacheControlEphemeral> _cacheControl()
Returns the raw JSON value of cacheControl.
Unlike cacheControl, this method doesn't throw if the JSON field has an unexpected type.
-
_container
final JsonField<MessageCreateParamsContainer> _container()
Returns the raw JSON value of container.
Unlike container, this method doesn't throw if the JSON field has an unexpected type.
-
_diagnostics
final JsonField<DiagnosticsParam> _diagnostics()
Returns the raw JSON value of diagnostics.
Unlike diagnostics, this method doesn't throw if the JSON field has an unexpected type.
-
_inferenceGeo
final JsonField<String> _inferenceGeo()
Returns the raw JSON value of inferenceGeo.
Unlike inferenceGeo, this method doesn't throw if the JSON field has an unexpected type.
-
_metadata
final JsonField<Metadata> _metadata()
Returns the raw JSON value of metadata.
Unlike metadata, this method doesn't throw if the JSON field has an unexpected type.
-
_outputConfig
final JsonField<OutputConfig> _outputConfig()
Returns the raw JSON value of outputConfig.
Unlike outputConfig, this method doesn't throw if the JSON field has an unexpected type.
-
_serviceTier
final JsonField<MessageCreateParams.ServiceTier> _serviceTier()
Returns the raw JSON value of serviceTier.
Unlike serviceTier, this method doesn't throw if the JSON field has an unexpected type.
-
_speed
final JsonField<MessageCreateParams.Speed> _speed()
Returns the raw JSON value of speed.
Unlike speed, this method doesn't throw if the JSON field has an unexpected type.
-
_stopSequences
final JsonField<List<String>> _stopSequences()
Returns the raw JSON value of stopSequences.
Unlike stopSequences, this method doesn't throw if the JSON field has an unexpected type.
-
_system
final JsonField<MessageCreateParams.System> _system()
Returns the raw JSON value of system.
Unlike system, this method doesn't throw if the JSON field has an unexpected type.
-
_thinking
final JsonField<ThinkingConfigParam> _thinking()
Returns the raw JSON value of thinking.
Unlike thinking, this method doesn't throw if the JSON field has an unexpected type.
-
_toolChoice
final JsonField<ToolChoice> _toolChoice()
Returns the raw JSON value of toolChoice.
Unlike toolChoice, this method doesn't throw if the JSON field has an unexpected type.
-
_tools
final JsonField<List<ToolUnion>> _tools()
Returns the raw JSON value of tools.
Unlike tools, this method doesn't throw if the JSON field has an unexpected type.
-
_additionalProperties
final Map<String, JsonValue> _additionalProperties()
-
toBuilder
final MessageCreateParams.Body.Builder toBuilder()
-
validate
final MessageCreateParams.Body validate()
Validates that the types of all values in this object match their expected types recursively.
This method is not forwards compatible with new types from the API for existing fields.
-
builder
final static MessageCreateParams.Body.Builder builder()
Returns a mutable builder for constructing an instance of Body.
The following fields are required:
.maxTokens() .messages() .model()
-
-
-
-