Claude 3.5 Sonnet: Capabilities and Usage
Table of Contents
1. Background
Claude 3.5 Sonnet is Anthropic's mid-tier model balancing capability with speed. Released in June 2024, it offers strong reasoning and coding abilities at lower cost than Opus.
1.1. Model Comparison
| Model | Strengths | Context | Speed |
|---|---|---|---|
| Claude 3.5 Sonnet | Balanced, coding, analysis | 200K | Fast |
| Claude 3 Opus | Complex reasoning | 200K | Slower |
| Claude 3 Haiku | Speed, simple tasks | 200K | Fastest |
2. API Usage
2.1. Python SDK
import anthropic
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=[
{"role": "user", "content": "Explain recursion in simple terms."}
]
)
print(message.content[0].text)
2.2. Streaming Responses
with client.messages.stream(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=[{"role": "user", "content": "Write a haiku about coding."}]
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
2.3. System Prompts
message = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
system="You are a helpful coding assistant. Always include code examples.",
messages=[
{"role": "user", "content": "How do I read a file in Python?"}
]
)
3. Tool Use
tools = [
{
"name": "get_weather",
"description": "Get current weather for a location",
"input_schema": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "City name"}
},
"required": ["location"]
}
}
]
message = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
tools=tools,
messages=[{"role": "user", "content": "What's the weather in Boston?"}]
)
4. Best Practices
- Use system prompts for consistent behavior
- Leverage tool use for structured outputs
- Stream responses for better UX
- Handle rate limits with exponential backoff
- Cache responses when appropriate