Code beginner · 3 min read

How to use Claude 3.5 Sonnet in python

Direct answer
Use the anthropic Python SDK to create a client with your API key, then call client.messages.create with model="claude-3-5-sonnet-20241022" and your messages to get completions.

Setup

Install
bash
pip install anthropic
Env vars
ANTHROPIC_API_KEY
Imports
python
import os
import anthropic

Examples

inWhat is the capital of France?
outThe capital of France is Paris.
inWrite a Python function to reverse a string.
outHere's a Python function to reverse a string: ```python def reverse_string(s): return s[::-1] ```
inExplain quantum computing in simple terms.
outQuantum computing uses quantum bits that can be both 0 and 1 at the same time, allowing it to solve certain problems much faster than classical computers.

Integration steps

  1. Install the Anthropic Python SDK and set your API key in the environment variable ANTHROPIC_API_KEY.
  2. Import the anthropic library and initialize the client with your API key from os.environ.
  3. Prepare your conversation messages as a list of dictionaries with roles and content.
  4. Call client.messages.create with model="claude-3-5-sonnet-20241022", system prompt, max_tokens, and messages.
  5. Extract the generated text from the response's content field.
  6. Use or display the returned text as needed in your application.

Full code

python
import os
import anthropic

client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

system_prompt = "You are a helpful assistant."
user_message = "Explain the benefits of using Claude 3.5 Sonnet in Python."

response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=300,
    system=system_prompt,
    messages=[{"role": "user", "content": user_message}]
)

print("Response from Claude 3.5 Sonnet:")
print(response.content[0].text)
output
Response from Claude 3.5 Sonnet:
Claude 3.5 Sonnet offers advanced natural language understanding and generation capabilities, making it ideal for Python developers to build conversational AI, automate tasks, and generate code efficiently.

API trace

Request
json
{"model": "claude-3-5-sonnet-20241022", "max_tokens": 300, "system": "You are a helpful assistant.", "messages": [{"role": "user", "content": "Explain the benefits of using Claude 3.5 Sonnet in Python."}]}
Response
json
{"id": "chatcmpl-xxx", "object": "chat.completion", "model": "claude-3-5-sonnet-20241022", "created": 1680000000, "choices": [{"index": 0, "message": {"role": "assistant", "content": ["Claude 3.5 Sonnet offers advanced natural language understanding and generation capabilities..."]}, "finish_reason": "stop"}], "usage": {"prompt_tokens": 50, "completion_tokens": 100, "total_tokens": 150}}
Extractresponse.content[0].text

Variants

Streaming response with Claude 3.5 Sonnet

Use streaming to display partial results immediately for better user experience with long responses.

python
import os
import anthropic

client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

system_prompt = "You are a helpful assistant."
user_message = "Tell me a story about a robot learning to code."

stream = client.messages.stream(
    model="claude-3-5-sonnet-20241022",
    max_tokens=500,
    system=system_prompt,
    messages=[{"role": "user", "content": user_message}]
)

print("Streaming response:")
for chunk in stream:
    print(chunk.content[0].text, end='', flush=True)
Async usage of Claude 3.5 Sonnet

Use async calls when integrating into asynchronous Python applications to improve concurrency.

python
import os
import asyncio
import anthropic

async def main():
    client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
    response = await client.messages.acreate(
        model="claude-3-5-sonnet-20241022",
        max_tokens=200,
        system="You are a helpful assistant.",
        messages=[{"role": "user", "content": "What is AI?"}]
    )
    print(response.content[0].text)

asyncio.run(main())
Using Claude 3.5 Haiku model for creative tasks

Use the haiku variant for poetry or creative writing tasks where style matters.

python
import os
import anthropic

client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

response = client.messages.create(
    model="claude-3-5-haiku-20241022",
    max_tokens=100,
    system="You are a creative poetry assistant.",
    messages=[{"role": "user", "content": "Write a haiku about spring."}]
)

print(response.content[0].text)

Performance

Latency~1.2 seconds for typical 200-token completions
Cost~$0.003 per 1000 tokens
Rate limitsTier 1: 300 requests per minute, 60,000 tokens per minute
  • Use concise prompts to reduce token usage.
  • Limit <code>max_tokens</code> to only what you need.
  • Reuse context efficiently by summarizing prior conversation.
ApproachLatencyCost/callBest for
Standard call~1.2s~$0.003/1k tokensGeneral purpose completions
StreamingStarts immediately, total ~1.2s~$0.003/1k tokensLong responses with better UX
Async call~1.2s (concurrent)~$0.003/1k tokensConcurrent or async apps

Quick tip

Always set a clear system prompt with <code>system=</code> to guide Claude 3.5 Sonnet's behavior effectively.

Common mistake

Beginners often forget to use the <code>system=</code> parameter and instead try to pass system instructions as a message, which Claude's SDK does not support.

Verified 2026-04 · claude-3-5-sonnet-20241022
Verify ↗