CH 51 / 86Chapter 7: The API and Building Your Own Agents

API Basics

On this page9 items

In 10 seconds

The API is how you call Claude from your own programs. It is billed separately from the monthly plans, and you pay only for what you use.
Treat your key exactly like a password.

Up through Chapter 6, we talked about using finished products. From here on, you move to the building side.

Subscription or API?#

As explained in No.08, the monthly plans and the API are billed separately. Subscribing to one does not make the other free.

The decision is simple.

What to use
You use it yourself, interactivelyA monthly plan (the apps, Cowork, Claude Code)
A program calls it automaticallyThe API
You build it into your own service for others to useThe API
You run the same task in large volumesThe API

The dividing line is whether a person is waiting. If a person is in front of the screen, use a plan; if a program is waiting, use the API.

Also note that building your own agent with the Agent SDK requires authentication with an API key (No.54). Letting third-party products use your claude.ai login is, as a rule, not permitted.

Managing your key#

Get this right first. An API key is the same as a password.

Things you must not do:

  • Write it directly in your code
  • Commit it to Git
  • Put it on the client side (a browser or a smartphone app)

The third one is where accidents happen most. If you design your app so the browser calls Claude directly, your key is visible to users. Always call the API from the server side.

Reading the key from an environment variable is the standard approach.

text1 lines
export ANTHROPIC_API_KEY="sk-ant-..."

The SDKs for each language read this environment variable automatically, so the key never appears in your code.

Your first request#

Here is a Python example.

text12 lines
import anthropic

client = anthropic.Anthropic()   # Reads the key from the environment variable

message = client.messages.create(
    model="claude-sonnet-5",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Hello. What can you do?"}
    ],
)
print(message.content)

That’s all there is to it. There are three things to understand.

model: which model to use (No.07). A balanced model is fine to start with. Model names change, so keep this in your configuration rather than hard-coding it.

max_tokens: the maximum length of the reply. It is required. If it is too small, the reply gets cut off.

messages: the conversation history. The API does not remember conversations. If you want to continue, you send the entire previous exchange every time. This is the biggest difference from using the apps.

What “continuing a conversation” means#

You simply keep adding to messages.

text5 lines
messages = [
    {"role": "user", "content": "Hello"},
    {"role": "assistant", "content": "Hello. How can I help you?"},
    {"role": "user", "content": "Following up on what we discussed earlier..."},
]

You are sending everything, every time. That is why the input tokens grow as the conversation gets longer. The point from No.44 in Chapter 5, that “in a long session even a single line is expensive,” comes from this mechanism.

Limits#

The API has rate limits. They depend on your usage tier (Start / Build / Scale) and are defined by tokens and requests per minute.

If you hit one, don’t retry immediately; wait a little first. The SDKs include a built-in retry mechanism.

If you want to move up a tier, or need more than that, apply through the Console.

Try it: get your first request through (5 min)

Create a key in the Claude Console, put it in an environment variable, and run the code.

bash pip install anthropic export ANTHROPIC_API_KEY="(the key you created)"

Save the Python example above as hello.py and run it.

Then change max_tokens to 50 and run it again.

Expected result: The second run is cut off partway, and stop_reason is max_tokens.

If it doesn’t work: If you get an authentication error, the environment variable isn’t being read. Check it with echo $ANTHROPIC_API_KEY.

Common pitfalls#

Writing the key in your code. The most common accident. Use an environment variable.

Assuming the API remembers the conversation. The API is stateless. You build up the history yourself.

Setting max_tokens too low. The reply gets cut off. You can tell whether it was cut off from stop_reason.

Confusing plan usage limits with API rate limits. They are different things (No.08).

Related pages#

Summary#

  • Monthly plans and the API are billed separately. The dividing line is “is a person waiting?”
  • A key is the same as a password. Read it from an environment variable and never put it on the client side
  • The API does not remember conversations. Build up and send the history every time
  • max_tokens is required. If a reply is cut off, check stop_reason

Read next#

Key API Features