Claude API from A to Z · Lesson 2
The Messages API structure
system/user/assistant roles, content blocks (text, image), multi-turn conversations with thinking blocks, mid-conversation system messages, and strict JSON through structured outputs instead of prefill.
Three roles
- system — instructions and behavior for the whole conversation, set with the top-level
systemparameter. - user — the user's turns.
- assistant — the model's replies. You add these back into history for the next turn.
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic(); // reads ANTHROPIC_API_KEY
const res = await client.messages.create({
model: "claude-opus-5-5",
max_tokens: 1024,
system: "You are a concise technical assistant. Get to the point.",
messages: [
{ role: "user", content: "What is idempotency?" },
],
});
for (const block of res.content) {
if (block.type === "text") console.log(block.text);
}
Current Claude models use adaptive thinking by default, so thinking blocks may come before the text in res.content. Look for the block of type text; reading res.content[0].text is unreliable here.
The rest of this lesson opens after a free sign-up
Sign up with email or Google: the first two lessons of every course stay open for good, and the whole catalog is open for 7 days. No card needed.


