messages.create is the call every other feature in this course extends.
A Messages API request has three required parts:
model: which Claude model should answer, for exampleclaude-opus-5-5.max_tokens: the most tokens (small pieces of text) Claude may write in its reply. It is a hard ceiling, not a target.messages: the conversation so far, as a list of dictionaries with arole(userorassistant) andcontent.
The reply is a Python object, not a plain string. Its content field is a list of content blocks. A block is one typed piece of the answer: a text block holds words, and later you will meet tool_use and thinking blocks. Even a simple answer comes back as a list with one text block, so the safe pattern is to loop and keep only blocks whose type is text.
Content you send can also be a list of blocks instead of a plain string. That is how you will later attach images and documents next to your text.
Two things often surprise newcomers. First, max_tokens is required; there is no default. If Claude hits it, the answer is cut off mid-sentence. Second, recent Claude models reject sampling settings such as temperature (randomness) with an error. You steer the output with clear instructions instead.
Model names change every few months. Keep the model id in one constant so you can update it in a single place.
Example
import anthropic
MODEL = "claude-opus-5-5" # model names change; check the models overview
client = anthropic.Anthropic()
message = client.messages.create(
model=MODEL,
max_tokens=500,
messages=[
{"role": "user", "content": "Give me three names for a pet tortoise."}
],
)
for block in message.content:
if block.type == "text":
print(block.text)Remember
- Required: model, max_tokens, messages
- The reply's
contentis a list of typed blocks; read thetextones - Keep the model id in one constant
Watch out for
- Using
message.content[0].textblindly; the first block is not always text once tools or thinking are on.