Tool cheatsheet
Claude API Cheatsheet
Call Claude from Python, TypeScript or curl: messages, streaming, thinking, tools, structured output, caching, batches and error handling.
Setup and first request
- Install the Python SDK — Install the official Anthropic SDK for Python.
- Install the TypeScript SDK — Install the official SDK for TypeScript and JavaScript.
- Set your API key — The SDKs read ANTHROPIC_API_KEY from the environment. Use your own key and keep it out of source control.
- Send your first message (Python) — Create a client and send one message. The response content is a list of blocks, so check each block's type.
- Send your first message (TypeScript) — The same request with the TypeScript SDK.
- Send your first message (curl) — The raw HTTP request behind the SDKs. Parse responses with jq rather than grep.
- Pick a model — Model IDs for the current generation. Check the models overview for the latest list, context windows and prices.
Shaping requests
- Add a system prompt — Set Claude's role and rules with the top-level system parameter.
- Continue a conversation — The API is stateless: send the whole history on every request, alternating user and assistant turns.
- Ask about an image — Pass an image by URL, or as base64 with media_type, in the user message before the question.
- Control how hard Claude thinks — Use adaptive thinking and set output_config.effort (low, medium, high, xhigh or max) to trade depth against cost and speed.
- Read thinking and text blocks — Responses can mix thinking and text blocks. Branch on each block's type.
- Check why generation stopped — Check stop_reason before using the content: end_turn, max_tokens, tool_use, pause_turn or refusal.
Streaming
- Stream text (Python) — Print text as it is generated. Use streaming for long outputs and high max_tokens, which avoids request timeouts.
- Stream and get the full message — Stream for the timeout benefits, then read the complete message at the end.
- Stream asynchronously — Use the async client with async with and async for.
- Stream in TypeScript — Subscribe to text events, then await the final message.
Tools and structured output
- Let the SDK run the tool loop — Decorate Python functions with @beta_tool and the tool runner calls them and feeds results back until Claude is done. The docstring describes the tool to Claude.
- Define a tool by hand — A tool has a name, a description and a JSON Schema for its input. Claude replies with a tool_use block when it wants to call it.
- Return a tool result — Run the tool, then send its output back in a user message whose tool_use_id matches. Return all results for parallel calls in one message.
- Guarantee valid tool input — Set strict: true on a tool so its input always matches the schema. The schema needs additionalProperties: false and required.
- Get a validated Python object back — Pass a Pydantic class as output_format and read the parsed object. The response is guaranteed to match the schema.
- Constrain output with a JSON Schema — Use output_config.format when you want raw JSON that follows your schema.
Caching, batches and tokens
- Cache a large prompt — Add top-level cache_control to cache the last cacheable block. Repeated requests with the same prefix are much cheaper.
- Check that caching works — If cache_read_input_tokens stays zero across repeated requests, something in the prefix changes every time, such as a timestamp.
- Count tokens before sending — Estimate the input size and cost of a request without running it.
- Submit a batch — Process many requests asynchronously at half the price. Each request needs a custom_id.
- Wait for a batch and read results — Poll until processing ends, then stream the results. Results arrive in any order, so match them by custom_id.
Errors and configuration
- Handle API errors — Catch the most specific exception first. The SDK already retries rate limits and server errors.
- Set a timeout and retries for one call — Override client settings for a single request without changing the client.
- Log the request ID — Include the request ID when reporting a problem to Anthropic.
- Turn on SDK logging — Print the SDK's requests and responses while debugging.
Related cheatsheets
- Ansible 2 cheatsheet — Run ad-hoc commands and playbooks, manage inventories, roles, collections and secrets with Ansible.
- Argo CD 3 cheatsheet — Install Argo CD, define Applications declaratively, and manage sync, diffs, history and rollbacks with the argocd CLI.
- AWS CLI 2 cheatsheet — Configure credentials and manage S3, EC2, Lambda, logs, containers and secrets with the AWS CLI v2.
- Azure CLI 2 cheatsheet — Sign in and manage resource groups, VMs, web apps, storage, Kubernetes, registries and secrets with the az command.
- Bash and Linux 5 cheatsheet — Everyday shell and Linux commands for files, searching text, permissions, processes and servers.
- Claude Code cheatsheet — Run Claude Code in the terminal: starting sessions, slash commands, permissions, CLAUDE.md, hooks, MCP servers and headless use.