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.
The Anthropic Python SDK makes calling the Claude API straightforward. This guide shows the minimal working example and explains each parameter.
pip install anthropic
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)
| Parameter | Type | Notes |
|---|---|---|
model | string | Model ID. Use claude-sonnet-4-6 for most tasks. |
max_tokens | int | Maximum output tokens. Billed even if unused up to model max (200K). Set conservatively. |
messages | list | Alternating user/assistant turns. First message must be role: "user". |
system | string | Optional system prompt. Separate from messages. |
temperature | float 0–1 | Default 1. Lower = more deterministic. Use 0 for structured output. |
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)
# 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.