Anthropic Claude API Tutorial: Complete 2026 Step-by-Step 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.
Whether you're building a customer support bot, an AI writing tool, or integrating Claude into your existing application, this guide covers everything you need. If you're new to prompt engineering, check out our guide on how to write better AI prompts before diving into the API.
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!
If you want to explore different prompting techniques with the API, our chain-of-thought prompting guide shows how to dramatically improve accuracy on complex tasks.
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. Understanding the difference between system and user prompts is critical for building reliable applications.
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)}")
For production applications, consider using the OpenAI Assistants API as an alternative, which handles conversation state management automatically.
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.
For a detailed comparison of Claude models and when to use each, read our Claude Sonnet vs Opus comparison.
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
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.
Final Thoughts
The Claude API is one of the most powerful and developer-friendly AI APIs available in 2026. With its 200K context window, excellent instruction following, and competitive pricing, it's an excellent choice for building AI-powered applications.
Start with the simple examples in this tutorial, then gradually add complexity as you understand the API better. Remember to always use environment variables for your API key, set billing limits, and test thoroughly before deploying to production.
For advanced techniques like few-shot prompting and role-based system prompts, check out our few-shot prompting examples and best prompts for Claude AI guides.