DEV Community

Sangmin Lee
Sangmin Lee

Posted on • Originally published at claudeguide.io

Claude API Python Tutorial: Complete Guide with Code Examples

Originally published at claudeguide.io/claude-api-python-tutorial

Claude API Python Tutorial: Complete Guide with Code Examples

The Claude API Python SDK (2026) (anthropic) lets you call Claude models from any Python script or application with a single pip install. This guide covers every major feature — messages, system prompts, multi-turn conversations, streaming, tool use, prompt caching, and async — with working code you can paste and run today.


Installation and environment setup

Install the SDK and python-dotenv for managing your API key securely:

pip install anthropic python-dotenv
Enter fullscreen mode Exit fullscreen mode

Store your key in a .env file in your project root:

ANTHROPIC_API_KEY=sk-ant-...
Enter fullscreen mode Exit fullscreen mode

Load it at the top of any script:

from dotenv import load_dotenv
import os

load_dotenv()
# ANTHROPIC_API_KEY is now available as an environment variable
# The Anthropic client picks it up automatically
Enter fullscreen mode Exit fullscreen mode

The Anthropic() client reads ANTHROPIC_API_KEY from the environment by default, so you never need to pass it explicitly in your code.


Basic messages.create() call

The messages.create() method is the core of the API. Every request sends a list of messages and receives a response object:

import anthropic

client = anthropic.Anthropic()

message = client.messages.create(
    model="claude-sonnet-4-5",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Explain the difference between a list and a tuple in Python."}
    ]
)

print(message.content[0].text)
Enter fullscreen mode Exit fullscreen mode

The response object contains:

  • message.content — a list of content blocks (usually one TextBlock)
  • message.usage.input_tokens and message.usage.output_tokens — token counts for cost tracking
  • message.stop_reason"end_turn", "max_tokens", or "tool_use"

System prompts

A system prompt sets the model's persona and constraints. Pass it as the system parameter (not as a message):

message = client.messages.create(
    model="claude-sonnet-4-5",
    max_tokens=1024,
    system="You are a senior Python engineer. Respond with concise, production-quality code. Include type hints. No explanatory prose unless asked.",
    messages=[
        {"role": "user", "content": "Write a function that retries a callable with exponential backoff."}
    ]
)

print(message.content[0].text)
Enter fullscreen mode Exit fullscreen mode

A well-crafted system prompt reduces output tokens and improves consistency across requests. See How to Write System Prompts for Claude for patterns that work.


Multi-turn conversations

The API is stateless — you maintain conversation history yourself by appending each turn to the messages list:


python
import anthropic

client = anthropic.Anthropic()

conversation = []

def chat(user_message: str) -

[→ Get the Agent SDK Cookbook — $49](https://shoutfirst.gumroad.com/l/ogxhmy?utm_source=claudeguide&utm_medium=article&utm_campaign=claude-api-python-tutorial)

*30-day money-back guarantee. Instant download.*
Enter fullscreen mode Exit fullscreen mode

Top comments (0)