Course map
Stage 1: Foundations
Stage 2: Machine Learning
Stage 3: Deep Learning
Stage 4: LLMs & Generative AI
Stage 5: Production & Specialization

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_reason tells you end_turn (finished) vs max_tokens (truncated — check this in production).
  • response.content is a list of typed blocks — text, tool_use, and others. Loop and filter by block.type; never assume content[0] is text.

🧪 Quick quiz

  1. Where do standing instructions for the model go?
  2. How should you read the text from response.content?
Show the answers
  1. In the system parameter — per the SDK documentation, not the first user message.
  2. 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.

Search the guide