Getting Started with the Claude API: A Beginner's Guide to Building with Anthropic's Models
Learn how to set up the Claude API, make your first API call, understand the Messages API structure, and explore key features like tools, streaming, and extended thinking.
This guide walks you through setting up the Claude API, making your first request with Python, understanding the Messages API structure, and exploring key capabilities like streaming, tools, and extended thinking.
Getting Started with the Claude API: A Beginner's Guide
Anthropic's Claude models—Claude Opus 4.7, Claude Sonnet 4.6, and Claude Haiku 4.5—offer frontier intelligence for a wide range of applications, from complex reasoning and agentic coding to fast, scalable enterprise workflows. Whether you're building a custom chatbot, automating document analysis, or creating an AI-powered tool, the Claude API gives you direct access to these models.
This guide is your starting point. You'll learn how to set up your environment, make your first API call, understand the core Messages API structure, and explore the key features that make Claude a powerful platform for developers.
Prerequisites
Before you begin, make sure you have:
- An Anthropic account and an API key. If you don't have one, sign up at console.anthropic.com.
- Python 3.8+ installed on your machine.
- Basic familiarity with Python and REST APIs.
Step 1: Make Your First API Call
Let's start by installing the Anthropic Python SDK and sending your first message to Claude.
Install the SDK
Open your terminal and run:
pip install anthropic
Set Your API Key
For security, store your API key as an environment variable:
export ANTHROPIC_API_KEY="your-api-key-here"
Send Your First Message
Create a file named first_call.py and add the following code:
import anthropic
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1000,
messages=[
{"role": "user", "content": "Hello, Claude! What can you do?"}
]
)
print(message.content[0].text)
Run the script:
python first_call.py
You should see a friendly response from Claude. Congratulations—you've made your first API call!
Note: The model name claude-sonnet-4-20250514 refers to Claude Sonnet 4.6. For the latest models, check the models overview.
Step 2: Understand the Messages API
The Messages API is the primary way to interact with Claude. It uses a simple request/response structure that supports multi-turn conversations, system prompts, and various content types.
Basic Request Structure
Here's a more complete example showing the key parameters:
import anthropic
client = anthropic.Anthropic()
response = 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 best way to learn Python?"},
{"role": "assistant", "content": "Arr, I'd start with the basics: variables, loops, and functions!"},
{"role": "user", "content": "Can you give me an example of a loop?"}
]
)
print(response.content[0].text)
Key parameters explained:
model: The Claude model you want to use (e.g.,claude-sonnet-4-20250514,claude-opus-4-20250514).max_tokens: The maximum number of tokens in the response.system: An optional system prompt that sets the behavior and persona of the assistant.messages: An array of message objects, each with arole(userorassistant) andcontent. This allows for multi-turn conversations.
Understanding Stop Reasons
Every response includes a stop_reason field that tells you why Claude stopped generating. Common values include:
"end_turn": Claude finished its response naturally."max_tokens": The response reached themax_tokenslimit."stop_sequence": Claude encountered a custom stop sequence you defined."tool_use": Claude wants to call a tool (more on this later).
print(f"Stop reason: {response.stop_reason}")
Step 3: Choose the Right Model
Anthropic offers several Claude models, each optimized for different use cases:
| Model | Best For | Speed | Cost |
|---|---|---|---|
| Claude Opus 4.7 | Complex reasoning, agentic coding, research | Slower | Higher |
| Claude Sonnet 4.6 | General coding, agents, enterprise workflows | Fast | Moderate |
| Claude Haiku 4.5 | High-throughput, real-time applications | Fastest | Lowest |
Step 4: Explore Key Features
Once you're comfortable with the basics, explore these powerful features:
Streaming Responses
For real-time applications, stream responses token by token:
with client.messages.stream(
model="claude-sonnet-4-20250514",
max_tokens=1000,
messages=[{"role": "user", "content": "Tell me a short story."}]
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
Extended Thinking
Claude can show its reasoning process before giving a final answer. This is especially useful for complex problem-solving:
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=2000,
thinking={"type": "enabled", "budget_tokens": 1000},
messages=[
{"role": "user", "content": "Solve this: If a train leaves Station A at 60 mph and another leaves Station B at 80 mph, 200 miles apart, when do they meet?"}
]
)
Access the thinking content
for block in response.content:
if block.type == "thinking":
print("Thinking:", block.thinking)
elif block.type == "text":
print("Answer:", block.text)
Tool Use (Function Calling)
Claude can call external tools or functions. Here's a minimal example:
def get_weather(location: str) -> str:
# Simulated weather function
return f"The weather in {location} is sunny, 72°F."
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
tools=[
{
"name": "get_weather",
"description": "Get the current weather for a location",
"input_schema": {
"type": "object",
"properties": {
"location": {"type": "string"}
},
"required": ["location"]
}
}
],
messages=[{"role": "user", "content": "What's the weather in Tokyo?"}]
)
Check if Claude wants to use a tool
for block in response.content:
if block.type == "tool_use":
print(f"Tool called: {block.name}")
print(f"Arguments: {block.input}")
result = get_weather(block.input["location"])
print(f"Result: {result}")
Structured Outputs
You can ask Claude to return structured data (like JSON) using the response_format parameter:
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
response_format={"type": "json_object"},
messages=[
{"role": "user", "content": "List three famous scientists and their discoveries as a JSON array."}
]
)
print(response.content[0].text)
Prompt Caching
Reduce costs and latency by caching frequently used system prompts or context:
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
system=[
{
"type": "text",
"text": "You are an expert historian specializing in ancient civilizations.",
"cache_control": {"type": "ephemeral"}
}
],
messages=[{"role": "user", "content": "Tell me about the Roman Empire."}]
)
Next Steps
Now that you've mastered the basics, here's what to explore next:
- Read the Messages API documentation for advanced parameters like
stop_sequences,temperature, andtop_p. - Explore the Claude Cookbook for interactive Jupyter notebooks covering PDFs, embeddings, and more.
- Try the Developer Console to prototype prompts with the Workbench and prompt generator.
- Learn about Claude Managed Agents for long-running, asynchronous tasks.
Key Takeaways
- Start with the Python SDK: Install
anthropic, set your API key, and useclient.messages.create()to send your first message. - Understand the Messages API: The request structure includes
model,max_tokens,system, andmessages—supporting multi-turn conversations and system prompts. - Choose the right model: Use Claude Sonnet for general development, Opus for complex reasoning, and Haiku for high-throughput, low-cost tasks.
- Leverage key features: Streaming, extended thinking, tool use, structured outputs, and prompt caching unlock Claude's full potential.
- Explore further: The Developer Console, Cookbook, and Managed Agents documentation are your next stops for building production-ready applications.