Claude API Python Quickstart

Working Python code to call the Claude API in 2026. Install the Anthropic SDK, set your API key, and send your first message in under 10 lines.

💥 50p impulse-buy: Power Prompts PDF (first 10 buyers) 30 battle-tested Claude Code prompts · 8-page PDF · paste into CLAUDE.md and never re-type a prompt again · 50p impulse-buy, no commitment

The Anthropic Python SDK makes calling the Claude API straightforward. This guide shows the minimal working example and explains each parameter.

Installation

pip install anthropic

Minimal example

import anthropic

client = anthropic.Anthropic()  # reads ANTHROPIC_API_KEY from env

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Explain prompt caching in one paragraph."}
    ]
)

print(message.content[0].text)

Key parameters

ParameterTypeNotes
modelstringModel ID. Use claude-sonnet-4-6 for most tasks.
max_tokensintMaximum output tokens. Billed even if unused up to model max (200K). Set conservatively.
messageslistAlternating user/assistant turns. First message must be role: "user".
systemstringOptional system prompt. Separate from messages.
temperaturefloat 0–1Default 1. Lower = more deterministic. Use 0 for structured output.

With system prompt

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=512,
    system="You are a concise technical writer. Answer in bullet points.",
    messages=[
        {"role": "user", "content": "What is prompt caching?"}
    ]
)
print(message.content[0].text)

Reading the response

# Full response object
print(message.model)            # "claude-sonnet-4-6"
print(message.usage.input_tokens)   # tokens billed for input
print(message.usage.output_tokens)  # tokens billed for output
print(message.stop_reason)     # "end_turn" or "max_tokens"
print(message.content[0].text) # the actual text output

To estimate the cost of each call, paste your session log into the Claude API Cost Calculator. For pricing reference across models, see the Anthropic API pricing 2026 page.

Frequently asked questions

Which Python SDK do I use for the Claude API?
Install `anthropic` from PyPI: `pip install anthropic`. The package name has been `anthropic` since 2023; there is no separate `claude` package.
Where do I get an Anthropic API key?
Sign up at console.anthropic.com, create a project, and generate an API key under Settings → API Keys. Free tier gives you rate-limited access immediately.
Which model string should I use in Python?
Use `claude-sonnet-4-6` for most workloads (best price/quality balance in 2026). Use `claude-haiku-4-5-20251001` for high-volume cheap tasks. Use `claude-opus-4-7` only for long-context reasoning where quality is critical.

Free tools

Cost Calculator → API Cookbook → Diff Summarizer → Skills Browser →

More examples

Claude API Node.js / TypeScript QuickstartClaude API Streaming in PythonClaude API Streaming in Node.js / TypeScriptClaude API Tool Use in PythonClaude API Tool Use in Node.js / TypeScriptClaude Prompt Caching in Python