How to Use the Claude API: A Complete Beginner Tutorial

How to Use the Claude API: A Complete Beginner Tutorial


Disclosure: This article may contain affiliate links. We only recommend products we believe in.

The Claude API is one of the best ways to add AI capabilities to your applications. This tutorial takes you from zero to a working AI app in about 15 minutes.

Prerequisites

  • Basic knowledge of Python or JavaScript
  • A terminal / command line
  • An Anthropic account (free to create)

Step 1: Get Your API Key

  1. Go to console.anthropic.com
  2. Create an account or sign in
  3. Handle to API Keys
  4. Click Create Key and copy it

Save your API key somewhere safe. You’ll need it in the next step.

Important: Never commit API keys to version control. Use environment variables or a .env file.

Step 2: Install the SDK

Python:

pip install anthropic

JavaScript/TypeScript:

npm install @anthropic-ai/sdk

Step 3: Your First API Call

Python:

import anthropic

client = anthropic.Anthropic(
    api_key="your-api-key-here"
)

message = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": "Explain what an API is in one paragraph, like I'm 10 years old."
        }
    ]
)

print(message.content[0].text)

JavaScript:

import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic({
    apiKey: 'your-api-key-here',
});

const message = await client.messages.create({
    model: 'claude-sonnet-4-20250514',
    max_tokens: 1024,
    messages: [
        {
            role: 'user',
            content: 'Explain what an API is in one paragraph, like I\'m 10 years old.'
        }
    ]
});

console.log(message.content[0].text);

Run it. You should see Claude’s response printed to your terminal.

Step 4: Adding System Prompts

System prompts tell Claude how to behave. This is how you customize Claude’s personality and focus:

message = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    system="You are a senior Python developer. Give concise, practical answers with code examples. Always use type hints.",
    messages=[
        {
            "role": "user",
            "content": "How do I read a CSV file and filter rows where the 'age' column is greater than 30?"
        }
    ]
)

Step 5: Multi-Turn Conversations

Claude supports conversation history. Pass previous messages to maintain context:

messages = [
    {"role": "user", "content": "What's the best Python web framework for a REST API?"},
    {"role": "assistant", "content": "For a REST API, I'd recommend FastAPI..."},
    {"role": "user", "content": "Show me a basic FastAPI setup with one endpoint."}
]

message = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=messages
)

Step 6: Streaming Responses

For better UX in real-time applications, stream the response token by token:

with client.messages.stream(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Write a haiku about coding"}]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

Pricing Quick Reference

ModelInput (per 1M tokens)Output (per 1M tokens)
Claude 3.5 Sonnet$3.00$15.00
Claude 3 Haiku$0.25$1.25
Claude 3 Opus$15.00$75.00

For most use cases, Claude 3.5 Sonnet offers the best balance of intelligence and cost. Use Haiku for simple, high-volume tasks.

Common Errors & Fixes

AuthenticationError, Your API key is invalid or missing. Double-check it.

RateLimitError, You’re sending too many requests. Add exponential backoff or reduce frequency.

InvalidRequestError, Usually means your messages array is malformed. Ensure roles alternate between user and assistant.

What to Build Next

Now that you have the basics, here are some practical project ideas:

  • Code review bot, Paste code, get suggestions
  • Documentation generator, Turn code into docs automatically
  • Chat support bot, Customer service powered by Claude
  • Content summarizer, Feed in articles, get key takeaways

Frequently Asked Questions

What represents a ‘Token’ in Claude?

A token is essentially a piece of a word. In the Claude models, a token typically corresponds to about 3 to 4 English characters. This means 100 tokens are roughly equivalent to 75 words. This metric is critical to understand because Anthropic bills its API usage based exclusively on the token counts of your input (prompts) and output (generated text).

How does Tool Calling (Function Calling) work with the API?

The Claude API supports “Tool Use” (similar to OpenAI’s function calling). You can pass a JSON schema describing external tools (like a weather API, a database querying function, or a calculator to build a custom t-test app) alongside your prompt. Claude will intelligently decide if it needs to use a tool to answer the user’s question, halt execution, ask you (the local application) to run the tool, and then incorporate the tool’s result back into its final response.

Why do I keep getting RateLimitErrors despite passing the hard-coded limits?

If you are running multi-threaded scripts or loops, Anthropic enforces limits on both Requests Per Minute (RPM) and Tokens Per Minute (TPM). Even if you only send 5 requests, if your context window pushes massive megabytes of data, you’ll hit the TPM cap instantly. To avoid this, implement the tenacity library in Python to add automatic exponential back-offs and retries.

Are Claude API requests logged or used for training?

No. Anthropic’s enterprise and general API Terms of Service explicitly protect developers: any data processed via the Claude API is not used to train Anthropic’s foundational models. Also, prompts and completions are only retained for a strict 30-day period for trust and safety purposes before being securely deleted.


This is part of our API tutorial series. Want us to cover a specific API next? Let us know.

We evaluate software and tech tools independently. To learn more about our editorial standards, read our Editorial Policy.