Part 14 / Topic 1 of 7
The Messages API
One endpoint does everything: send messages, get a message back.
# examples/01_calling_claude_with_the_python_sdk.py (ran against the local mock)
import anthropic
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY
response = client.messages.create(
model="claude-opus-5-5",
max_tokens=16000,
system="You are a support assistant for an online store. Answer in two sentences.",
messages=[{"role": "user", "content": "How long does standard shipping take?"}],
)
for block in response.content:
if block.type == "text":
print(block.text)
print("stop_reason:", response.stop_reason) # end_turn
print("tokens in/out:", response.usage.input_tokens, response.usage.output_tokens)
The parts that matter:
system— standing instructions (role, rules, tone) live here, not in the first user message.max_tokens— a hard cap on the reply;stop_reasontells youend_turn(finished) vsmax_tokens(truncated — check this in production).response.contentis a list of typed blocks — text, tool_use, and others. Loop and filter byblock.type; never assumecontent[0]is text.
🧪 Quick quiz
- Where do standing instructions for the model go?
- How should you read the text from
response.content?
Show the answers
- In the
systemparameter — per the SDK documentation, not the first user message. - Loop over the blocks and use those with
type == "text"— content is a list of typed blocks (verified against the SDK).
Done here? The practice set for Part 14 — 4 exercises with solutions — is on the part page.