Claude API · Tutorial

Anthropic Claude API Tutorial Step by Step (Complete 2026 Guide)

PL
Prashant LalwaniApril 18, 2026 · 16 min read
APIPython
claude_api_example.py 1import anthropic 2 3client = anthropic.Anthropic( 4 api_key="your-api-key" 5) 6 7message = client.messages.create( 8 model="claude-sonnet-4-20250514" 9 max_tokens=1024 10 messages=[ 11 {{"role": "user", "content": "Hello"}} 12 ] 13) 14print(message.content) API KEY sk-ant-api03-... console.anthropic.com MODEL claude-sonnet-4 Recommended RESPONSE message.content ✓ Working! CLAUDE API TUTORIAL

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

  1. Go to console.anthropic.com
  2. Create an account or sign in
  3. Click API Keys in the left menu
  4. Click Create Key — give it a name like "my-first-key"
  5. Copy the key immediately — it starts with sk-ant-api03-
  6. 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)

ModelInput (per 1M tokens)Output (per 1M tokens)Best For
claude-haiku-3-5$0.80$4.00High-volume, simple tasks
claude-sonnet-4$3.00$15.00Most applications ✓ Recommended
claude-opus-4$15.00$75.00Complex 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

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.