Technical Guide · Anthropic API

How to Use the Claude APIComplete Guide 2026

From first request to production: authentication, model selection, streaming, cost control, and best practices for integrating Claude in Python, Node.js, or any REST client.

Available Models

All models share the same API — just change the model parameter.

ModelBest forInput /1MOutput /1MContext
Claude 2.0High-volume, simple tasks, chatbots———
Claude Sonnet 4General use, production, balanced cost/quality$3.00$15.00200K tokens
Claude Opus 4Complex tasks, deep analysis, hard coding problems$15.00$75.00200K tokens

Prices in USD per 1M tokens.

Getting Started

1. Get an API key

Go to console.anthropic.com, create an account, add payment credits, and generate an API key under “API Keys”. Store the key safely — it is not shown again.

2. Install the SDK

Python

pip install anthropic

Node.js / TypeScript

npm install @anthropic-ai/sdk

3. Set the environment variable

export ANTHROPIC_API_KEY=“sk-ant-...”

Or add it to .env and use python-dotenv / dotenv.

Code Examples

First request (Python)python
import anthropic

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

# "model" below is an example — see the current slug at docs.anthropic.com/models
message = client.messages.create(
    model="claude-sonnet-5",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Explain what RAG is in 3 paragraphs."}
    ]
)

print(message.content[0].text)
Streaming response (Node.js)typescript
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });

// "model" below is an example — see the current slug at docs.anthropic.com/models
const stream = client.messages.stream({
  model: "claude-sonnet-5",
  max_tokens: 1024,
  messages: [{ role: "user", content: "List 5 AI frameworks." }],
});

for await (const chunk of stream) {
  if (chunk.type === "content_block_delta") {
    process.stdout.write(chunk.delta.text);
  }
}
System prompt with contextpython
# "model" below is an example — see the current slug at docs.anthropic.com/models
message = client.messages.create(
    model="claude-sonnet-5",
    max_tokens=2048,
    system="You are an assistant specialized in financial analysis. "
           "Always be objective and cite sources when relevant.",
    messages=[
        {"role": "user", "content": "What are the risks of investing in LLMs in 2026?"}
    ]
)
Raw REST (curl)bash
# "model" below is an example — see the current slug at docs.anthropic.com/models
curl https://api.anthropic.com/v1/messages \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{
    "model": "claude-sonnet-5",
    "max_tokens": 1024,
    "messages": [{"role": "user", "content": "Hello!"}]
  }'

Cost Estimates

Estimated values. Actual cost varies with prompt size.

Chatbot with 1,000 users/day

500 input tokens · 300 output tokens · Sonnet ($2/M input, $10/M output)

~$4.00/day

Data extraction (10k docs/month)

2,000 input tokens · 500 output tokens · Haiku (economy tier)

~$0.02/day

Legal document analysis (100 docs/day)

8,000 input tokens · 2,000 output tokens · Opus (flagship tier)

~$13.50/day

For precise calculations: cost-efficiency ranking or the API pricing comparison.

Production Best Practices

✅

Never hardcode your API key

Use environment variables (`ANTHROPIC_API_KEY`). Never commit the key to your repository — use `.env` + `.gitignore`.

✅

Always set max_tokens

Without a defined limit, responses may be truncated unpredictably. Calculate the maximum needed for your task and add a 20% margin.

✅

Use the system prompt for fixed context

Persona, language, response format, and constraints belong in `system`, not `user`. This reduces tokens and improves consistency.

✅

Implement retry with exponential backoff

The API returns 429 (rate limit) under peak load. The official SDK already includes automatic retry — use it instead of rolling your own.

⚠️

Avoid unnecessarily long contexts

Context tokens are billed. Keep only the messages relevant to the current task. For long conversation history, use summarization.

⚠️

Don't use Opus for simple tasks

Claude Opus costs 10× more than Haiku. For structured data extraction, classification, and simple Q&A, Haiku is sufficient and far more cost-effective.

Frequently Asked Questions

How do I get a Claude API key?

Go to console.anthropic.com, create an account, add payment credits, and generate an API key under "API Keys". The whole process takes under 5 minutes.

Does the Claude API handle non-English languages well?

Yes, natively. Claude Sonnet and Opus perform excellently in multiple languages — natural text, no major grammatical errors, and solid cultural context understanding.

Which Claude model should I use for production?

The Sonnet tier (balanced) is the default for most production applications: good quality, adequate speed, and manageable cost. Use Haiku for high-volume work, Opus for tasks requiring deep reasoning. See current models and pricing at /benchmark/custo-api.

Is there a rate limit on the Claude API?

Yes. Initial tier: 50 requests/minute, 40,000 tokens/minute. Limits scale with historical spend. The official SDK handles automatic retry on 429 errors.

Can I use the Claude API from anywhere in the world?

Yes. The API works globally with no geographic restrictions. Payment is in USD via international credit card.

Keep reading