usefulcheatsheets.com
Technology · AI

Claude API Basics

A quick reference for making your first Claude API call, picking a model, and reading the response.

Beginner
usefulcheatsheets.com/technology/ai/claude-api-basics01 / 25
Technology/AI/Claude API Basics

TL;DR

  1. 01Install the Anthropic SDK and set your ANTHROPIC_API_KEY variable.
  2. 02Call client.messages.create() with a model, prompt, and max_tokens.
  3. 03Check stop_reason before reading content to handle responses safely.
usefulcheatsheets.com/technology/ai/claude-api-basics02 / 25
Technology/AI/Claude API Basics

Installation and Auth

pip install anthropic Installs the Python SDK from PyPI so you can start calling Claude.
pip install anthropic
usefulcheatsheets.com/technology/ai/claude-api-basics03 / 25
Technology/AI/Claude API Basics

Installation and Auth

(cont.)
npm install @anthropic-ai/sdk Installs the TypeScript and Node SDK for JavaScript projects.
npm install @anthropic-ai/sdk
usefulcheatsheets.com/technology/ai/claude-api-basics04 / 25
Technology/AI/Claude API Basics

Installation and Auth

(cont.)
ANTHROPIC_API_KEY Stores your API key as an environment variable instead of hard-coding it.
export ANTHROPIC_API_KEY="sk-ant-..."
usefulcheatsheets.com/technology/ai/claude-api-basics05 / 25
Technology/AI/Claude API Basics

Your First Request

Anthropic() Creates a client that reads the API key from the environment automatically.
import anthropic
client = anthropic.Anthropic()
usefulcheatsheets.com/technology/ai/claude-api-basics06 / 25
Technology/AI/Claude API Basics

Your First Request

(cont.)
messages.create() Sends a prompt to Claude with a model, max_tokens, and a messages array.
message = client.messages.create(
    model="claude-opus-4-8",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Explain closures in one paragraph."}]
)
usefulcheatsheets.com/technology/ai/claude-api-basics07 / 25
Technology/AI/Claude API Basics

Your First Request

(cont.)
system parameter Gives Claude a persistent role or persona for the whole conversation.
message = client.messages.create(
    model="claude-opus-4-8",
    max_tokens=1024,
    system="You are a concise technical writer.",
    messages=[{"role": "user", "content": "Explain closures."}]
)
usefulcheatsheets.com/technology/ai/claude-api-basics08 / 25
Technology/AI/Claude API Basics

Your First Request

(cont.)
max_tokens Caps the response length; short answers need less, long docs need more.
# Short answers: 256-512  |  Long docs: 4096-8192  |  Max: 128000
usefulcheatsheets.com/technology/ai/claude-api-basics09 / 25
Technology/AI/Claude API Basics

Reading the Response

content[0].text Reads the text of Claude's reply from the response object.
print(message.content[0].text)
usefulcheatsheets.com/technology/ai/claude-api-basics10 / 25
Technology/AI/Claude API Basics

Reading the Response

(cont.)
stop_reason Explains why generation stopped: end_turn, max_tokens, tool_use, or refusal.
if message.stop_reason == "end_turn":
    print(message.content[0].text)
usefulcheatsheets.com/technology/ai/claude-api-basics11 / 25
Technology/AI/Claude API Basics

Reading the Response

(cont.)
usage Reports input and output token counts so you can track cost.
print(message.usage.input_tokens)
print(message.usage.output_tokens)
usefulcheatsheets.com/technology/ai/claude-api-basics12 / 25
Technology/AI/Claude API Basics

Reading the Response

(cont.)
message.model Confirms which model actually served the request.
print(message.model)  # e.g. "claude-opus-4-8"
usefulcheatsheets.com/technology/ai/claude-api-basics13 / 25
Technology/AI/Claude API Basics

Choosing a Model

Claude Opus 4.8 Best for hard reasoning and agentic tasks; the default for most new integrations.
claude-opus-4-8
usefulcheatsheets.com/technology/ai/claude-api-basics14 / 25
Technology/AI/Claude API Basics

Choosing a Model

(cont.)
Claude Sonnet 5 Near-Opus quality on coding and agentic work at a lower cost.
claude-sonnet-5
usefulcheatsheets.com/technology/ai/claude-api-basics15 / 25
Technology/AI/Claude API Basics

Choosing a Model

(cont.)
Claude Haiku 4.5 Fastest and cheapest option for high-volume tasks like tagging or routing.
claude-haiku-4-5
usefulcheatsheets.com/technology/ai/claude-api-basics16 / 25
Technology/AI/Claude API Basics

Choosing a Model

(cont.)
Claude Fable 5 Anthropic's most capable model for the hardest, longest-horizon tasks.
claude-fable-5
usefulcheatsheets.com/technology/ai/claude-api-basics17 / 25
Technology/AI/Claude API Basics
In Practice: Send a Prompt, Read the Reply
  1. 01Creating the client once and reusing it avoids re-reading credentials on every call.
  2. 02Passing max_tokens and model explicitly keeps the request valid and predictable in production.
  3. 03Checking stop_reason before touching content prevents crashes when a response is truncated or refused.
  4. 04Printing usage after every call makes token cost visible instead of a surprise on the bill.
import anthropic

client = anthropic.Anthropic()

message = client.messages.create(
    model="claude-opus-4-8",
    max_tokens=1024,
    system="You are a concise technical writer.",
    messages=[{"role": "user", "content": "Explain what a closure is, in two sentences."}]
)

if message.stop_reason == "end_turn":
    print(message.content[0].text)
elif message.stop_reason == "max_tokens":
    print("Response was cut off - increase max_tokens")
elif message.stop_reason == "refusal":
    print("Request declined by safety classifier")

print(f"Tokens used: {message.usage.input_tokens} in / {message.usage.output_tokens} out")
usefulcheatsheets.com/technology/ai/claude-api-basics18 / 25
Technology/AI/Claude API Basics
FAQ 01 / 05

Set the ANTHROPIC_API_KEY environment variable and the SDK reads it automatically when you create a client. Pass api_key directly to the constructor only if you manage multiple keys or a secrets manager.

usefulcheatsheets.com/technology/ai/claude-api-basics19 / 25
Technology/AI/Claude API Basics
FAQ 02 / 05

Start with claude-opus-4-8 for most new integrations — it balances reasoning quality with cost. Switch to claude-haiku-4-5 for high-volume, latency-sensitive tasks like tagging or routing, and reach for claude-sonnet-5 as a faster, cheaper middle ground.

usefulcheatsheets.com/technology/ai/claude-api-basics20 / 25
Technology/AI/Claude API Basics
FAQ 03 / 05

stop_reason explains why Claude stopped generating: end_turn means a normal finish, max_tokens means the response got cut off, tool_use means Claude called a tool, and refusal means the safety classifier declined the request. Always check it before reading content.

usefulcheatsheets.com/technology/ai/claude-api-basics21 / 25
Technology/AI/Claude API Basics
FAQ 04 / 05

The Messages API requires model, max_tokens, and messages on every request. Missing any of these — most often max_tokens — fails validation before the request is sent, so double-check your payload against the required fields first.

usefulcheatsheets.com/technology/ai/claude-api-basics22 / 25
Technology/AI/Claude API Basics
FAQ 05 / 05

Append each turn to the messages array in order, alternating user and assistant roles. Claude has no memory between calls, so you must resend the full history on every request.

messages = [
    {"role": "user", "content": "What is a closure?"},
    {"role": "assistant", "content": "A closure captures variables from its enclosing scope."},
    {"role": "user", "content": "Give me a JavaScript example."},
]
usefulcheatsheets.com/technology/ai/claude-api-basics23 / 25
Technology/AI/Claude API Basics

Tips

  1. 01Pin an exact model ID like claude-opus-4-8 in production so a later model update never silently changes your app's behavior.
  2. 02Set max_tokens generously for long or open-ended replies, since Claude stops the moment it reaches that limit, even mid-sentence.

Warnings

  1. 01max_tokens is required on every request — omitting it raises a validation error before Claude even sees your prompt.
  2. 02Never hard-code an API key in source control; load it from an environment variable or a secrets manager instead.
usefulcheatsheets.com/technology/ai/claude-api-basics24 / 25
Useful Cheatsheets

Fast, clear reference sheets for technology, finance, health, and everyday adulting.

usefulcheatsheets.com