🔑 Claude API Setup
npm install @anthropic-ai/sdk ANTHROPIC_API_KEY=sk-ant-...
💬 Basic Call
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic();
const msg = await client.messages.create({
model: "claude-sonnet-4-20250514",
max_tokens: 1024,
system: "You are a helpful assistant.",
messages: [
{ role: "user", content: "Hello" }
]
});
console.log(msg.content[0].text);
⭐ Prompt Caching (90% off!)
Long system prompt বা document — repeat call-এ massive cost savings।
messages: [{
role: "user",
content: [
{
type: "text",
text: "[Huge document, 10K tokens]",
cache_control: { type: "ephemeral" }
},
{
type: "text",
text: "Question: ..."
}
]
}]
// First call: full price (write to cache)
// Subsequent (within 5 min): 90% off cached portion!
🛠️ Tool Use
tools: [{
name: "get_weather",
description: "Get current weather",
input_schema: {
type: "object",
properties: { location: { type: "string" } },
required: ["location"]
}
}]
🖥️ Computer Use ⭐
Claude OS/browser control করতে পারে — screenshot দেখে mouse/keyboard action।
tools: [{ type: "computer_20241022", name: "computer",
display_width_px: 1024, display_height_px: 768 }]
// Screenshot → click(x,y), type, scroll
📁 Files API
PDF, image upload করে context-এ use।
🎯 Claude vs OpenAI
| Feature | Claude | OpenAI |
|---|---|---|
| Context | 200K | 128K |
| Caching | 90% off | 50% off (batch) |
| Vision | Yes | Yes |
| Computer Use | Yes ⭐ | Operator |
| Code | Best-in-class | Excellent |
| Image gen | No | DALL-E |
| Voice | No native | TTS, Whisper |
💡 When Claude Better
- Long document (200K context)
- Complex reasoning
- Coding task
- Repeated system prompt (caching)
- Nuanced writing
- Following complex instruction
🐍 Python Example
import anthropic
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello"}]
)
print(message.content[0].text)