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 Opus 4 | Complex tasks, deep analysis, hard coding problems | $15 | $75 | 200K |
| Claude Sonnet 4.5 | General use, production, balanced cost/quality | $3 | $15 | 200K |
| Claude Haiku 4.5 | High-volume, simple tasks, chatbots | $0.80 | $4 | 200K |
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")
message = client.messages.create(
model="claude-sonnet-4-5-20251001",
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 });
const stream = client.messages.stream({
model: "claude-sonnet-4-5-20251001",
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);
}
}message = client.messages.create(
model="claude-sonnet-4-5-20251001",
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?"}
]
)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-4-5-20251001",
"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 4.5
~$0.54/day
Data extraction (10k docs/month)
2,000 input tokens · 500 output tokens · Haiku 4.5
~$0.02/day
Legal document analysis (100 docs/day)
8,000 input tokens · 2,000 output tokens · Opus 4
~$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.
Claude Sonnet 4.5 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.
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.