Step-by-step tutorial โข 8 min read
Sign up at console.anthropic.com, generate an API key, install anthropic SDK, and make your first request in under 5 minutes.
sk-ant-Python:
pip install anthropic
Node.js:
npm install @anthropic-ai/sdk
import anthropic
client = anthropic.Anthropic(api_key="sk-ant-...")
response = client.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
messages=[
{"role": "user", "content": "Explain quantum computing in 3 sentences."}
]
)
print(response.content[0].text)
| Model | Speed | Cost | Best For |
|---|---|---|---|
| Claude Opus 4.7 | Slower | Higher | Complex reasoning, coding |
| Claude Sonnet 4.6 | Fast | Medium | Most tasks (default) |
| Claude Haiku 4.5 | Fastest | Cheapest | Bulk, real-time |
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
system="You are a helpful coding assistant. Respond only with code.",
messages=[{"role": "user", "content": "Reverse a linked list in Python"}]
)
with client.messages.stream(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[{"role": "user", "content": "Write a poem"}]
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
Cache long system prompts or context โ subsequent calls are 90% cheaper. Add cache_control markers to system messages.
Give Claude access to functions (weather, database queries, code execution). See docs.claude.com for the full tool_use spec.
messages = [
{"role": "user", "content": "Hi"},
{"role": "assistant", "content": "Hello! How can I help?"},
{"role": "user", "content": "Explain recursion"}
]
response = client.messages.create(model="claude-sonnet-4-6", max_tokens=1024, messages=messages)
import base64
image_data = base64.b64encode(open("chart.png", "rb").read()).decode()
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[{"role": "user", "content": [
{"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": image_data}},
{"type": "text", "text": "What does this chart show?"}
]}]
)
Prompt caching reduces input cost by 90% for cached tokens.
Related: ChatGPT vs Claude ยท Latest Anthropic News ยท AI Glossary