The Claude API from Anthropic gives you direct programmatic access to Claude. This guide covers everything you need to get from API key to production code.
Setup
pip install anthropic
Get your API key from console.anthropic.com. Set it as an environment variable: export ANTHROPIC_API_KEY=sk-ant-...
Your First Message
import anthropic
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-opus-4-6",
max_tokens=1024,
messages=[{"role": "user", "content": "Explain AI agents in one paragraph."}]
)
print(message.content[0].text)
Available Models
claude-opus-4-6: Most capable. Best for complex reasoning, coding, analysis, and agentic tasks.
claude-sonnet-4-6: Balanced capability and speed. Good for most production tasks.
claude-haiku-4-5: Fastest and cheapest. Best for high-volume classification and simple generation.
Streaming
with client.messages.stream(
model="claude-opus-4-6",
max_tokens=1024,
messages=[{"role": "user", "content": "Write a blog post about AI agents."}]
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
Tool Use
tools = [{
"name": "search_web",
"description": "Search the web for current information.",
"input_schema": {
"type": "object",
"properties": {"query": {"type": "string"}},
"required": ["query"]
}
}]
response = client.messages.create(
model="claude-opus-4-6",
max_tokens=1024,
tools=tools,
messages=[{"role":"user","content":"What are the latest AI agent frameworks?"}]
)
if response.stop_reason == "tool_use":
for block in response.content:
if block.type == "tool_use":
print(f"Tool called: {block.name}, input: {block.input}")
System Prompts
response = client.messages.create(
model="claude-opus-4-6",
max_tokens=1024,
system="You are an expert Singapore business consultant. Give practical advice tailored to Singapore context.",
messages=[{"role": "user", "content": "How should I structure my AI agent pricing?"}]
)
Vision
import base64
with open("screenshot.png", "rb") as f:
image_data = base64.standard_b64encode(f.read()).decode("utf-8")
response = client.messages.create(
model="claude-opus-4-6",
max_tokens=1024,
messages=[{"role": "user", "content": [
{"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": image_data}},
{"type": "text", "text": "Describe what you see in this screenshot."}
]}]
)
Cost Management
- Use Haiku for classification and high-volume simple tasks
- Use prompt caching to reduce repeat system prompt costs by up to 90%
- Set
max_tokensconservatively — you only pay for tokens generated - Log all API calls with token counts to spot expensive patterns early
Next Steps
Building a production application on Claude? Power Digital builds custom AI agent solutions for Singapore businesses.