S9.6
Claude API Tutorial: How to Make Your First API Call
The short answer
TL;DR
- →You can make your first Claude API call in under 10 minutes.
- →The Claude API uses a simple messages format: role + content.
- →Claude Haiku is the cheapest model at $0.25 per million input tokens.
- →Always set a max_tokens limit or costs can spike unexpectedly.
- →The system prompt is where you define Claude's behaviour for your app.
How do you get started with the Claude API?
To make your first Claude API call, get an API key from console.anthropic.com, install the Anthropic SDK, and send a messages request with a role and content. You'll get a text response back in under a second. The whole setup takes about 10 minutes.
This tutorial covers Python. The TypeScript/JavaScript SDK works the same way.
Step 1: Get your API key
Go to console.anthropic.com and create an account. Go to API Keys and generate a new key. Store it as an environment variable, never hardcode it in your source.
export ANTHROPIC_API_KEY="your-key-here"Step 2: Install the SDK
pip install anthropicThat's it. The SDK handles authentication, request formatting, and response parsing.
Step 3: Make your first call
import anthropic
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-3-5-haiku-20241022",
max_tokens=1024,
system="You are a helpful assistant for a software company.",
messages=[
{"role": "user", "content": "What's the difference between REST and GraphQL?"}
]
)
print(message.content[0].text)Run that and you'll get a full response. Two things to note.
Always set max_tokens. Without it, the model will run until it finishes naturally. That's fine until someone asks a question that generates a 5000-token response and your bill spikes. Set a sensible limit for your use case.
Use the system parameter. This is where you tell Claude how to behave. What's its role? What should it do or not do? What format should it respond in? A good system prompt is the difference between a generic AI and a useful product feature.
Step 4: Handle the response
The response object has a few useful fields.
# The text response
text = message.content[0].text
# Token usage for cost tracking
input_tokens = message.usage.input_tokens
output_tokens = message.usage.output_tokens
# Why the model stopped (end_turn, max_tokens, tool_use)
stop_reason = message.stop_reasonCheck stop_reason. If it's max_tokens, the response was cut off. Either raise your limit or tell the model to be more concise.
Step 5: Add conversation history
For a multi-turn conversation, pass the full message history each time.
messages = []
# First turn
messages.append({"role": "user", "content": "What is RAG?"})
response = client.messages.create(
model="claude-3-5-haiku-20241022",
max_tokens=1024,
messages=messages
)
messages.append({"role": "assistant", "content": response.content[0].text})
# Second turn
messages.append({"role": "user", "content": "When should I use it?"})
response = client.messages.create(
model="claude-3-5-haiku-20241022",
max_tokens=1024,
messages=messages
)The model doesn't store history. You send it every time. Keep this in mind for cost: every turn includes all previous turns in the input token count.
Which Claude model should you use?
| Model | Speed | Cost | Best For |
|---|---|---|---|
| Claude Haiku 3.5 | Fastest | Lowest | High-volume, simple tasks |
| Claude Sonnet 3.7 | Fast | Mid | Most production apps |
| Claude Opus 4.8 | Slower | Highest | Complex reasoning, analysis |
Start with Haiku for prototyping. Move to Sonnet when you need better reasoning. Only use Opus for tasks that genuinely need it.
For a comparison with the OpenAI API, see OpenAI API vs Claude API for builders. Once you've got basic calls working, read prompt engineering for developers to get consistently better results.
Want someone to build the full integration? Devwiz handles end-to-end AI app development for businesses.
Ready to build? Talk to us about what you're making.
Frequently Asked Questions
- How much does the Claude API cost?
- Claude pricing varies by model. Claude Haiku 3.5 costs $0.80 per million input tokens and $4 per million output tokens. Claude Sonnet 3.7 costs $3 per million input and $15 per million output. For most apps, Haiku is enough for straightforward tasks.
- What's the difference between the system prompt and the user message?
- The system prompt sets the context and behaviour for the whole conversation. The user message is what the end user sends. Put your instructions, persona, and constraints in the system prompt. Put user input in the user message.
- How do I handle streaming responses from the Claude API?
- Use the stream=True parameter in the Python SDK or the streaming endpoint. Streaming returns tokens as they're generated instead of waiting for the full response. This makes apps feel faster for users.
Related Services