Getting Started with Claude API in Python: A Practical Guide

Table of Contents

If you’re building AI-powered features into your Python applications, the Claude API offers one of the most capable language models available today. In this guide, you’ll learn how to set up the Anthropic Python SDK, send your first message, work with system prompts, and build multi-turn conversations.

Installing the Anthropic Python SDK

First, grab your API key from the Anthropic Console. Then install the SDK:

pip install anthropic

Set your API key as an environment variable (never hardcode it):

export ANTHROPIC_API_KEY="your-api-key-here"

Verify the installation:

import anthropic
client = anthropic.Anthropic()
print("SDK ready")

The client automatically reads ANTHROPIC_API_KEY from your environment.

Sending Your First Message to Claude

Here’s the simplest possible request:

import anthropic

client = anthropic.Anthropic()

message = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Explain Python decorators in 3 sentences."}
    ]
)

print(message.content[0].text)

Key parameters:

  • model: Choose between claude-sonnet-4-20250514 (balanced) or claude-opus-4-20250514 (most capable)
  • max_tokens: Maximum response length
  • messages: List of conversation turns

Using System Prompts for Consistent Behaviour

System prompts define Claude’s personality and constraints. They’re essential for production applications:

message = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    system="You are a senior Python developer. Give concise, practical answers with code examples. Avoid unnecessary explanations.",
    messages=[
        {"role": "user", "content": "How do I handle rate limiting in API requests?"}
    ]
)

System prompts are billed as input tokens but persist across the conversation without repetition.

Building Multi-Turn Conversations

Claude doesn’t maintain state between requests. You need to send the full conversation history:

conversation = []

def chat(user_message):
    conversation.append({"role": "user", "content": user_message})
    
    response = client.messages.create(
        model="claude-sonnet-4-20250514",
        max_tokens=1024,
        system="You are a helpful coding assistant.",
        messages=conversation
    )
    
    assistant_message = response.content[0].text
    conversation.append({"role": "assistant", "content": assistant_message})
    
    return assistant_message

# Usage
print(chat("What's a generator in Python?"))
print(chat("Show me a practical example."))
print(chat("How is that different from a list comprehension?"))

Each request includes all previous messages, so Claude has full context.

Real Use Case: Document Summarisation

Here’s a practical function for summarising documents:

def summarise_document(text, max_words=150):
    response = client.messages.create(
        model="claude-sonnet-4-20250514",
        max_tokens=512,
        system=f"Summarise documents in {max_words} words or less. Focus on key points and actionable information. Use bullet points.",
        messages=[
            {"role": "user", "content": f"Summarise this document:\n\n{text}"}
        ]
    )
    return response.content[0].text

# Example
long_document = open("report.txt").read()
summary = summarise_document(long_document)
print(summary)

For large documents, consider chunking and summarising in stages.

Real Use Case: Automated Code Review

This is where Claude shines. Here’s a code review function I use in my projects:

def review_code(code, language="python"):
    response = client.messages.create(
        model="claude-sonnet-4-20250514",
        max_tokens=2048,
        system="""You are a strict code reviewer. For each issue found:
1. State the line or section
2. Explain the problem
3. Provide the fix

Focus on: bugs, security issues, performance problems, and readability.
Skip minor style issues unless they affect maintainability.""",
        messages=[
            {"role": "user", "content": f"Review this {language} code:\n\n```{language}\n{code}\n```"}
        ]
    )
    return response.content[0].text

# Example
code_to_review = """
def get_user(id):
    query = f"SELECT * FROM users WHERE id = {id}"
    return db.execute(query)
"""

print(review_code(code_to_review))

Claude will catch the SQL injection vulnerability and suggest parameterised queries.

Handling Errors and Rate Limits

Always wrap API calls properly:

import anthropic
from time import sleep

def safe_request(messages, retries=3):
    for attempt in range(retries):
        try:
            return client.messages.create(
                model="claude-sonnet-4-20250514",
                max_tokens=1024,
                messages=messages
            )
        except anthropic.RateLimitError:
            sleep(2 ** attempt)  # Exponential backoff
        except anthropic.APIError as e:
            print(f"API error: {e}")
            raise
    raise Exception("Max retries exceeded")

Key Takeaways

  • Install with pip install anthropic and set ANTHROPIC_API_KEY in your environment
  • Use system prompts to control Claude’s behaviour consistently across requests
  • Maintain conversation history manually for multi-turn chats
  • Wrap API calls with retry logic for production reliability

The Claude API is straightforward to integrate, and the response quality makes it worth the effort. Start with simple use cases like summarisation, then expand to more complex workflows like code review pipelines or document Q&A systems.