Skip to content

Cheatsheet: Your first Claude API call

POST https://api.anthropic.com/v1/messages
Headers
x-api-key: $ANTHROPIC_API_KEY
anthropic-version: 2023-06-01
Content-Type: application/json
Body (required)
model precise identifier, e.g. claude-opus-4-8
max_tokens output ceiling, integer
messages [{role: user | assistant, content: "..."}, ...]
ends with role: user (model generates next assistant)
Body (optional)
system standing instructions (persona, format, constraint)
LanguageInstall
Pythonpip install anthropic
TypeScriptnpm install @anthropic-ai/sdk
Javacom.anthropic:anthropic-java (Maven Central)
Gogithub.com/anthropics/anthropic-sdk-go
C-sharp, PHP, Rubyofficial SDKs published
CLIbrew install anthropics/tap/ant

All SDKs auto-read ANTHROPIC_API_KEY from the environment.

FamilyIdentifierWhen to reach for it
Opusclaude-opus-4-8Flagship Opus; complex reasoning, agentic coding
Sonnetclaude-sonnet-4-6Frontier intelligence at scale
Haikuclaude-haiku-4-5Fastest; near-frontier intelligence

Lesson 3 of this track is the proper selection lesson.

{
"id": "msg_01HCDu5LRGeP2o7s2xGmxyx8",
"type": "message",
"role": "assistant",
"content": [{ "type": "text", "text": "..." }],
"model": "claude-opus-4-8",
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": { "input_tokens": 21, "output_tokens": 305 }
}
ValueMeaning
end_turnModel finished naturally (the normal case)
max_tokensTruncated; raise the cap or split the request
stop_sequenceYour custom stop string was generated
tool_useModel is requesting a tool call (lesson 4)
messages = [
{ role: "user", content: "Hello, Claude" },
{ role: "assistant", content: "Hello!" },
{ role: "user", content: "Can you describe LLMs to me?" }
]
  • API holds NOTHING between calls.
  • Your application owns conversation state.
  • Roles alternate user / assistant, ending with user.
  • Synthesized prior turns are OK (the API does not check).
  • Every turn pays for the full prior history as input tokens.

System parameter (where instructions live)

Section titled “System parameter (where instructions live)”
client.messages.create(
model="claude-opus-4-8",
max_tokens=1024,
system="You are a terse code reviewer. Return only the changes...",
messages=[{ "role": "user", "content": "<diff>" }],
)

System text is NOT a message. No role. No alternation. Standing instruction for the whole call.

FailureRecognize byFix
API key not setAuth error on first callexport ANTHROPIC_API_KEY='...'; persist in shell profile
Wrong model nameUnknown-model errorUse precise identifier (claude-opus-4-8), not “Claude” or “Opus”
max-tokens too smallSuccessful response, stop_reason: max_tokensRaise the cap or split into smaller turns
Content read as a stringWorks once, breaks on tool use / visionIterate response.content and dispatch on each block’s type
Treating API as statefulModel “forgets” earlier turnsSend the entire conversation history every call

What this lesson does NOT cover (and where to find it)

Section titled “What this lesson does NOT cover (and where to find it)”
TopicLands at
Streaming, batches, error handlingLesson 2
Model selection + extended thinkingLesson 3
Tool use (define + handle)Lesson 4
Server-side tools (web search, web fetch, code execution)Lesson 5
Model Context ProtocolLesson 6
Prompt caching + context managementLesson 7
Agent loop, agent patternsLessons 8-11
Shipping a Claude applicationLesson 12
  • Endpoint: the URL the request goes to (https://api.anthropic.com/v1/messages).
  • Stateless: the API holds no state between calls; conversation state lives in your application.
  • System parameter: standing instructions, separate from the messages list.
  • Stop reason: the field telling you why generation ended; check on every response.
  • Anthropic public Claude docs, Get Started + Working with the Messages API, at https://platform.claude.com/docs/en/get-started and https://platform.claude.com/docs/en/build-with-claude/working-with-messages. Structural-mirror citation; see references.