Claude API Tutorial: Get Started with Anthropic's API (2025)
Complete Claude API guide — get your API key from console.anthropic.com, make your first call in Python and Node.js, set system prompts, build multi-turn conversations, stream responses, send images, and use tool calling. Full copy-paste code examples.
1. What is the Claude API?
The Claude API (by Anthropic) lets you integrate Claude's language models directly into your own applications. Instead of using the Claude.ai chat interface, you call the API from your code and get Claude's responses programmatically.
Common use cases
Models available (2025)
$3/1M input tokens, $15/1M output tokens. Best balance of intelligence and speed — the default choice for most use cases.
$0.80/1M input, $4/1M output. Fastest and cheapest — good for simple classification, triage, and high-volume tasks where cost matters most.
Highest intelligence, most expensive. Use for complex reasoning, long document analysis, and tasks where quality is the top priority over cost.
2. Get your API key (5 steps)
You need an API key to authenticate every request. Here's how to generate one.
Go to console.anthropic.com
Open console.anthropic.com in your browser. This is Anthropic's developer console — separate from the Claude.ai chat UI.
Sign up or sign in with your email
Create a new account or log in to an existing one. You can use email or Google sign-in.
Add a payment method
A payment method is required before API access is granted. Anthropic uses pay-per-use billing — you are only charged for tokens you actually use. There is no monthly minimum.
Navigate to "API Keys" in the left sidebar
In the console, find the API Keys section in the left navigation menu.
Click "Create Key" — name it — copy it immediately
Give the key a descriptive name (e.g., "my-app-dev"). Copy the key immediately after creation — it is shown only once and cannot be retrieved again.
sk-ant-. Never hardcode it in your source code or commit it to Git. Store it as an environment variable named ANTHROPIC_API_KEY.
3. Your first API call
Anthropic provides official SDKs for Python and Node.js. Install one and make your first call in under 5 minutes.
Python
pip install anthropicimport anthropic
client = anthropic.Anthropic(api_key="YOUR_API_KEY") # or use ANTHROPIC_API_KEY env var
message = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=[
{"role": "user", "content": "Explain the difference between a list and a tuple in Python."}
]
)
print(message.content[0].text) Node.js
npm install @anthropic-ai/sdkimport Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });
const message = await client.messages.create({
model: "claude-3-5-sonnet-20241022",
max_tokens: 1024,
messages: [
{ role: "user", content: "Explain the difference between a list and a tuple in Python." }
]
});
console.log(message.content[0].text); The Anthropic client automatically reads ANTHROPIC_API_KEY from the environment — no explicit passing needed when using the env var.
4. System prompts — how to set Claude's role
A system prompt is a set of instructions that frames Claude's behavior before the user's first message. It defines Claude's role, persona, output format, and constraints.
message = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
system="You are a senior Python engineer. Be concise. Always include type hints in code examples. Never explain obvious things.",
messages=[
{"role": "user", "content": "Write a function to parse CSV files safely."}
]
) Good system prompt practices
5. Multi-turn conversations
The Claude API is stateless — it does not remember previous calls. To build a conversation, you pass the full message history on every request. Append each new message and response to the list before calling the API again.
messages = []
# Turn 1
messages.append({"role": "user", "content": "I'm building a REST API with FastAPI."})
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=messages
)
messages.append({"role": "assistant", "content": response.content[0].text})
# Turn 2 — Claude remembers the FastAPI context
messages.append({"role": "user", "content": "How should I handle authentication?"})
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=messages
)
print(response.content[0].text) 6. Streaming responses
Instead of waiting for the full response, streaming lets you display Claude's output token by token — just like the Claude.ai chat UI. This improves perceived latency for end users.
with client.messages.stream(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=[{"role": "user", "content": "Write a 3-paragraph essay about the impact of AI on software development."}]
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True) 7. Vision — send images to Claude
Claude can analyze images. You encode the image as base64 and pass it in the content array alongside your text message.
import base64
with open("screenshot.png", "rb") as f:
image_data = base64.standard_b64encode(f.read()).decode("utf-8")
message = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": image_data,
}
},
{
"type": "text",
"text": "Describe what's happening in this screenshot."
}
]
}
]
)
print(message.content[0].text) "type": "url" instead of base64.
8. Tool use (function calling)
Tool use lets you define functions (tools) that Claude can decide to call. You pass a list of tool definitions with your request. When Claude determines a tool is needed, it returns a tool_use block with the function name and arguments — your code then executes the function and returns the result.
tools = [
{
"name": "get_weather",
"description": "Get current weather for a city",
"input_schema": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["city"]
}
}
]
message = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
tools=tools,
messages=[{"role": "user", "content": "What's the weather in Tokyo?"}]
)
# Claude will respond with a tool_use block
if message.stop_reason == "tool_use":
tool_call = message.content[1]
print(f"Claude wants to call: {tool_call.name}")
print(f"With args: {tool_call.input}") tool_use block, your code calls the actual function, then sends the result back in a new message with role user and type tool_result. Claude then uses that result to produce its final answer.
9. Pricing and token counting
The Claude API charges per token. A token is roughly 4 characters in English. "Hello world" is 2 tokens. You pay separately for input tokens (your messages + system prompt) and output tokens (Claude's response).
Pricing summary (per 1M tokens)
| Model | Input | Output | Best for |
|---|---|---|---|
| claude-3-5-sonnet | $3.00 | $15.00 | Default, general use |
| claude-haiku-3-5 | $0.80 | $4.00 | High volume, simple tasks |
| claude-opus-4 | Higher | Higher | Complex reasoning |
Count tokens before sending
token_count = client.beta.messages.count_tokens(
model="claude-3-5-sonnet-20241022",
messages=[{"role": "user", "content": "How many tokens is this?"}]
)
print(token_count.input_tokens) Monitor Anthropic API status at Prismix
Before you debug unexpected errors in your Claude API integration, check if Anthropic is having an outage. Prismix monitors Anthropic's API in real time and sends free alerts when status changes — so you know immediately whether it's your code or their infrastructure.
FAQ
How do I get a Claude API key?
Go to console.anthropic.com, sign up, add a payment method, navigate to "API Keys" in the sidebar, click "Create Key." The key starts with sk-ant-. Store it as an environment variable (ANTHROPIC_API_KEY) — never hardcode it.
How much does the Claude API cost?
claude-3-5-sonnet (recommended): $3 per 1 million input tokens, $15 per 1 million output tokens. claude-haiku-3-5 (fast): $0.80 input / $4 output per 1M tokens. Anthropic provides some free credits on signup for testing. Billing is pay-per-use with no monthly minimum.
What is the difference between Claude API and Claude.ai?
Claude.ai is the chat interface (like ChatGPT). The Claude API lets you integrate Claude into your own applications by writing code. The API gives you full control: custom system prompts, streaming, tool use, vision, and JSON output — things not exposed in the chat UI.
Can I use Claude API for free?
Anthropic provides initial free credits for testing. After that, you pay per token used. There is no free permanent tier for the API (unlike Claude.ai which has a free chat tier). The API requires a valid payment method before you can start making requests.