Claude API Getting Started Guide for Developers
Anthropic’s Claude API gives developers access to one of the most capable large language models available today. Whether you’re building a chatbot, a coding assistant, a document analyzer, or an agentic workflow, Claude’s API is well-documented, straightforward to use, and competitively priced. This guide walks you through everything you need to get up and running.
Getting Your API Key
Before writing a single line of code, you need an Anthropic account and an API key.
- Go to console.anthropic.com and sign up or log in.
- Navigate to API Keys in the left sidebar.
- Click Create Key, give it a name, and copy it immediately — you won’t be able to see it again.
- Store it securely in an environment variable or a secrets manager. Never hardcode it in source files.
export ANTHROPIC_API_KEY="sk-ant-api03-..."
Add that line to your ~/.bashrc or ~/.zshrc to persist it across sessions.
Installing the Python SDK
Anthropic maintains an official Python SDK that wraps the REST API cleanly.
pip install anthropic
For projects, you should pin the version in your requirements.txt:
pip install anthropic==0.25.0
The SDK supports Python 3.8+ and is also available for TypeScript/Node.js via npm install @anthropic-ai/sdk.
Your First API Call
With the SDK installed, here’s the minimal code to send a message and get a response:
import anthropic
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from env
message = client.messages.create(
model="claude-opus-4-5",
max_tokens=1024,
messages=[
{"role": "user", "content": "Explain what a transformer model is in 3 sentences."}
]
)
print(message.content[0].text)
The messages.create() call is the core of the API. It returns a Message object with a content list, where each item has a type (usually "text") and text field.
Understanding the Messages API
Claude uses a conversation-style messages API where you pass a list of alternating user and assistant turns. This lets you build multi-turn conversations easily.
response = client.messages.create(
model="claude-opus-4-5",
max_tokens=2048,
messages=[
{"role": "user", "content": "What is the capital of France?"},
{"role": "assistant", "content": "The capital of France is Paris."},
{"role": "user", "content": "What's the population of that city?"}
]
)
Key Parameters
| Parameter | Description | Example |
|---|---|---|
model | Which Claude model to use | claude-opus-4-5, claude-haiku-3-5 |
max_tokens | Maximum tokens in the response | 1024 |
messages | List of conversation turns | [{"role": "user", "content": "..."}] |
temperature | Randomness (0.0–1.0) | 0.7 |
top_p | Nucleus sampling | 0.9 |
Adding System Prompts
A system prompt sets persistent instructions that apply to the entire conversation. It’s separate from the messages list and is the right place to define Claude’s persona, output format, or constraints.
response = client.messages.create(
model="claude-opus-4-5",
max_tokens=1024,
system="You are a senior Python developer. Always provide type hints in your code examples. Be concise.",
messages=[
{"role": "user", "content": "Write a function to parse a CSV file."}
]
)
System prompts are billed as input tokens, so keep them focused. A well-crafted system prompt dramatically improves output quality and consistency across calls.
Streaming Responses
For long outputs or chat interfaces, streaming makes the experience feel faster:
with client.messages.stream(
model="claude-opus-4-5",
max_tokens=1024,
messages=[{"role": "user", "content": "Write a haiku about Python."}]
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
Tool Use (Function Calling)
Claude supports tool use, allowing it to call functions you define. This is essential for building agents that can fetch data, run calculations, or interact with external systems.
tools = [
{
"name": "get_weather",
"description": "Get current weather for a location",
"input_schema": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "City name"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["location"]
}
}
]
response = client.messages.create(
model="claude-opus-4-5",
max_tokens=1024,
tools=tools,
messages=[{"role": "user", "content": "What's the weather in Tokyo?"}]
)
# Check if Claude wants to use a tool
if response.stop_reason == "tool_use":
tool_call = next(b for b in response.content if b.type == "tool_use")
print(f"Tool: {tool_call.name}, Input: {tool_call.input}")
After executing the tool, you pass the result back as a tool_result message to continue the conversation.
Pricing Tiers (as of early 2026)
Anthropic offers several models at different price points:
| Model | Input (per 1M tokens) | Output (per 1M tokens) | Best For |
|---|---|---|---|
| Claude Opus 4.5 | ~$15 | ~$75 | Complex reasoning, agents |
| Claude Sonnet 4 | ~$3 | ~$15 | Balanced tasks |
| Claude Haiku 3.5 | ~$0.80 | ~$4 | Fast, high-volume tasks |
Prompt caching is available for repeated system prompts or context, reducing costs significantly for applications that reuse large contexts. Cached tokens cost about 10% of normal input pricing.
Rate Limits and Best Practices
- Start with Tier 1 (default for new accounts) — it covers most prototyping workloads.
- Use exponential backoff when you hit
429rate limit errors. - Set
max_tokensconservatively — you’re charged for actual tokens generated, but setting it too low cuts off responses. - Use Haiku for classification, routing, or simple extraction tasks to cut costs by 10–20x versus Opus.
import time
import anthropic
def call_with_retry(client, **kwargs, max_retries=3):
for attempt in range(max_retries):
try:
return client.messages.create(**kwargs)
except anthropic.RateLimitError:
wait = 2 ** attempt
print(f"Rate limited. Waiting {wait}s...")
time.sleep(wait)
raise Exception("Max retries exceeded")
Next Steps
Once you’re comfortable with basic API calls, explore:
- Batches API for offline, high-volume processing at 50% cost reduction
- Files API for uploading PDFs and documents for analysis
- Computer use for browser and desktop automation
- The Anthropic Cookbook on GitHub for practical examples
The Claude API is one of the most developer-friendly AI APIs available, with excellent documentation and an active community. Start small, measure your token usage, and scale from there.