How to Use Claude 3.5 Sonnet API for Beginners (Complete Guide)
Step-by-step guide to set up Claude 3.5 Sonnet API with Node.js, Python, and cURL. Includes examples, pricing, and migrating from GPT-4.

Claude 3.5 Sonnet is Anthropic's fastest model (March 2026). If you're coming from OpenAI's GPT-4, Claude offers:
- 50% cheaper than GPT-4 Turbo ($3/$15 vs $0.03/$0.06 per 1K tokens input/output)
- 200K context window (reads 150 pages of text for $0.75)
- Better reasoning (ranked #1 on reasoning benchmarks, Feb 2026)
- Faster (50–100ms latency on average)
But the API setup intimidates beginners. Where do you get the API key? How do you authenticate? Which SDK? This guide walks through every step — you'll have Claude running in 5 minutes.
What is Claude API?
Claude API lets you send text prompts to Claude's model and get responses programmatically. You pay per token (like GPT-4 API), but no monthly subscription.
When to use Claude API:
- Building chatbots (customer support, lead qualification)
- Batch processing documents (summarization, extraction)
- Automating content creation (blog drafts, email responses)
- Fine-tuning workflows (extraction + classification chains)
When NOT to use Claude API:
- One-off usage (use claude.ai web interface instead — free)
- Strict latency requirement <50ms (use cached_responses)
- Custom vision models (use GPT-4 Vision instead)
Step 1: Get an API Key
1.1 Create Anthropic Account
Go to console.anthropic.com and sign up (email + password).
1.2 Create API Key
- Click "API Keys" in the left sidebar
- Click "Create Key"
- Name it "My First Key" (or anything)
- Copy the key — save it somewhere safe (you won't see it again)
Never share your key publicly or commit it to Git.
1.3 Set Environment Variable
macOS/Linux:
export ANTHROPIC_API_KEY="sk-ant-..."
Windows (PowerShell):
$env:ANTHROPIC_API_KEY = "sk-ant-..."
Windows (Command Prompt):
set ANTHROPIC_API_KEY=sk-ant-...
Verify:
echo $ANTHROPIC_API_KEY # macOS/Linux
echo %ANTHROPIC_API_KEY% # Windows
Should print your key.
Step 2: Install SDK
Claude has official SDKs for Node.js, Python, and Go. We'll cover Python (most popular for beginners).
Python
pip install anthropic
Node.js
npm install @anthropic-ai/sdk
Go
go get github.com/anthropics/anthropic-sdk-go
Step 3: Make Your First API Call
Python
Create a file called claude_hello.py:
import anthropic
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=[
{"role": "user", "content": "Explain quantum computing in one sentence."}
],
)
print(message.content[0].text)
Run:
python claude_hello.py
Output:
Quantum computing uses quantum bits (qubits) that exploit superposition and entanglement to solve certain problems exponentially faster than classical computers.
Done! You've called Claude API.
Node.js
Create claude_hello.js:
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
});
async function main() {
const message = await client.messages.create({
model: "claude-3-5-sonnet-20241022",
max_tokens: 1024,
messages: [
{
role: "user",
content: "Explain quantum computing in one sentence.",
},
],
});
console.log(message.content[0].text);
}
main();
Run:
node claude_hello.js
cURL
curl https://api.anthropic.com/v1/messages \
-H "x-api-key: sk-ant-..." \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-3-5-sonnet-20241022",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "Explain quantum computing in one sentence."
}
]
}'
Step 4: Understand Response Format
Claude always returns a response object:
{
"id": "msg-abc123",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "Quantum computing uses quantum bits..."
}
],
"model": "claude-3-5-sonnet-20241022",
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 15,
"output_tokens": 25
}
}
Key fields:
content[0].text— The actual responseusage.input_tokens— Cost:input_tokens * $3 / 1Musage.output_tokens— Cost:output_tokens * $15 / 1Mstop_reason— Why it stopped (end_turn,max_tokens, etc.)
Step 5: Build a Multi-Turn Conversation
Claude remembers conversation history. Build a chatbot by passing message history:
import anthropic
client = anthropic.Anthropic()
messages = []
def chat(user_input):
messages.append({"role": "user", "content": user_input})
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
system="You are a helpful assistant.",
messages=messages,
)
assistant_message = response.content[0].text
messages.append({"role": "assistant", "content": assistant_message})
return assistant_message
# Conversation
print(chat("What is Python?"))
print(chat("Give me 3 use cases."))
print(chat("How do I learn Python?"))
Output:
Python is a high-level programming language...
3 use cases: (1) Web development with Django... (2) Data science with pandas... (3) Automation with scripts...
Here's how to learn Python: (1) Start with basics... (2) Build projects... (3) Join communities...
Notice Claude remembers the previous questions.
Step 6: Add System Prompts
System prompts set the AI's behavior. Example: customer support bot.
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
system="""You are a friendly customer support agent for TechCorp Inc.
- Be helpful and professional
- Always offer solutions
- If unsure, say 'Let me find that for you' (don't make up info)
- End with 'Is there anything else I can help with?'
""",
messages=[
{"role": "user", "content": "My app keeps crashing. Help!"}
],
)
print(response.content[0].text)

Step 7: Migrating from GPT-4 to Claude
If you're switching from OpenAI:
| Feature | GPT-4 | Claude 3.5 Sonnet |
|---|---|---|
| Cost (input) | $0.03 per 1K | $3 per 1M (cheaper) |
| Cost (output) | $0.06 per 1K | $15 per 1M (cheaper) |
| Context | 128K tokens | 200K tokens |
| Speed | ~200ms | ~50ms faster |
| Reasoning | Good | Better (ranked #1) |
| Vision | Yes | Yes |
| Function calling | Yes (tools) | Yes (tools) |
Migration checklist:
# Old (OpenAI)
from openai import OpenAI
client = OpenAI(api_key="sk-...")
response = client.chat.completions.create(
model="gpt-4",
messages=[...]
)
# New (Claude)
from anthropic import Anthropic
client = Anthropic(api_key="sk-ant-...")
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
messages=[...]
)
# Extract text the same way
print(response.content[0].text)
Key differences:
messages.create()instead ofchat.completions.create()- No
temperature(Claude uses default randomness — usually better) systemparameter instead of role-based system messages- Response structure slightly different but same
content[0].textpattern

Step 8: Pricing & Cost Optimization
Claude 3.5 Sonnet Pricing (March 2026)
- Input: $3 per 1M tokens
- Output: $15 per 1M tokens
- No monthly minimum (pay as you go)
Example Costs
- Summarize a 10,000-word article: ~$0.03 (input) + $0.01 (output) = ~$0.04
- Customer support conversation (100 messages): ~$0.50–1.00
- Batch process 1,000 documents: ~$30–50
Cost Optimization Tips
1. Use Caching
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
system=[
{
"type": "text",
"text": "You are a helpful assistant."
},
{
"type": "text",
"text": "Large context (e.g., 100-page document)",
"cache_control": {"type": "ephemeral"}
}
],
messages=[...],
)
Saves 90% on cached tokens.
2. Batch API (for non-urgent work) Instead of calling Claude 1,000 times instantly (= 1,000 × API latency), submit all 1,000 requests at once:
# Batch 1,000 requests, get results in 1 minute
# Saves 10% on input/output tokens
3. Reuse System Prompts If you're calling Claude 100 times with the same system prompt, cache it (see #1).
Troubleshooting
Error: "Invalid API Key"
Fix: Verify your key is set in environment:
echo $ANTHROPIC_API_KEY
If empty, re-export it.
Error: "Model not found"
Fix: Use correct model name: claude-3-5-sonnet-20241022 (not claude-3.5 or sonnet)
Error: "Rate limited"
Fix: You're making too many requests per second. Add a delay:
import time
time.sleep(1) # Wait 1 second between calls
Or upgrade your plan.
Error: "max_tokens too high"
Fix: Sonnet max is 4,096 output tokens. Use max_tokens=4096 (not higher).
Advanced: Building a Chatbot with CustomGPT
If you want to build a custom chatbot trained on your own data (without coding), use CustomGPT. It integrates with Claude API but handles authentication for you.
Steps:
- Upload your documents (PDF, website, docs)
- CustomGPT trains Claude on your data
- Get a chat widget + API key
- Embed in your website (no code needed)
Example: Customer support bot trained on your FAQ → auto-answers 80% of tickets.
CustomGPT link: customgpt.ai (affiliate)
Key Takeaways
✓ Claude 3.5 Sonnet is 50% cheaper than GPT-4 and faster
✓ Get API key from console.anthropic.com (takes 2 minutes)
✓ Install SDK: pip install anthropic
✓ First API call in 5 lines of code
✓ Multi-turn conversations supported (pass message history)
✓ Migrating from GPT-4 is straightforward (same patterns)
✓ Cost optimization: use caching, batch API, reuse system prompts
✓ For no-code chatbots, use CustomGPT instead
Related Guides

Alex the Engineer
•Founder & AI ArchitectSenior software engineer turned AI Agency owner. I build massive, scalable AI workflows and share the exact blueprints, financial models, and code I use to generate automated revenue in 2026.
Related Articles

How to Install ComfyUI for Flux.1 Image Generation (Complete Guide)
Step-by-step guide to install ComfyUI with Flux.1 for unlimited AI image generation on Windows, Mac, and Linux. No API costs.

Grok AI vs ChatGPT 2026: What Beginners Need to Know
Grok is X's AI chatbot powered by xAI. Learn how it compares to ChatGPT, its strengths, limitations, and whether it's worth using as a beginner or side hustler.