LIVE UPDATE API Development

Anthropic Claude API Tutorial: Complete 2026 Step-by-Step Guide

20min
Setup Time
$0.003
Avg Call Cost
200K
Context Window
Python
Primary SDK
Prashant Lalwani
June 14, 2026 ยท 16 min read
Updated Today

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

  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!

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

Frequently Asked Questions

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.
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.
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.
AuthenticationError means your API key is wrong or not set. Check that you've set the ANTHROPIC_API_KEY environment variable correctly. Never hardcode the key in your source code โ€” use environment variables or a .env file.

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.