Claude API Basics

A quick reference for making your first Claude API call, picking a model, and reading the response.

TL;DR

  1. Install the Anthropic SDK and set your ANTHROPIC_API_KEY variable.
  2. Call client.messages.create() with a model, prompt, and max_tokens.
  3. Check stop_reason before reading content to handle responses safely.

Installation and Auth

    pip install anthropic

    Installs the Python SDK from PyPI so you can start calling Claude.

    pip install anthropic
    npm install @anthropic-ai/sdk

    Installs the TypeScript and Node SDK for JavaScript projects.

    npm install @anthropic-ai/sdk
    ANTHROPIC_API_KEY

    Stores your API key as an environment variable instead of hard-coding it.

    export ANTHROPIC_API_KEY="sk-ant-..."

Your First Request

    Anthropic()

    Creates a client that reads the API key from the environment automatically.

    import anthropic
    client = anthropic.Anthropic()
    messages.create()

    Sends a prompt to Claude with a model, max_tokens, and a messages array.

    message = client.messages.create(
        model="claude-opus-4-8",
        max_tokens=1024,
        messages=[{"role": "user", "content": "Explain closures in one paragraph."}]
    )
    system parameter

    Gives Claude a persistent role or persona for the whole conversation.

    message = client.messages.create(
        model="claude-opus-4-8",
        max_tokens=1024,
        system="You are a concise technical writer.",
        messages=[{"role": "user", "content": "Explain closures."}]
    )
    max_tokens

    Caps the response length; short answers need less, long docs need more.

    # Short answers: 256-512  |  Long docs: 4096-8192  |  Max: 128000

Reading the Response

    content[0].text

    Reads the text of Claude's reply from the response object.

    print(message.content[0].text)
    stop_reason

    Explains why generation stopped: end_turn, max_tokens, tool_use, or refusal.

    if message.stop_reason == "end_turn":
        print(message.content[0].text)
    usage

    Reports input and output token counts so you can track cost.

    print(message.usage.input_tokens)
    print(message.usage.output_tokens)
    message.model

    Confirms which model actually served the request.

    print(message.model)  # e.g. "claude-opus-4-8"

Choosing a Model

    Claude Opus 4.8

    Best for hard reasoning and agentic tasks; the default for most new integrations.

    claude-opus-4-8
    Claude Sonnet 5

    Near-Opus quality on coding and agentic work at a lower cost.

    claude-sonnet-5
    Claude Haiku 4.5

    Fastest and cheapest option for high-volume tasks like tagging or routing.

    claude-haiku-4-5
    Claude Fable 5

    Anthropic's most capable model for the hardest, longest-horizon tasks.

    claude-fable-5

Tips

  1. Pin an exact model ID like claude-opus-4-8 in production so a later model update never silently changes your app's behavior.
  2. Set max_tokens generously for long or open-ended replies, since Claude stops the moment it reaches that limit, even mid-sentence.

Warnings

  1. max_tokens is required on every request — omitting it raises a validation error before Claude even sees your prompt.
  2. Never hard-code an API key in source control; load it from an environment variable or a secrets manager instead.

In Practice

FAQ