Claude 3.5 Sonnet: Capabilities and Usage

Table of Contents

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.

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

API Usage

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)

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)

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?"}
    ]
)

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?"}]
)

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

Resources

Author: Jason Walsh

j@wal.sh

Last Updated: 2025-12-21 23:02:59

build: 2025-12-23 09:13 | sha: e32f33e