Anthropic API & Python SDK · Messages · tools · streaming · thinking · verified against the Anthropic API (2026)

Claude API cheat sheet

Build with Claude through the Anthropic Messages API and the official anthropic SDK. One endpoint (client.messages.create) does chat, vision, tool use, structured output, and extended thinking. Pick a model by string, cache big prefixes for up to ~90% savings, stream long responses, and let Claude call your tools. This sheet is Python-first (the SDK is nearly identical in TypeScript/Go/Java/Ruby/C#/PHP). Defaults shown target Claude Opus 5. Model IDs, prices & API shapes verified 2026.

setup & models core requests cost & caching tools & structured errors & ecosystem gotcha most common

Verified 2026-08-31 against the Anthropic API docs & the anthropic Python SDK (1.x). Everything goes through POST /v1/messages; tools & output constraints are features of that one endpoint. Model strings are complete as-is — never append a date suffix. Prices are first-party API rates per 1M tokens.

Outline

Create a client, call messages.create, read content blocks. Then stream, think, cache, batch, call tools, and constrain output — all on the one Messages endpoint.

Setup & models

  1. 1Install, client & auth
  2. 2First message & blocks
  3. 3Models & pricing

Core requests

  1. 4System, turns & params
  2. 5Streaming
  3. 6Thinking & effort

Cost & tools

  1. 7Prompt caching
  2. 8Batches & token counting
  3. 9Tool use
  4. 10Structured output & vision

Server tools & ecosystem

  1. 11Server tools
  2. 12Errors & ecosystem

Setup & Models

Install the SDK, create a client, and pick a model.

1Install, client & authanthropic SDK
2First message & blocksmessages.create
3Models & pricingpick a string

Core Requests

System prompts, multi-turn, streaming, and extended thinking.

4System, turns & paramsshape the request
5Streamingfor long output
6Thinking & effortreasoning depth

Cost & Tools

Cache prefixes, batch offline work, and let Claude call your tools.

7Prompt cachingup to ~90% off
8Batches & token countingoffline & estimates
9Tool usefunction calling
10Structured output & visiontyped JSON & images

Server Tools & Ecosystem

Anthropic-hosted tools, robust error handling, and the wider Claude surface.

11Server toolsAnthropic-hosted
12Errors & ecosystemrobustness & surfaces

Worth memorizing

anthropic.Anthropic()reads ANTHROPIC_API_KEY
messages.create(model, max_tokens, messages)the one endpoint
resp.content = [blocks]check block.type before .text
claude-opus-5default; sonnet-5 for volume; haiku-4-5 cheap
model IDs are completenever append a date suffix
statelessresend full history each turn
messages.stream + get_final_messagefor long output
thinking adaptive + effortbudget_tokens removed on new models
cache_control ephemeralprefix match; check cache_read_input_tokens
batches = 50% costkey results by custom_id
tools + tool_runnerstrict:true; json.loads inputs
messages.parse(output_config)typed structured output