DEV Community

Jonas Prenissl
Jonas Prenissl

Posted on • Originally published at ainews.q-sci.org

How to Use the Claude API (2026 Guide)

Step-by-step Claude API tutorial with code

Step-by-step tutorial • 8 min read

TL;DR

Sign up at console.anthropic.com, generate an API key, install anthropic SDK, and make your first request in under 5 minutes.

1. Get an API Key

  • Go to console.anthropic.com and create an account
  • Add billing information (you get some free credits to start)
  • Navigate to API Keys in the sidebar
  • Click Create Key and copy it — it starts with sk-ant-

2. Install the SDK

Python:
pip install anthropic
Node.js:
npm install @anthropic-ai/sdk

3. Your First Request (Python)

`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)`

4. Model Selection (2026)

ModelSpeedCostBest For
Claude Opus 4.7SlowerHigherComplex reasoning, coding
Claude Sonnet 4.6FastMediumMost tasks (default)
Claude Haiku 4.5FastestCheapestBulk, real-time

5. Key Features to Use

System Prompts

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"}]
)

Streaming

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)

Prompt Caching (Save Costs)

Cache long system prompts or context — subsequent calls are 90% cheaper. Add cache_control markers to system messages.

Tool Use

Give Claude access to functions (weather, database queries, code execution). See docs.claude.com for the full tool_use spec.

6. Common Patterns

Multi-turn Conversation

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)

File Input (PDF, Images)

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?"}
]}]
)

7. Pricing (2026)

  • Opus 4.7: $15/M input, $75/M output
  • Sonnet 4.6: $3/M input, $15/M output
  • Haiku 4.5: $0.80/M input, $4/M output

Prompt caching reduces input cost by 90% for cached tokens.

8. Common Mistakes

  • Not using system prompts — huge quality improvement
  • Ignoring streaming — better UX for long responses
  • Not caching long contexts — leaves 90% savings on the table
  • Using Opus for everything — Sonnet is enough for most tasks
  • Skipping prompt engineering — 30 minutes of prompt tuning beats model swaps

Related: ChatGPT vs Claude · Latest Anthropic News · AI Glossary


🎧 Get Daily AI News in 5 Minutes

Stay current on AI with a 5-minute daily podcast:

Original: https://ainews.q-sci.org/how-to-use-claude-api.html

Top comments (0)