Your first Messages API call

Claude API · Essentials · 10 min

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 example claude-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 a role (user or assistant) and content.

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 content is a list of typed blocks; read the text ones
  • Keep the model id in one constant

Watch out for

  • Using message.content[0].text blindly; the first block is not always text once tools or thinking are on.

Check yourself

  1. What does max_tokens control?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: platform.claude.com/docs/en/build-with-claude/working-with-messages