Your Complete Guide to Building with the Claude API: From First Call to Production
Learn how to integrate Claude into your applications using the Messages API, SDKs, and managed agents. Includes code examples, model selection tips, and best practices for production.
This guide walks you through the entire Claude API development journey: getting your API key, making your first call with Python or TypeScript, choosing the right model (Opus, Sonnet, or Haiku), and using advanced features like tool use, streaming, and managed agents.
Introduction
Claude is more than just a conversational AI — it's a powerful platform for building intelligent applications. Whether you're creating a customer support bot, a code assistant, or a complex agent that uses tools and external data, the Claude API gives you everything you need to go from idea to production.
This guide covers the full developer journey: setting up your environment, making your first API call, choosing the right model, and leveraging advanced features like tool use, streaming, and managed agents.
Getting Started with the Claude API
Step 1: Get Your API Key
Before you can make any API calls, you need an API key. Head to the Claude Console and log in. Navigate to the API Keys section and create a new key. Keep this key secure — treat it like a password.
Step 2: Choose Your Development Surface
The Claude Platform offers two primary ways to build:
- Messages API: Direct model access. You construct every turn, manage conversation state, and write your own tool loop. Best for custom integrations and fine-grained control.
- Claude Managed Agents: Fully managed agent infrastructure. Deploy and manage autonomous agents in stateful sessions with persistent event history. Best for rapid deployment and reduced operational overhead.
Step 3: Install an SDK
Anthropic provides official SDKs for Python, TypeScript, Go, Java, Ruby, PHP, C#, and more. Here's how to install the two most popular ones:
Pythonpip install anthropic
TypeScript
npm install @anthropic-ai/sdk
Making Your First API Call
Once you have your API key and SDK installed, you're ready to make your first request. Here's a complete example in Python:
import anthropic
client = anthropic.Anthropic(
api_key="your-api-key-here"
)
message = client.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
messages=[
{"role": "user", "content": "Hello, Claude!"}
]
)
print(message.content[0].text)
And the same call in TypeScript:
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-opus-4-7',
max_tokens: 1024,
messages: [{ role: 'user', content: 'Hello, Claude!' }],
});
console.log(message.content[0].text);
}
main();
Understanding the Response
The API returns a structured response. The content field is an array of content blocks. For a simple text response, you'll access message.content[0].text. For more complex responses (like tool calls), you'll iterate over the content blocks.
Choosing the Right Claude Model
Claude comes in three tiers, each optimized for different use cases:
| Model | ID | Best For |
|---|---|---|
| Opus 4.7 | claude-opus-4-7 | Complex analysis, coding, creative tasks requiring deep reasoning |
| Sonnet 4.6 | claude-sonnet-4-6 | Ideal balance of intelligence and speed for most production workloads |
| Haiku 4.5 | claude-haiku-4-5 | Lightning-fast responses for high-volume, latency-sensitive applications |
Advanced Features
Tool Use (Function Calling)
Claude can use external tools to perform actions, fetch data, or compute results. Here's a minimal example of defining a tool:
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
tools=[
{
"name": "get_weather",
"description": "Get the current weather for a city",
"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"Claude wants to call: {block.name}")
print(f"With input: {block.input}")
Streaming Responses
For a better user experience, stream responses token by token:
with client.messages.stream(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[{"role": "user", "content": "Tell me a story"}]
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
Vision (Image Understanding)
Claude can analyze images. Pass them as base64-encoded data:
import base64
with open("chart.png", "rb") as f:
image_data = base64.b64encode(f.read()).decode("utf-8")
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What does this chart show?"},
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": image_data
}
}
]
}
]
)
print(response.content[0].text)
Prompt Caching
Reduce costs and latency by caching repeated system prompts or context:
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
system=[
{
"type": "text",
"text": "You are a helpful assistant specialized in Python programming.",
"cache_control": {"type": "ephemeral"}
}
],
messages=[{"role": "user", "content": "Write a function to sort a list"}]
)
Evaluating and Shipping
Before going to production, follow these best practices:
- Run evaluations: Use the Evaluation Tool in the Claude Console to test your prompts against expected outputs.
- Set up guardrails: Implement content filtering and rate limiting.
- Monitor usage: Use the Workspaces & Admin API to track API key usage and costs.
- Handle errors gracefully: Implement retry logic for rate limits and timeouts.
Key Takeaways
- Start with the Messages API for maximum flexibility, then consider Managed Agents for simpler deployment.
- Choose your model wisely: Opus for deep reasoning, Sonnet for balanced performance, Haiku for speed.
- Leverage advanced features like tool use, streaming, and vision to build richer applications.
- Always run evaluations before shipping to production to ensure quality and safety.
- Use prompt caching to reduce costs and latency for repeated system prompts.