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.
All models share the same API — just change the model parameter.
| Model | Best for | Input /1M | Output /1M | Context |
|---|---|---|---|---|
| Claude 2.0 | High-volume, simple tasks, chatbots | — | — | — |
| Claude Sonnet 4 | General use, production, balanced cost/quality | $3.00 | $15.00 | 200K tokens |
| Claude Opus 4 | Complex tasks, deep analysis, hard coding problems | $15.00 | $75.00 | 200K tokens |
Prices in USD per 1M tokens.
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.
Python
pip install anthropicNode.js / TypeScript
npm install @anthropic-ai/sdkexport ANTHROPIC_API_KEY=“sk-ant-...”Or add it to .env and use python-dotenv / dotenv.
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)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);
}
}# "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?"}
]
)# "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!"}]
}'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.
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.
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.
Yes, natively. Claude Sonnet and Opus perform excellently in multiple languages — natural text, no major grammatical errors, and solid cultural context understanding.
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.
Yes. Initial tier: 50 requests/minute, 40,000 tokens/minute. Limits scale with historical spend. The official SDK handles automatic retry on 429 errors.
Yes. The API works globally with no geographic restrictions. Payment is in USD via international credit card.