OverpayingForAIPricing desk

Lesson 7 of 8 · 11 min read · Beginner

API quickstart: your first call and what it costs

When the API beats the app, how to make your first call in curl and Python, how tokens are billed, and the two features — caching and batch — that cut the bill before you write real code.

In this lesson you will

  • Decide when to use the Claude API instead of the app
  • Make a first request with curl and the official Python SDK
  • Read the usage numbers in a response and price them

The API is the same Claude with a different meter and no interface. Use it when you want to automate a repeated task, build Claude into something, or when your usage is bursty enough that a subscription would bill idle weeks. Skip it if you just want to chat — a good client costs money and time, and Pro is simpler.

Three steps to a first call

  1. 1

    Get a key

    Create an account at the Anthropic Console, add a small amount of credit, and create an API key. Set a monthly spend limit while you are there; it is the cheapest insurance you will ever buy.

  2. 2

    Send a message

    Everything goes through one endpoint, POST /v1/messages. You choose a model, set a maximum output length, and send a list of messages.

  3. 3

    Read the usage

    Every response reports input and output tokens. Multiply by the model's rates and you know the cost of the call to the cent.

bashcurl — the minimum request
curl https://api.anthropic.com/v1/messages \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{
    "model": "claude-sonnet-5",
    "max_tokens": 1024,
    "messages": [
      {"role": "user", "content": "In three bullets, when is an API cheaper than a chat subscription?"}
    ]
  }'
pythonPython — official SDK (pip install anthropic)
import anthropic

client = anthropic.Anthropic()  # reads ANTHROPIC_API_KEY from the environment

response = client.messages.create(
    model="claude-sonnet-5",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "In three bullets, when is an API cheaper than a chat subscription?"}
    ],
)

for block in response.content:
    if block.type == "text":
        print(block.text)

u = response.usage
print(f"input={u.input_tokens} output={u.output_tokens}")
# Sonnet 5 at the time of writing: $2 / 1M input, $10 / 1M output
print(f"cost ≈ ${u.input_tokens * 2 / 1e6 + u.output_tokens * 10 / 1e6:.5f}")

That call costs a fraction of a cent. The usage object is the whole cost-control story: every optimisation in lesson 8 is about making those two numbers smaller, or making the input tokens cheaper.

Tokens and the context windowA token is roughly ¾ of an English word. The context window is the maximum tokens the model can hold at once — everything you send plus everything it writes.System prompt + instructionsConversation history + documentsNew answerfreeWhy it costs moneyEvery turn re-sends the whole history. A 40-message chat with a pasted PDF bills that PDF 40 times unless the provider caches it.“1M context” is a ceiling, not a target. Filling it on every request is the fastest way to overpay.rule of thumb: 1,000 tokens ≈ 750 words ≈ 1.5 pages
Figure 1.The API is stateless. If you want a conversation, you send the whole history every turn — and pay for it every turn.

Two features to know before you build anything

Prompt caching: stop paying full price for the same prefixIf the start of your request is identical each time (system prompt, docs, tool list), the provider can serve it from cache at a fraction of the input price.Request 1Stable prefix — written to cache (full price)new questionRequest 2Stable prefix — cache hit (discounted)new questionRequest 3Stable prefix — cache hit (discounted)new questionRule: put the unchanging parts first, the changing parts last. A timestamp at the top of a system prompt silently kills the cache on every call.
Figure 2.Prompt caching: mark the stable part of your request and Claude serves it from cache on later calls at roughly a tenth of the input rate.
  • Prompt caching. Add cache_control to the stable part of a request — the system prompt, a big document, the tool list — and repeated calls read it from cache at about 10% of the normal input price. On a workload that re-sends the same 50,000-token document, that is the difference between a bill and a rounding error. Details: Anthropic's prompt caching docs.
  • Message Batches. Send up to thousands of requests as one batch, get results within a day, pay half price. Anything that does not need an instant answer — nightly reports, bulk classification, evaluations — belongs here. Details: Batch processing docs.

Knowledge check

Your app sends the same 40,000-token policy document with every user question. What cuts the bill most?

Lesson FAQ

Is there a free API tier?

No. The API is prepaid or billed per token from the first call. Small amounts of credit go a long way at Haiku and Sonnet prices; a few dollars covers a serious amount of experimentation.

Which model id should I use?

At the time of writing the current ids are claude-sonnet-5, claude-opus-5, claude-haiku-4-5 and claude-fable-5-1. The Models API lists them live, and the site's Models page shows their current prices.

Can I use the API with the OpenAI SDK?

Anthropic provides an OpenAI-compatible endpoint for quick migration tests, but the official Anthropic SDKs expose caching, batches, tools and thinking properly. Use those for real work.

Finished reading?

Mark it done to track your progress through the course.

Compare, calculate, decide — for Claude

If our calculators helped you cut down on hidden AI wallet leaks, consider buying us a coffee. A tiny fraction of your savings keeps our pricing indexes updated daily.

Not sure which AI is cheapest for your use case? Find out in 30 seconds — no signup required.

AI cost intelligence

Stop overpaying for AI tools

Join the OverpayingForAI list for pricing updates, cheaper alternatives, and practical buying guidance.

Now tracking 50+ AI tools, models, platforms, subscriptions, coding tools, and automation products.

We use your email only for OverpayingForAI updates. Unsubscribe anytime.