Getting Started with the Claude API: From First Call to Production
A practical guide to building with the Claude API, covering SDKs, Messages API, Managed Agents, and best practices for production deployment.
Learn how to integrate Claude into your applications using the Messages API or Managed Agents. This guide covers API key setup, SDK installation, code examples in Python and TypeScript, and best practices for moving from prototype to production.
Introduction
Claude isn't just a chatbot—it's a powerful AI engine you can embed directly into your applications. Whether you're building a customer support agent, a code assistant, or a content generation pipeline, the Claude API gives you programmatic access to the same models that power claude.ai.
This guide walks you through the entire developer journey: from getting your first API key to writing production-ready code. You'll learn about the two main integration surfaces—Messages API and Managed Agents—and get practical examples in Python and TypeScript.
Choose Your Integration Surface
Claude offers two primary ways to build:
Messages API (Direct Model Access)
With the Messages API, you have full control. You construct every turn of the conversation, manage conversation state yourself, and write your own tool loop. This is the right choice when you need:
- Custom conversation logic
- Fine-grained control over model behavior
- Integration with existing backend systems
- Advanced features like extended thinking, vision, or tool use
Managed Agents (Fully Managed Infrastructure)
Managed Agents handle the heavy lifting. You define the agent's instructions and tools, and Anthropic manages the state, event history, and tool execution loop. This is ideal when you want to:
- Deploy autonomous agents quickly
- Offload session management
- Get persistent event history out of the box
- Focus on agent behavior rather than infrastructure
Prerequisites
Before you start coding, you'll need:
- An Anthropic account – Sign up at console.anthropic.com
- An API key – Generate one in the API Keys section of the console
- A supported SDK – Python 3.7+ or Node.js 18+ recommended
Step 1: Get Your API Key
- Log in to the Anthropic Console
- Navigate to API Keys
- Click Create Key and give it a descriptive name (e.g., "My App - Production")
- Copy the key immediately—you won't be able to see it again
Security Tip: Never hardcode your API key in client-side code or commit it to version control. Use environment variables or a secrets manager.
Step 2: Install the SDK
Python
pip install anthropic
TypeScript / JavaScript
npm install @anthropic-ai/sdk
Other Languages
Anthropic also provides official SDKs for Go, Java, Ruby, PHP, and C#. You can find installation instructions in the API Reference.
Step 3: Make Your First API Call
Let's start with a simple "Hello, Claude" message.
Python Example
import anthropic
client = anthropic.Anthropic(
api_key="YOUR_API_KEY" # Better: use environment variable
)
message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[
{"role": "user", "content": "Hello, Claude"}
]
)
print(message.content[0].text)
TypeScript Example
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
});
async function main() {
const message = await client.messages.create({
model: 'claude-sonnet-4-6',
max_tokens: 1024,
messages: [
{ role: 'user', content: 'Hello, Claude' }
],
});
console.log(message.content[0].text);
}
main();
What's happening?
model: Specifies which Claude model to use.claude-sonnet-4-6offers the best balance of speed and intelligence for most use cases.max_tokens: Limits the response length. Start with 1024 and adjust based on your needs.messages: An array of conversation turns. Each turn has arole(user or assistant) andcontent.
Step 4: Build a Multi-Turn Conversation
Claude is stateless—each API call is independent. To maintain context across multiple turns, you need to send the entire conversation history with each request.
import anthropic
client = anthropic.Anthropic()
conversation = [
{"role": "user", "content": "What is the capital of France?"}
]
First turn
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=256,
messages=conversation
)
Add Claude's response to history
conversation.append({"role": "assistant", "content": response.content[0].text})
Add user's follow-up
conversation.append({"role": "user", "content": "What about Germany?"})
Second turn with full history
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=256,
messages=conversation
)
print(response.content[0].text)
Step 5: Explore Advanced Features
Once you have the basics working, you can unlock Claude's full potential:
Extended Thinking
For complex reasoning tasks, enable Claude's extended thinking mode:
message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=4096,
thinking={"type": "enabled", "budget_tokens": 2048},
messages=[{"role": "user", "content": "Solve this complex math problem..."}]
)
Vision (Image Input)
Claude can analyze images:
import base64
with open("chart.png", "rb") as f:
image_data = base64.b64encode(f.read()).decode()
message = client.messages.create(
model="claude-sonnet-4-6",
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
}}
]
}]
)
Tool Use (Function Calling)
Give Claude the ability to call your own functions:
tools = [
{
"name": "get_weather",
"description": "Get the current weather for a city",
"input_schema": {
"type": "object",
"properties": {
"city": {"type": "string"}
},
"required": ["city"]
}
}
]
message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
tools=tools,
messages=[{"role": "user", "content": "What's the weather in Tokyo?"}]
)
Step 6: Move to Production
Prompt Caching
Reduce costs and latency for repeated system prompts by enabling prompt caching:
message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
system=[
{
"type": "text",
"text": "You are a helpful assistant...",
"cache_control": {"type": "ephemeral"}
}
],
messages=[{"role": "user", "content": "Hello"}]
)
Rate Limits & Error Handling
Always implement retry logic with exponential backoff:
import time
from anthropic import Anthropic, APIError, RateLimitError
client = Anthropic()
max_retries = 3
for attempt in range(max_retries):
try:
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello"}]
)
break
except RateLimitError:
if attempt < max_retries - 1:
time.sleep(2 ** attempt)
else:
raise
except APIError as e:
print(f"API error: {e}")
raise
Cost Optimization
- Use Haiku for high-volume, simple tasks
- Use Sonnet for most production workloads
- Reserve Opus for complex analysis and creative tasks
- Enable prompt caching for repeated system prompts
- Set appropriate
max_tokenslimits
Choosing the Right Model
| Model | Best For | Speed | Cost |
|---|---|---|---|
Opus 4.7 (claude-opus-4-7) | Complex analysis, coding, deep reasoning | Slowest | Highest |
Sonnet 4.6 (claude-sonnet-4-6) | General production workloads | Balanced | Moderate |
Haiku 4.5 (claude-haiku-4-5) | High-volume, latency-sensitive apps | Fastest | Lowest |
Next Steps
- Try the Workbench – Experiment with prompts in the Anthropic Console
- Explore the Cookbook – Find code samples and patterns at docs.anthropic.com/en/cookbook
- Take Interactive Courses – Learn advanced techniques through guided courses
- Deploy with Managed Agents – If you want to skip state management, dive into the Managed Agents Quickstart
Key Takeaways
- Two integration paths: Choose Messages API for full control or Managed Agents for turnkey deployment
- Stateless design: You must send the full conversation history with each API call
- SDK support: Official SDKs available for Python, TypeScript, Go, Java, Ruby, PHP, and C#
- Advanced features: Leverage extended thinking, vision, tool use, and prompt caching for production apps
- Model selection: Match model choice to your use case—Haiku for speed, Sonnet for balance, Opus for depth