Reference Requests and replies
MessageRequest
A Messages API request. Build one from the three required parts and refine it with withX
methods.
Reference#
MessageRequest(model, maxTokens, messages)#
def apply(model: Model, maxTokens: Int, messages: NonEmptyList[MessageParam]): MessageRequest
def apply(model: Model, maxTokens: Int, first: MessageParam, rest: MessageParam*): MessageRequest
Parameters
model: aModel, such asModel.ClaudeOpus5_5.maxTokens: the most tokens the reply may use.messages: the conversation, starting with a user message: aNonEmptyList, or one message and the rest.
Fields#
final case class MessageRequest private (
model: Model,
maxTokens: Int,
messages: NonEmptyList[MessageParam],
system: Option[SystemPrompt],
tools: List[ToolDefinition],
toolChoice: Option[ToolChoice],
thinking: Option[ThinkingConfig],
outputConfig: Option[OutputConfig],
userId: Option[String],
stopSequences: List[String],
serviceTier: Option[String],
cacheControl: Option[CacheControl],
speed: Option[String],
inferenceGeo: Option[String],
fallbacks: Option[Fallbacks],
extras: JsonObject
)
Caveats
- There are no
temperature,top_portop_kfields: the current API version rejects them. - Anything the SDK does not model yet goes through
withExtra; a modeled field wins over an extra of the same name.
withX methods#
def withModel(m: Model): MessageRequest
def withMaxTokens(n: Int): MessageRequest
def withMessages(ms: NonEmptyList[MessageParam]): MessageRequest
def addMessages(ms: MessageParam*): MessageRequest
def withSystem(text: String): MessageRequest
def withSystem(prompt: SystemPrompt): MessageRequest
def withTools(ts: List[ToolDefinition]): MessageRequest
def addTools(ts: ToolDefinition*): MessageRequest
def withToolChoice(tc: ToolChoice): MessageRequest
def withThinking(t: ThinkingConfig): MessageRequest
def withEffort(e: Effort): MessageRequest
def withOutputFormat(f: OutputFormat): MessageRequest
def withStopSequences(ss: List[String]): MessageRequest
def withCacheControl(c: CacheControl): MessageRequest
def withUserId(id: String): MessageRequest
def withExtra(key: String, value: Json): MessageRequest
Caveats
withUserIdtakes an opaque id. Never put personal data in it.withCacheControlturns on automatic prompt caching for the request.withThinkingandwithEffortare described in Model, thinking and effort.
MessageParam#
def user(text: String): MessageParam
def user(first: ContentBlock, rest: ContentBlock*): MessageParam
def user(blocks: List[ContentBlock]): MessageParam
def assistant(text: String): MessageParam
def assistant(blocks: List[ContentBlock]): MessageParam
def system(text: String): MessageParam
Caveats
MessageParam.systemis an operator instruction in the middle of a conversation. It must follow a user message, and it keeps the prompt cache valid where changing the system prompt would not.
SystemPrompt#
sealed trait SystemPrompt extends Product with Serializable
final case class Text(value: String) extends SystemPrompt
final case class Blocks(blocks: List[ContentBlock]) extends SystemPrompt
def cached(text: String): SystemPrompt
withSystem("…") takes plain text; SystemPrompt.cached marks a long system prompt for caching.
Usage#
Building a request#
MessageRequest(Model.ClaudeOpus5_5, maxTokens = 16000, MessageParam.user("Hello"))
.withSystem("You are terse.")
.withThinking(ThinkingConfig.adaptive)
.withEffort(Effort.High)
Every signature and example on this page is copied, character for character, from the public source of anthropic4s and its Scaladoc. The complete API, every type included, is in the Scaladoc of each module.