Getting Started with the Claude API: A Practical Guide for Developers
Learn how to build with the Claude API from scratch. Covers setup, Messages API, model selection, and key features like extended thinking 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 advanced features like tool use and extended thinking.
Getting Started with the Claude API: A Practical Guide for Developers
Claude, Anthropic's family of large language models, offers developers two primary ways to integrate AI capabilities into their applications: the Messages API for direct model access and Claude Managed Agents for pre-built, configurable agent harnesses. This guide focuses on the Messages API—the most flexible and widely used approach for custom integrations.
Whether you're building a chatbot, automating code generation, or creating a document analysis tool, this guide will take you from zero to a working Claude integration.
Understanding Your Options: Messages API vs. Managed Agents
Before diving into code, it's important to choose the right integration path:
| Feature | Messages API | Claude Managed Agents |
|---|---|---|
| Best for | Custom agent loops and fine-grained control | Long-running tasks and asynchronous work |
| Control | Full control over prompts, tools, and logic | Pre-built agent harness with configurable settings |
| Infrastructure | You manage your own infrastructure | Runs in Anthropic's managed infrastructure |
Step 1: Make Your First API Call
Prerequisites
- An Anthropic account and API key
- Python 3.7+ or Node.js 16+
- Basic familiarity with REST APIs
Python Quickstart
Install the Anthropic SDK:
pip install anthropic
Create a file claude_test.py:
import anthropic
client = anthropic.Anthropic(
api_key="your-api-key-here"
)
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 claude_test.py
TypeScript Quickstart
Install the SDK:
npm install @anthropic-ai/sdk
Create claude_test.ts:
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic({
apiKey: 'your-api-key-here',
});
async function main() {
const message = await client.messages.create({
model: 'claude-sonnet-4-20250514',
max_tokens: 1024,
messages: [
{ role: 'user', content: 'Hello, Claude! What can you do?' }
],
});
console.log(message.content[0].text);
}
main();
Step 2: Understand the Messages API Structure
The Messages API uses a simple request/response format. Here's what you need to know:
Request Structure
message = client.messages.create(
model="claude-sonnet-4-20250514", # Required: model identifier
max_tokens=1024, # Required: maximum output length
system="You are a helpful assistant.", # Optional: system prompt
messages=[
{"role": "user", "content": "What is the capital of France?"},
{"role": "assistant", "content": "The capital of France is Paris."},
{"role": "user", "content": "Tell me more about its history."}
]
)
Key Parameters
model: Choose from Claude Opus, Sonnet, or Haikumax_tokens: Maximum number of tokens in the responsesystem: System prompt to set behavior and contextmessages: Array of message objects withrole(user/assistant) andcontent
Response Structure
The response contains:
content: Array of content blocks (text, tool_use, etc.)stop_reason: Why generation stopped ("end_turn", "max_tokens", "stop_sequence", "tool_use")usage: Token usage statistics
{
"content": [
{
"type": "text",
"text": "Paris was founded in the 3rd century BC..."
}
],
"stop_reason": "end_turn",
"usage": {
"input_tokens": 25,
"output_tokens": 150
}
}
Step 3: Choose the Right Model
Claude offers three model families, each optimized for different use cases:
| Model | Best For | Cost | Speed |
|---|---|---|---|
| Claude Opus 4.7 | Complex reasoning, agentic coding, research | Highest | Moderate |
| Claude Sonnet 4.6 | Coding, agents, enterprise workflows | Medium | Fast |
| Claude Haiku 4.5 | Simple tasks, classification, real-time apps | Lowest | Fastest |
Step 4: Explore Key Features
Extended Thinking
Claude can "think" before responding, improving reasoning on complex tasks:
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=4096,
thinking={
"type": "enabled",
"budget_tokens": 2048 # Allocate tokens for thinking
},
messages=[
{"role": "user", "content": "Solve this complex math problem step by step..."}
]
)
Tool Use (Function Calling)
Claude can use external tools and APIs. Define tools in your request:
tools = [
{
"name": "get_weather",
"description": "Get current weather for a location",
"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?"}
]
)
Structured Outputs
Request JSON-formatted responses for reliable 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: Invoice #1234, dated 2024-01-15, for $500.00 payable to Acme Corp."
}
],
response_format={
"type": "json_object",
"schema": {
"type": "object",
"properties": {
"invoice_number": {"type": "string"},
"date": {"type": "string"},
"amount": {"type": "number"},
"payee": {"type": "string"}
},
"required": ["invoice_number", "date", "amount", "payee"]
}
}
)
Vision (Image Processing)
Claude can analyze images and generate text from them:
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 in detail."},
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": image_data
}
}
]
}
]
)
Streaming Responses
For real-time applications, stream responses token by token:
stream = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[{"role": "user", "content": "Tell me a story"}],
stream=True
)
for chunk in stream:
if chunk.type == "content_block_delta":
print(chunk.delta.text, end="", flush=True)
Best Practices for Production
- Handle stop reasons: Always check
stop_reasonin responses to handle tool calls, max tokens, or other termination conditions. - Implement retry logic: Use exponential backoff for rate limits and transient errors.
- Use prompt caching: Cache system prompts and common context to reduce costs and latency.
- Monitor token usage: Track input and output tokens to optimize costs.
- Set appropriate max_tokens: Avoid truncation by setting realistic limits based on your use case.
Developer Tools
Anthropic provides several tools to accelerate development:
- Developer Console: Prototype and test prompts with the Workbench and prompt generator.
- API Reference: Comprehensive documentation for all endpoints and SDKs.
- Claude Cookbook: Interactive Jupyter notebooks covering PDFs, embeddings, and more.
Key Takeaways
- Start with the Messages API for maximum flexibility and control over your Claude integration.
- Choose your model wisely: Sonnet for balance, Opus for complex reasoning, Haiku for speed and cost efficiency.
- Master the request/response structure: Understand system prompts, multi-turn conversations, and stop reasons.
- Leverage advanced features: Extended thinking, tool use, structured outputs, and streaming can dramatically improve your application.
- Use Anthropic's developer tools: The Console, API Reference, and Cookbook accelerate development and testing.