Tutorials10 min read

Claude API Tutorial: Complete Beginner's Guide to Building with Anthropic (2026)

Learn how to use the Claude API from scratch — get your API key, install the Python SDK, make your first call, and build real apps. Pricing, code examples, and use cases included.

How to Use the Claude API: A Complete Beginner's Guide (2026)

You've heard that Claude is one of the best large language models for reasoning, coding, and long-document tasks. Now you want to build something with it — but the API documentation feels like it assumes you already know what you're doing.

This guide doesn't. We'll walk through everything from creating your first API key to making production-grade calls, with actual Python code you can run in under 10 minutes.


What Is the Claude API (and Why Should You Use It)?

The Claude API lets you integrate Anthropic's Claude models directly into your applications, scripts, and workflows. Instead of using Claude through claude.ai, you call it programmatically — sending a message, getting a response, and building on top of that.

When the Claude API is the right tool:
  • You need to process documents, emails, or data at scale
  • You're building a chatbot, assistant, or AI-powered feature
  • You want to automate a reasoning task (summarization, classification, extraction)
  • You're building an agent that takes actions autonomously

What makes Claude's API worth using in 2026:
  • 1M token context window on all models — not a premium tier, just the default
  • Constitutional AI foundation — reliable, predictable outputs that are safer for production
  • Transparent pricing — three model tiers, no hidden fees, prompt caching available
  • Managed Agents — a fully managed agent harness now in public beta for agentic workflows

The Claude API is a strong choice if you need high-quality reasoning and large context. OpenAI has a broader model ecosystem; Claude has the edge on context length and complex reasoning tasks. We'll compare both in detail later.


Step 1: Get Your Anthropic API Key

Everything starts here.

  • Go to console.anthropic.com — it redirects to platform.claude.com
  • Sign up with email, Google, or SSO
  • Complete your profile and set up billing (pay-as-you-go, set a $10–$25 development cap to avoid surprises)
  • In the left sidebar, click API Keys
  • Click Create Key and give it a descriptive name (e.g., my-first-app-dev)
  • Copy the key immediately — it's only shown once
  • Store your key as an environment variable. Never hardcode it in source files:

    bashexport ANTHROPIC_API_KEY=sk-ant-xxxxxxxxxxxxxxxxxxxx

    For persistent storage, add that line to your ~/.bashrc or ~/.zshrc. On Windows, use setx ANTHROPIC_API_KEY "your-key-here" or set it in your system environment variables.


    Step 2: Install the Anthropic Python SDK

    The official SDK is the fastest way to start. It handles authentication, retries, error handling, and streaming automatically.

    bashpip install anthropic

    If you're using TypeScript/Node.js:

    bashnpm install @anthropic-ai/sdk

    This guide focuses on Python, but the TypeScript SDK has near-identical syntax.


    Step 3: Make Your First API Call

    Here's the minimal working example — a complete Python script you can run right now:

    pythonimport os
    from anthropic import Anthropic
    
    client = Anthropic(
        api_key=os.environ.get("ANTHROPIC_API_KEY"),
    )
    
    message = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=1024,
        messages=[
            {
                "role": "user",
                "content": "Explain what a REST API is in 3 sentences, for a complete beginner.",
            }
        ],
    )
    
    print(message.content[0].text)

    What's happening here:
    • Anthropic() — creates the client, reads your API key from the environment
    • model — specifies which Claude model to use (we'll cover the options below)
    • max_tokens — caps the response length (1024 tokens ≈ 750 words)
    • messages — the conversation array; each item has a role (user or assistant) and content
    • message.content[0].text — extracts the text response from the response object

    Run it:

    bashpython your_script.py

    If you see a well-formed explanation of REST APIs, you're connected.


    Step 4: Understand the Model Tiers and Pricing

    Claude offers three model tiers in 2026, each optimized for different needs:

    ModelBest ForInput PriceOutput Price
    Claude Haiku 4.5High-volume, simple tasks$1/MTok$5/MTok
    Claude Sonnet 4.6General use, coding, analysis$3/MTok$15/MTok
    Claude Opus 4.6Complex reasoning, research$5/MTok$25/MTok
    MTok = million tokens. 1M tokens ≈ 750,000 words. When to use each model: Haiku — Use it when you're processing thousands of requests and the task is simple: classifying support tickets, extracting structured fields from forms, routing customer messages. Sonnet — The workhorse. Use it for chatbots, code generation, document summarization, knowledge base Q&A. Best price-to-performance ratio for most apps. Opus — Use it for tasks where quality is non-negotiable: legal document analysis, multi-step research, complex multi-file code refactoring, or any task requiring deep reasoning across long documents. Cost optimization tips:
    • Prompt caching reduces costs by ~90% for repeated context (like a system prompt or large document you send with every request)
    • Batch API gives 50% off for non-real-time processing
    • Combined: up to 95% cost reduction in optimized workflows
    • All models include the 1M token context window at standard rates — no surcharge


    Step 5: Add a System Prompt

    Most real applications need a system prompt — instructions that set Claude's role and behavior before the conversation starts.

    pythonimport os
    from anthropic import Anthropic
    
    client = Anthropic()
    
    message = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=1024,
        system="You are a senior Python developer. Give concise, production-ready code examples. Always include error handling. Avoid explanatory fluff — just working code.",
        messages=[
            {
                "role": "user",
                "content": "Write a function that reads a CSV file and returns a list of dicts.",
            }
        ],
    )
    
    print(message.content[0].text)

    The system parameter sets the context for the entire conversation. Use it to:

    • Define Claude's role (expert, assistant, teacher, analyst)
    • Set the output format (always respond in JSON, use markdown tables, etc.)
    • Constrain behavior (only answer questions about X topic)
    • Establish tone (professional, concise, beginner-friendly)

    A well-crafted system prompt is often the difference between a generic and a genuinely useful AI feature.


    Step 6: Handle Multi-Turn Conversations

    Claude's API is stateless — it doesn't remember previous messages automatically. To build a multi-turn conversation, you maintain the history yourself and pass it with each request:

    pythonimport os
    from anthropic import Anthropic
    
    client = Anthropic()
    conversation_history = []
    
    def chat(user_message: str) -> str:
        conversation_history.append({
            "role": "user",
            "content": user_message,
        })
    
        response = client.messages.create(
            model="claude-sonnet-4-6",
            max_tokens=1024,
            system="You are a helpful coding assistant.",
            messages=conversation_history,
        )
    
        assistant_message = response.content[0].text
        conversation_history.append({
            "role": "assistant",
            "content": assistant_message,
        })
    
        return assistant_message
    
    # Example conversation
    print(chat("What's the difference between a list and a tuple in Python?"))
    print(chat("When would you use one over the other?"))
    print(chat("Give me a practical example."))

    Each call appends both the user message and the assistant response to conversation_history. The full history is sent with every request — which is why Claude's 1M context window matters for long conversations.

    Production note: For web apps, store conversation history in a database (by session ID) rather than in memory, so it persists across server restarts.

    Step 7: Enable Streaming for Real-Time Responses

    For any user-facing application, streaming makes Claude feel dramatically faster — the text appears word-by-word instead of all at once after a delay.

    pythonimport os
    from anthropic import Anthropic
    
    client = Anthropic()
    
    with client.messages.stream(
        model="claude-sonnet-4-6",
        max_tokens=1024,
        messages=[
            {
                "role": "user",
                "content": "Write a haiku about building software.",
            }
        ],
    ) as stream:
        for text in stream.text_stream:
            print(text, end="", flush=True)
    
    print()  # newline after stream completes

    The stream.text_stream iterator yields text chunks as they arrive. The flush=True ensures each chunk prints immediately rather than buffering.

    For FastAPI or Flask backends, you'd use stream_with_httpx or return a StreamingResponse — the SDK documentation covers both patterns.


    Common Use Cases and Which Model to Pick

    Here's how to match Claude models to real-world tasks:

    High-volume data processing (Haiku)
    • Classify 10,000 support tickets by category and urgency
    • Extract structured fields (name, date, amount) from invoices
    • Route customer messages to the right department
    • Moderate user-generated content at scale

    Knowledge base and chat applications (Sonnet)
    • Internal Q&A chatbot over company documentation
    • Customer support assistant that drafts responses for human review
    • Code review assistant that explains what a PR does
    • Email summarizer that surfaces action items

    Complex research and analysis (Opus)
    • Analyze 100+ page contracts and identify non-standard clauses
    • Multi-step research synthesis from academic papers
    • Multi-file code refactoring with architectural awareness
    • Agentic workflows that plan and execute multi-step tasks


    Claude API vs OpenAI API: Key Differences

    Both APIs are mature and well-documented. Here's what actually matters when choosing:

    FeatureClaude APIOpenAI API
    Context window1M tokens (all models)128K tokens (GPT-4o)
    Mid-tier price (input)Sonnet: $3/MTokGPT-4o: $2.50/MTok
    Flagship price (input)Opus: $5/MTokGPT-4.1: ~$15/MTok
    Code generationExcellent (leads on infra tasks)Excellent (leads on speed)
    ReasoningConstitutional AI — predictableGeneral — flexible
    Structured outputsNatural language instructionsStrict JSON schema
    Model count3 focused tiersBroad ecosystem + legacy
    AgentsManaged Agents betaAssistants API
    Choose Claude if: You're processing long documents, need consistent/predictable behavior, or are building complex reasoning workflows. Opus is notably cheaper than GPT-4.1 while matching or beating it on complex tasks. Choose OpenAI if: You need the broadest model ecosystem, have existing OpenAI integrations, or are doing image generation alongside text.

    Many developers use both — OpenAI for its ecosystem, Claude for specific tasks where its reasoning quality or 1M context window is a decisive advantage.


    What to Build Next

    Once you've made your first API call, here's a progression that builds real skills:

  • Document Q&A script — Read a PDF, send its text as context, ask questions about it. Validates Claude's context handling.
  • CSV data analyzer — Pass rows of data, ask for patterns, anomalies, or summaries. Builds intuition for structured prompts.
  • Automated email responder — Read incoming emails, classify them, draft responses for review. Real-world value quickly.
  • Coding assistant CLI — Pipe code into Claude via stdin, get explanations or refactoring suggestions. Developer productivity tool.
  • Multi-agent workflow — Use Claude Managed Agents to build an agent that reads files, searches the web, and synthesizes a report.
  • Each step introduces a new API concept: vision, tool use, streaming, agent orchestration.


    Key Takeaways

    • Setup takes 10 minutes: Create account → generate API key → pip install anthropic → run 10 lines of Python
    • Three model tiers: Haiku (speed + volume), Sonnet (general use), Opus (complex reasoning)
    • All models include 1M context at standard pricing — no premium required
    • System prompts shape behavior — invest time here for production-grade results
    • Manage conversation state yourself — the API is stateless, you own the history
    • Streaming is table stakes for user-facing apps — it makes responses feel 10x faster
    • Cost optimization via prompt caching and batch API can cut costs by up to 95%


    Next Steps

    Ready to go deeper? Here's where to go from here:

    If you're building production apps on Claude, the Anthropic documentation is thorough and regularly updated. Start with the Messages API reference and the Prompt Engineering guide — both are worth reading end to end before you ship anything to users.

    Ready to Start Practicing?

    300+ scenario-based practice questions covering all 5 CCA domains. Detailed explanations for every answer.

    Free CCA Study Kit

    Get domain cheat sheets, anti-pattern flashcards, and weekly exam tips. No spam, unsubscribe anytime.