Getting Started with the Claude API: A Beginner's Guide to Building with Anthropic's Latest Models
Learn how to set up the Claude API, make your first API call, understand the Messages API structure, and explore key features like extended thinking, tool use, and structured outputs.
This guide walks you through setting up the Claude API, making your first API call with Python, understanding the Messages API request/response structure, and exploring key capabilities like extended thinking, tool use, and structured outputs.
Introduction
Anthropic's Claude API gives developers direct access to the latest generation of Claude models—Claude Opus 4.7, Claude Sonnet 4.6, and Claude Haiku 4.5—each optimized for different use cases. Whether you're building a custom agent loop, integrating Claude into your SaaS product, or experimenting with advanced features like extended thinking and tool use, the Messages API is your gateway.
This guide covers everything you need to go from zero to a working Claude integration, including environment setup, your first API call, understanding the request/response structure, and exploring the most impactful features.
Prerequisites
Before you begin, you'll need:
- An Anthropic account with API access (sign up at console.anthropic.com)
- An API key from the Developer Console
- Python 3.8+ installed on your machine
- Basic familiarity with Python and REST APIs
Step 1: Set Up Your Environment
Install the official Anthropic Python SDK:
pip install anthropic
For TypeScript/Node.js developers, install the npm package:
npm install @anthropic-ai/sdk
Set your API key as an environment variable to keep it secure:
export ANTHROPIC_API_KEY="sk-ant-..."
Step 2: Make Your First API Call
Here's the simplest possible request—sending a single message to Claude and getting a response.
Python Example
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)
TypeScript Example
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic();
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();
What happens? Claude returns a Message object containing the generated text, stop reason, token usage, and model metadata.
Step 3: Understand the Messages API Structure
The Messages API is designed for both single-turn and multi-turn conversations. Here's the anatomy of a request:
Request Fields
| Field | Type | Description |
|---|---|---|
model | string | The model identifier (e.g., claude-sonnet-4-20250514) |
messages | array | Array of message objects with role and content |
system | string (optional) | System prompt to set behavior and context |
max_tokens | integer | Maximum tokens in the response |
temperature | float (optional) | Controls randomness (0.0–1.0, default 0.7) |
stop_sequences | array (optional) | Custom stop sequences |
Multi-Turn Conversations
To continue a conversation, include the full history:
messages = [
{"role": "user", "content": "What is the capital of France?"},
{"role": "assistant", "content": "The capital of France is Paris."},
{"role": "user", "content": "What is its population?"}
]
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=messages
)
Handling Stop Reasons
Every response includes a stop_reason field that tells you why Claude stopped generating:
"end_turn": Claude finished naturally"max_tokens": The response hit the token limit"stop_sequence": A custom stop sequence was encountered"tool_use": Claude is requesting to use a tool
if response.stop_reason == "max_tokens":
print("Response was truncated—consider increasing max_tokens.")
elif response.stop_reason == "tool_use":
print("Claude wants to call a tool. Handle the tool call and continue.")
Step 4: Choose the Right Model
Anthropic offers three model tiers:
| Model | Best For | Speed | Cost |
|---|---|---|---|
| Claude Opus 4.7 | Complex reasoning, agentic coding, research | Slowest | Highest |
| Claude Sonnet 4.6 | General coding, agents, enterprise workflows | Fast | Moderate |
| Claude Haiku 4.5 | High-throughput, low-latency tasks | Fastest | Lowest |
Step 5: Explore Key Features
Extended Thinking
Enable Claude to "think" before responding, improving reasoning on complex tasks:
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=4096,
thinking={"type": "enabled", "budget_tokens": 2048},
messages=[{"role": "user", "content": "Solve this logic puzzle..."}]
)
Structured Outputs
Get Claude to return JSON that matches a schema:
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[{"role": "user", "content": "Extract the name, date, and amount from this invoice..."}],
response_format={
"type": "json_schema",
"json_schema": {
"name": "invoice",
"schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"date": {"type": "string"},
"amount": {"type": "number"}
},
"required": ["name", "date", "amount"]
}
}
}
)
Tool Use (Function Calling)
Give Claude the ability to call external tools:
tools = [
{
"name": "get_weather",
"description": "Get the current weather for a city",
"input_schema": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "City name"}
},
"required": ["location"]
}
}
]
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[{"role": "user", "content": "What's the weather in Tokyo?"}],
tools=tools
)
When Claude responds with stop_reason: "tool_use", you execute the tool and send the result back in a follow-up message.
Streaming Responses
For real-time output, enable streaming:
stream = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[{"role": "user", "content": "Write a short poem"}],
stream=True
)
for event in stream:
if event.type == "content_block_delta":
print(event.delta.text, end="", flush=True)
Step 6: Use the Developer Console
Before writing code, prototype your prompts in the Anthropic Developer Console. The Workbench lets you:
- Test prompts with different models
- Adjust parameters (temperature, max tokens)
- View token usage and cost estimates
- Generate prompt templates
Best Practices
- Set appropriate
max_tokens— Too low truncates responses; too high wastes tokens. - Use system prompts to set Claude's persona and constraints.
- Implement retry logic with exponential backoff for rate limits.
- Monitor token usage to control costs, especially with tool calls and long conversations.
- Leverage prompt caching for repeated system prompts to reduce latency and cost.
Next Steps
- Read the Messages API guide for advanced usage
- Explore the Claude Cookbook for Jupyter notebook examples
- Build a tool-using agent with the Tool Use tutorial
- Learn about Managed Agents for long-running, asynchronous tasks
Key Takeaways
- The Claude API is accessible via a simple REST endpoint or the official Python/TypeScript SDKs, with the Messages API as the core interface.
- Always handle
stop_reasonto manage tool calls, truncation, and natural conversation endings. - Choose your model based on task complexity: Opus for deep reasoning, Sonnet for balanced performance, Haiku for speed and cost.
- Key features like extended thinking, structured outputs, tool use, and streaming unlock powerful applications beyond simple chat.
- Prototype in the Developer Console before coding to iterate quickly on prompts and parameters.