DEV Community

brian austin
brian austin

Posted on

How to use Claude API from Python in 15 lines (and why it costs $2/month)

How to use Claude API from Python in 15 lines

You don't need a $20/month subscription to use Claude. You need 15 lines of Python and $2/month.

Here's the complete working code:

import anthropic

client = anthropic.Anthropic(
    api_key="your-api-key-here"  # get from simplylouie.com/developers
)

message = client.messages.create(
    model="claude-opus-4-5",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Explain async/await in Python in 3 sentences."}
    ]
)

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

That's it. That's the whole API call.

What you get back

# The response object
print(message.model)           # claude-opus-4-5
print(message.stop_reason)    # end_turn
print(message.usage.input_tokens)   # 22
print(message.usage.output_tokens)  # 87
print(message.content[0].text) # the actual answer
Enter fullscreen mode Exit fullscreen mode

Add streaming (for long responses)

import anthropic

client = anthropic.Anthropic(api_key="your-api-key-here")

with client.messages.stream(
    model="claude-opus-4-5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Write a Python function to parse CSV files."}]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
Enter fullscreen mode Exit fullscreen mode

Add conversation history (multi-turn)

import anthropic

client = anthropic.Anthropic(api_key="your-api-key-here")

conversation = []

def chat(user_message):
    conversation.append({"role": "user", "content": user_message})

    response = client.messages.create(
        model="claude-opus-4-5",
        max_tokens=1024,
        messages=conversation
    )

    assistant_message = response.content[0].text
    conversation.append({"role": "assistant", "content": assistant_message})

    return assistant_message

# Use it
print(chat("What's the difference between a list and a tuple in Python?"))
print(chat("Can you show me an example of each?"))
print(chat("Which one should I use for database rows?"))
Enter fullscreen mode Exit fullscreen mode

System prompts (set the AI's behavior)

import anthropic

client = anthropic.Anthropic(api_key="your-api-key-here")

response = client.messages.create(
    model="claude-opus-4-5",
    max_tokens=1024,
    system="You are a senior Python developer. Always include type hints. Always include error handling. Be concise.",
    messages=[
        {"role": "user", "content": "Write a function to read a JSON file."}
    ]
)

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

Output:

import json
from pathlib import Path
from typing import Any

def read_json_file(file_path: str | Path) -> Any:
    """Read and parse a JSON file."""
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            return json.load(f)
    except FileNotFoundError:
        raise FileNotFoundError(f"JSON file not found: {file_path}")
    except json.JSONDecodeError as e:
        raise ValueError(f"Invalid JSON in {file_path}: {e}")
Enter fullscreen mode Exit fullscreen mode

Async version (for FastAPI / async codebases)

import asyncio
import anthropic

client = anthropic.AsyncAnthropic(api_key="your-api-key-here")

async def ask_claude(question: str) -> str:
    response = await client.messages.create(
        model="claude-opus-4-5",
        max_tokens=1024,
        messages=[{"role": "user", "content": question}]
    )
    return response.content[0].text

async def main():
    answer = await ask_claude("What is the GIL in Python?")
    print(answer)

asyncio.run(main())
Enter fullscreen mode Exit fullscreen mode

Install

pip install anthropic
Enter fullscreen mode Exit fullscreen mode

That's the only dependency. No LangChain required. No complex setup.

The $2/month part

The Anthropic API has its own pricing (per token), but SimplyLouie offers flat-rate API access at $2/month — same Claude models, fixed cost, no token math.

For developers who don't want to track token usage: you get a standard API key, it works with the code above, and the bill is $2/month regardless of how much you use it within the fair-use limit.

# Same code, SimplyLouie key
curl https://api.simplylouie.com/v1/messages \
  -H 'Content-Type: application/json' \
  -H 'x-api-key: YOUR_SIMPLYLOUIE_KEY' \
  -d '{
    "model": "claude-opus-4-5",
    "max_tokens": 1024,
    "messages": [{"role": "user", "content": "Hello"}]
  }'
Enter fullscreen mode Exit fullscreen mode

What's the actual difference between direct API and SimplyLouie?

Anthropic Direct SimplyLouie
Cost ~$3-15/month depending on usage $2/month flat
Billing Per token Fixed monthly
Models All Anthropic models Claude Sonnet/Haiku
Setup Credit card + API key Same
Good for High-volume production Learning, side projects, prototypes

Quick reference

# Minimal working example
from anthropic import Anthropic
client = Anthropic(api_key="sk-...")
result = client.messages.create(
    model="claude-opus-4-5",
    max_tokens=256,
    messages=[{"role": "user", "content": "Your question here"}]
)
print(result.content[0].text)
Enter fullscreen mode Exit fullscreen mode

That's the whole thing. 8 lines. The rest is optional.


If you're building something with Claude API and want flat-rate access without token math: simplylouie.com/developers

Top comments (0)