Anthropic Claude API Tutorial Step by Step (Complete 2026 Guide)
The Anthropic Claude API lets you build powerful AI-powered applications — chatbots, writing assistants, data processors, and more. This step-by-step tutorial takes you from zero to your first working Claude API call in under 20 minutes.
Prerequisites: Basic Python knowledge (variables, functions, importing libraries). No prior API experience needed — this guide explains everything from scratch. You'll need a credit card to activate the API (pay-per-use, not a subscription).
Step 1: Get Your Claude API Key
- Go to console.anthropic.com
- Create an account or sign in
- Click API Keys in the left menu
- Click Create Key — give it a name like "my-first-key"
- Copy the key immediately — it starts with
sk-ant-api03- - Add billing at Settings → Billing — you only pay for what you use
Security Warning: Never put your API key directly in code you'll share or commit to GitHub. Use environment variables (shown below). A leaked key will be used by others and billed to your account.
Step 2: Set Up Your Environment
# Install the Anthropic Python SDK pip install anthropic # Set your API key as an environment variable (recommended) # On Mac/Linux: export ANTHROPIC_API_KEY="sk-ant-api03-your-key-here" # On Windows (Command Prompt): set ANTHROPIC_API_KEY=sk-ant-api03-your-key-here
Step 3: Make Your First API Call
Here is the simplest possible Claude API call in Python:
import anthropic # Create the client (reads API key from environment variable) client = anthropic.Anthropic() # Send a message to Claude message = client.messages.create( model="claude-sonnet-4-20250514", # Best model for most tasks max_tokens=1024, messages=[ {"role": "user", "content": "Hello! What can you help me with?"} ] ) # Print Claude's response print(message.content[0].text)
Run this and you'll see Claude's response printed in your terminal. That's it — you've made your first Claude API call!
Step 4: Understanding the Response Object
The API returns a message object with several useful properties:
# Full response object properties print(message.id) # Unique message ID print(message.model) # Model used print(message.stop_reason) # Why generation stopped print(message.usage) # Input/output token counts # The actual text response text = message.content[0].text print(text)
Step 5: Using System Prompts
System prompts let you define Claude's role and behaviour for your application — this is how you build specialised AI assistants:
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
system="""You are a helpful customer service agent for TechShop.
You only answer questions about our products and services.
Always be polite, professional, and concise.""",
messages=[
{"role": "user", "content": "What's your return policy?"}
]
)Step 6: Build a Multi-Turn Conversation
For chatbots, you need to send the conversation history with each request:
import anthropic client = anthropic.Anthropic() conversation_history = [] def chat(user_message): # Add user message to history conversation_history.append({ "role": "user", "content": user_message }) # Send full history to Claude response = client.messages.create( model="claude-sonnet-4-20250514", max_tokens=1024, messages=conversation_history ) reply = response.content[0].text # Add Claude's reply to history conversation_history.append({ "role": "assistant", "content": reply }) return reply # Run a simple conversation loop while True: user_input = input("You: ") if user_input.lower() == "quit": break print(f"Claude: {chat(user_input)}")
Step 7: API Pricing (So You Don't Get a Surprise Bill)
| Model | Input (per 1M tokens) | Output (per 1M tokens) | Best For |
|---|---|---|---|
| claude-haiku-3-5 | $0.80 | $4.00 | High-volume, simple tasks |
| claude-sonnet-4 | $3.00 | $15.00 | Most applications ✓ Recommended |
| claude-opus-4 | $15.00 | $75.00 | Complex reasoning tasks |
A typical API call with a 500-word prompt and 500-word response uses roughly 500 + 500 = 1,000 tokens. At Sonnet rates, that's about $0.003 — less than a fraction of a cent per call.
Common Errors and How to Fix Them
- AuthenticationError — Your API key is wrong or not set. Check the environment variable.
- RateLimitError — You're making too many requests. Add retry logic with exponential backoff.
- InvalidRequestError — Check your message format. The messages array must alternate user/assistant roles.
- OverloadedError — Anthropic's servers are busy. Retry after a few seconds.
Frequently Asked Questions
How much does the Claude API cost?
You pay per token (roughly per word). Claude Sonnet costs $3 per million input tokens and $15 per million output tokens. Most test calls cost fractions of a cent. Set a monthly billing limit in the Anthropic console to avoid unexpected charges.
Which Claude model should I use?
Start with claude-sonnet-4-20250514 — it's the best balance of capability and cost for most applications. Use Haiku for high-volume simple tasks where cost matters. Use Opus only for complex tasks that genuinely need it.
Can I use the Claude API for free?
The API itself is pay-per-use with no free tier (unlike claude.ai which has a free web interface). You need to add billing, but costs are very low for development and testing.