Messages examples
Request and response examples for the Messages API
See the API reference for full documentation on available parameters.
Basic request and response
Multiple conversational turns
The Messages API is stateless, which means that you always send the full conversational history to the API. You can use this pattern to build up a conversation over time. Earlier conversational turns don’t necessarily need to actually originate from Claude — you can use synthetic assistant
messages.
Putting words in Claude’s mouth
You can pre-fill part of Claude’s response in the last position of the input messages list. This can be used to shape Claude’s response. The example below uses "max_tokens": 1
to get a single multiple choice answer from Claude.
Vision
Claude can read both text and images in requests. Currently, we support the base64
source type for images, and the image/jpeg
, image/png
, image/gif
, and image/webp
media types. See our vision guide for more details.
Tool use, JSON mode, and computer use (beta)
See our guide for examples for how to use tools with the Messages API. See our computer use (beta) guide for examples of how to control desktop computer environments with the Messages API.
Was this page helpful?