Models, keys, and your first request
Claude comes in a family — Opus (deepest reasoning), Sonnet (balanced, the everyday workhorse), and Haiku (fastest and cheapest) — and you pick per request by model. To call the API you get an API key from the Console and pass it to the SDK (or the x-api-key header over REST).
Key concept · The Messages request
A request to the Messages API carries: model, max_tokens (a cap on the response), an optional system prompt, and messages — an array of {role, content} objects alternating user and assistant. The response returns content blocks (text and, later, tool-use blocks), a stop_reason, and token usage. That single call is the atom every later feature builds on.
Multi-turn conversations and system prompts
The API is stateless — it doesn't remember prior turns. You create a conversation by resending the growing messages array each time.
Key concept · Append the history; set the role in the system prompt
Multi-turn: after each exchange, append Claude's assistant reply and the next user message to the array and send it again — the whole history is the context. System prompts are separate from the message list: they set Claude's role, tone, expertise, and rules ("You are a support agent for Acme; be concise and cite the policy"). A good system prompt is the cheapest, highest-leverage way to steer behavior consistently across every turn.
Temperature, streaming, and structured data
Three controls shape how the response comes back.
Key concept · Determinism, latency, and machine-readable output
Temperature (0–1) trades determinism for variety: low for consistent, factual, extraction-style tasks; higher for creative range. Response streaming returns tokens as they're generated (server-sent events) so a UI can show text appearing live instead of waiting for the whole reply — important for perceived latency in chat apps. Structured data: when you need machine-readable output, ask for JSON and steer it — describe the exact schema, use examples, and (a reliable trick) prefill the assistant turn with an opening { so Claude continues in JSON. This is what lets Claude's output feed the next step in your program rather than a human reader.
Chapter 19 quiz
Fifteen questions on the request, conversations, and output control. Answer first, then expand Show answer.
Part A · Multiple choice
Which model family member is the fastest and cheapest?
- A Haiku
- B Opus
- C Sonnet
- D They're identical
Show answer
A — Haiku. Opus is deepest reasoning; Sonnet is balanced.
What does the messages array contain?
- A {role, content} objects alternating user and assistant
- B Only the system prompt
- C API keys
- D Model weights
Show answer
A.
What does max_tokens control?
- A A cap on the length of Claude's response
- B The temperature
- C The number of conversations
- D The API key length
Show answer
A.
The Messages API is stateless. To have a multi-turn conversation you:
- A Resend the growing messages array each turn
- B Rely on the server to remember
- C Open a new key each turn
- D Can't have multi-turn chats
Show answer
A. The whole history is the context.
Where do you set Claude's role, tone, and rules?
- A The system prompt
- B max_tokens
- C The API key
- D temperature
Show answer
A. Separate from the messages list.
For consistent, factual extraction, set temperature:
- A Low
- B High
- C Above 1
- D It has no effect
Show answer
A — low. Higher temperature adds variety/creativity.
What does response streaming give you?
- A Tokens as they're generated, so a UI shows text appearing live
- B A cheaper request
- C A different model
- D Guaranteed JSON
Show answer
A. Better perceived latency.
A reliable trick to get JSON output is to:
- A Prefill the assistant turn with an opening {
- B Raise the temperature
- C Remove the system prompt
- D Ask twice
Show answer
A. Plus describe the schema and give examples.
Part B · True or false
The API response includes a stop_reason and token usage.
Show answer
True. Plus content blocks.
You get an API key from the Anthropic Console.
Show answer
True. Passed via the SDK or the x-api-key header.
A system prompt is just another item in the messages array.
Show answer
False. It's a separate top-level parameter.
Structured output lets Claude's response feed the next step of your program.
Show answer
True. Machine-readable rather than for a human reader.
You choose the model per request.
Show answer
True. Via the model parameter.
Part C · Short answer
Name the four core parameters of a Messages request.
Show answer
model, max_tokens, system (optional), and messages.
Name the three output controls covered.
Show answer
Temperature, response streaming, and structured data.