Anthropic API & Python SDK · Messages · tools · files · 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. This edition adds the Files API (PDFs/images + citations), Anthropic-schema client tools (bash, text-editor, computer use, memory), server tools (web search/fetch, code execution), the MCP connector & Skills, and context management for long agent runs. Python-first (the SDK is nearly identical in TS/Go/Java/Ruby/C#/PHP). Defaults target Claude Opus 5. Model IDs, prices & API shapes verified 2026.

setup & models core requests cost & context tools files, output & ecosystem gotcha most common

Verified 2026-09-08 against the Anthropic API docs (platform.claude.com) & the anthropic Python SDK (1.x). Everything goes through POST /v1/messages; tools, files & output constraints are features of that one endpoint. Model strings are complete as-is — never append a date suffix. Tool type strings DO carry dated suffixes (e.g. web_search_20260209) — use the exact string. Prices are first-party API rates per 1M tokens.

Outline

Create a client, call messages.create, read content blocks. Then stream, think, cache, manage context, call tools (yours, Anthropic's, MCP), attach files, and constrain output — all on the one Messages endpoint.

Setup & core

  1. 1Install, client & auth
  2. 2First message & blocks
  3. 3Models & pricing
  4. 4System, turns & params
  5. 5Streaming
  6. 6Thinking & effort

Cost, context & tools

  1. 7Prompt caching
  2. 8Batches & token counting
  3. 9Context management
  4. 10Tool use & Tool Runner
  5. 11Anthropic client tools
  6. 12Server tools
  7. 13MCP connector & Skills

Files, output & ecosystem

  1. 14Files, documents & citations
  2. 15Structured output & vision
  3. 16Errors & robustness
  4. 17The Claude ecosystem

Setup & Core Requests

Install the SDK, call the endpoint, pick a model, then stream & think.

1Install, client & authanthropic SDK
2First message & blocksmessages.create
3Models & pricingpick a string
4System, turns & paramsshape the request
5Streamingfor long output
6Thinking & effortreasoning depth

Cost & Context

Cache prefixes, batch offline work, and keep long agent runs under the window.

7Prompt cachingup to ~90% off
8Batches & token countingoffline & estimates
9Context managementlong agent runs

Tools

Your functions, Anthropic-schema client tools, server tools, and MCP.

10Tool use & Tool Runnerfunction calling
11Anthropic client toolsmemory · bash · editor
12Server toolsAnthropic-hosted
13MCP connector & Skillsconnect & extend

Files, Output & Ecosystem

Attach documents, constrain output, handle errors, and see the wider platform.

14Files, documents & citationsFiles API
15Structured output & visiontyped JSON
16Errors & robustnesshandle failure
17The Claude ecosystembeyond the API

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-5 default · model IDs are completeno date suffix
thinking adaptive + effortbudget_tokens removed on new models
cache_control ephemeralprefix match; check cache_read_input_tokens
batches = 50% · count_tokenskey results by custom_id
tools + tool_runner (@beta_tool)strict:true; json.loads inputs
memory_20250818 · bash · text_editor · computerclient tools you run
web_search_20260209 · code_execution_20260120server tools (dated type!)
mcp_servers=[...] · client.skillsconnect & extend
files.upload -> document + citationsgrounded answers
messages.parse(output_format=)typed structured output
context compaction (compact_20260112)long agent runs