Getting Started with the Claude API: A Practical Guide for Developers
Learn how to build with the Claude API from scratch. This guide covers setup, Messages API basics, model selection, and key features like streaming and tool use.
This guide walks you through making your first Claude API call, understanding the Messages API structure, choosing the right model, and exploring key features like streaming, tool use, and extended thinking.
Introduction
Claude is Anthropic's family of large language models designed for a wide range of tasks—from text generation and code completion to vision and complex reasoning. Whether you're building a chatbot, an agent, or an enterprise workflow, the Claude API gives you direct access to these powerful models.
This guide will take you from zero to a working Claude integration. You'll learn how to make your first API call, understand the core request/response structure, choose the right model for your use case, and explore the features that make Claude stand out.
Prerequisites
Before you start, you'll need:
- An Anthropic account and an API key from the Developer Console
- Python 3.8+ or Node.js 18+ installed on your machine
- Basic familiarity with REST APIs and JSON
Step 1: Make Your First API Call
Let's start by sending your first message to Claude. The quickest way is using the Python SDK.
Install the SDK
pip install anthropic
Set Your API Key
Set your API key as an environment variable:
export ANTHROPIC_API_KEY="sk-ant-..."
Send a Message
Create a file called hello_claude.py:
import anthropic
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[
{"role": "user", "content": "Hello, Claude! What can you do?"}
]
)
print(message.content[0].text)
Run it:
python hello_claude.py
You should see Claude's response printed in your terminal. Congratulations—you've made your first API call!
Step 2: Understand the Messages API
The Messages API is the primary way to interact with Claude. It's designed for multi-turn conversations, system prompts, and fine-grained control.
Request Structure
A basic request has three required fields:
model: The model identifier (e.g.,claude-sonnet-4-20250514)max_tokens: The maximum number of tokens in the responsemessages: An array of message objects, each with aroleandcontent
Roles
user: Messages from the end userassistant: Claude's responses (used to provide conversation history)
System Prompts
You can set the model's behavior with a system prompt:
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
system="You are a helpful assistant that speaks like a pirate.",
messages=[
{"role": "user", "content": "What's the weather like today?"}
]
)
Multi-Turn Conversations
To continue a conversation, include the full history:
messages = [
{"role": "user", "content": "What's the capital of France?"},
{"role": "assistant", "content": "The capital of France is Paris."},
{"role": "user", "content": "What's its most famous landmark?"}
]
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=messages
)
Stop Reasons
When Claude stops generating, the response includes a stop_reason field:
"end_turn": Claude finished naturally"max_tokens": The response was cut off because it hit the token limit"stop_sequence": Claude encountered a custom stop sequence you defined"tool_use": Claude wants to call a tool (more on this later)
Step 3: Choose the Right Model
Claude offers several models optimized for different use cases:
| Model | Best For | Speed | Cost |
|---|---|---|---|
| Claude Opus 4.7 | Complex reasoning, agentic coding, research | Slower | Highest |
| Claude Sonnet 4.6 | General coding, agents, enterprise workflows | Fast | Moderate |
| Claude Haiku 4.5 | Real-time apps, classification, quick tasks | Fastest | Lowest |
claude-sonnet-4-20250514 for most use cases. It offers the best balance of intelligence, speed, and cost.
Step 4: Explore Key Features
Claude's API supports several powerful features. Here are the ones you'll use most often:
Streaming
For real-time applications, stream responses token by token:
with client.messages.stream(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[{"role": "user", "content": "Tell me a story"}]
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
Tool Use (Function Calling)
Claude can call external tools. Define tools and let Claude decide when to use them:
tools = [
{
"name": "get_weather",
"description": "Get the current weather for a city",
"input_schema": {
"type": "object",
"properties": {
"location": {"type": "string"}
},
"required": ["location"]
}
}
]
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
tools=tools,
messages=[{"role": "user", "content": "What's the weather in Tokyo?"}]
)
When Claude requests a tool call, the response will have stop_reason: "tool_use" and a content block with tool call details.
Extended Thinking
For complex reasoning tasks, enable extended thinking to let Claude "think" before responding:
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=2048,
thinking={"type": "enabled", "budget_tokens": 1024},
messages=[{"role": "user", "content": "Solve this math problem step by step..."}]
)
Structured Outputs
Request JSON-formatted responses for easier parsing:
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[{"role": "user", "content": "Extract the name, date, and amount from this invoice..."}]
)
For guaranteed structured output, use the structured_outputs feature (available in newer models).
Vision
Claude can analyze images. Pass image data as base64 or use a URL:
import base64
with open("chart.png", "rb") as f:
image_data = base64.b64encode(f.read()).decode("utf-8")
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "Describe this chart"},
{"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": image_data}}
]
}
]
)
Step 5: Build with Developer Tools
Anthropic provides several tools to accelerate development:
- Developer Console: Test prompts in the Workbench, generate prompt templates, and manage API keys
- API Reference: Full documentation for the REST API and SDKs
- Claude Cookbook: Interactive Jupyter notebooks covering PDF processing, embeddings, and more
Best Practices
- Start with a small token limit (e.g., 256) during development to save costs
- Use system prompts to set behavior rather than repeating instructions in every user message
- Implement retry logic with exponential backoff for transient errors
- Monitor token usage to avoid surprises—use the
usagefield in responses - Cache frequent prompts with prompt caching to reduce latency and cost
Next Steps
Now that you've made your first API call and understand the basics, here's what to explore next:
- Build a multi-turn chatbot with conversation history
- Create a tool-using agent that can search the web or fetch data
- Implement batch processing for large-scale tasks
- Learn about prompt caching to optimize performance
Key Takeaways
- The Claude API is accessible via a simple REST API or SDKs for Python, TypeScript, and other languages
- The Messages API supports multi-turn conversations, system prompts, and fine-grained control with stop reasons
- Choose the right model: Haiku for speed, Sonnet for balance, Opus for complex reasoning
- Key features include streaming, tool use, extended thinking, structured outputs, and vision
- Use the Developer Console and Cookbook to prototype and learn faster