O
Octo
O
Octo
CoursesPricingDashboardPrivacyTerms

© 2026 Octo

Claude Foundations
1Meet Claude2Getting Started with Claude3Claude Code: Your AI Coding Partner4Prompt Engineering for Claude5Claude at Work6Extended Thinking & Deep Reasoning7Building with the Claude API8Claude Best Practices & Safety
Module 7

Building with the Claude API

Go from user to builder. Learn how the Claude API works, make your first API call, and understand how developers integrate Claude into their own products.

From using Claude to building with Claude

You've been using Claude through the web interface — typing prompts, getting responses. But what if you want Claude to power a feature in your own app? An internal tool that summarizes customer tickets automatically. A Slack bot that answers employee questions from your company wiki. A product feature that generates personalized recommendations.

That's what the Claude API is for. It lets developers send prompts to Claude programmatically and get responses back — turning Claude from a tool you use into a tool you build with.

🔑Why this matters even for non-developers
Even if you'll never write API code yourself, understanding how the Claude API works helps you: (1) have better conversations with your engineering team about AI features, (2) evaluate what's possible to build with Claude, and (3) understand the cost and performance trade-offs of AI-powered products.

How the API works

The Claude API is a REST API — you send an HTTP request with your prompt, and Claude sends back a response. Every interaction with Claude, whether through the web interface, Claude Code, or a custom app, ultimately goes through this API.

1. Your App sends a message (with your API key) to api.anthropic.com

2. Claude API receives the request and routes it to the model

3. Claude Model processes the prompt and generates a response

4. Response is sent back to your app to display or use

The flow is simple:

  1. Your app sends a message (with your API key) to api.anthropic.com
  2. Claude processes the prompt and generates a response
  3. The response is sent back to your app
  4. Your app displays or uses the response however it wants

Key API concepts

Messages API

The core API is the Messages API. You send a list of messages (conversation turns) and Claude responds:

import anthropic

client = anthropic.Anthropic()

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Explain API rate limiting in one paragraph."}
    ]
)

print(message.content[0].text)

That's it. Five lines to send a prompt and get a response.

Models

You choose which Claude model to use in every API call:

ModelAPI nameBest forRelative cost
Claude Opusclaude-opus-4-6Complex analysis, nuanced tasks$$$
Claude Sonnetclaude-sonnet-4-6Balanced everyday use$$
Claude Haikuclaude-haiku-4-5-20251001Fast, simple tasks$

System prompts

System prompts set persistent instructions for the entire conversation:

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    system="You are a customer support agent for Acme Corp. Be friendly, "
           "concise, and always offer to escalate to a human if you can't "
           "help. Never make up product features.",
    messages=[
        {"role": "user", "content": "How do I reset my password?"}
    ]
)

The system prompt shapes Claude's behavior for every response — it's the API equivalent of Project instructions.

Multi-turn conversations

Send conversation history to maintain context:

messages = [
    {"role": "user", "content": "I'm building a task management app."},
    {"role": "assistant", "content": "Great! What technology stack are you using?"},
    {"role": "user", "content": "React frontend, Python FastAPI backend."},
    {"role": "assistant", "content": "Solid choice. What feature are you working on?"},
    {"role": "user", "content": "I need to add recurring tasks. How should I model this?"}
]

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=2048,
    messages=messages
)

Claude sees the full conversation history and responds with context.

There Are No Dumb Questions

Do I need to send the entire conversation history every time?

Yes. The API is stateless — Claude doesn't remember previous calls. You send the full message history with each request. This gives you complete control over what Claude "remembers."

How much does it cost?

You pay per token (roughly per word). Prices vary by model — Haiku is cheapest, Opus is most expensive. A typical API call processing a few paragraphs costs fractions of a cent. Check Anthropic's pricing page for current rates.

Can I use the API for free?

Anthropic sometimes offers free trial credits for new accounts. After that, it's pay-as-you-go. The cost is very low for moderate usage — many developers spend less than $10/month during development.

Token economics

Understanding tokens is essential for API usage:

What is a token? — A word-piece. "Hello" is 1 token. "Anthropic" is 2-3 tokens. Roughly 1 token ≈ 4 characters of English text.

Input tokens — What you send (your prompt + conversation history). You pay for these.

Output tokens — What Claude generates (the response). These cost more than input tokens.

Max tokens — You set a cap on how long Claude's response can be. This controls cost and response time.

Context window — Claude's total capacity: input + output combined. 200K tokens for the latest models.

200KMax context window (tokens)

Cost optimization tips:

  • Use Haiku for simple tasks (classification, extraction)
  • Use Sonnet for most tasks (it's the best value)
  • Reserve Opus for truly complex analysis
  • Set max_tokens to limit response length
  • Keep conversation histories trimmed — only include relevant turns

Tool use (function calling)

One of the API's most powerful features: Claude can use tools. You define functions, and Claude decides when and how to call them:

tools = [
    {
        "name": "get_weather",
        "description": "Get current weather for a location",
        "input_schema": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "City and state, e.g. San Francisco, CA"
                }
            },
            "required": ["location"]
        }
    }
]

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    tools=tools,
    messages=[{"role": "user", "content": "What's the weather in Tokyo?"}]
)

Claude responds with a tool call request. Your app executes the function, sends the result back, and Claude incorporates it into its response. This lets Claude interact with databases, APIs, file systems — anything you can write a function for.

💡Tools unlock real applications
Without tools, Claude can only work with information in the conversation. With tools, Claude can look up live data, create records, send notifications, and take real actions. This is what separates a chatbot from an AI agent.

Streaming

For real-time applications, use streaming to display Claude's response as it's generated:

with client.messages.stream(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Write a product announcement."}]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

Streaming makes your app feel responsive — users see words appearing in real time instead of waiting for the full response.

Real-world API use cases

Use caseHow it worksModel recommendation
Customer support botSystem prompt with company knowledge + tool use for ticket lookupSonnet
Document summarizerUpload docs → summarize with structured outputSonnet or Haiku
Code review toolAnalyze PRs, flag issues, suggest improvementsSonnet
Content generatorSystem prompt for brand voice, structured templatesSonnet
Data classifierCategorize inputs into predefined bucketsHaiku (fast + cheap)
Research assistantExtended thinking + tool use for web searchOpus

⚡

Design an API-powered feature

50 XP
Think of a product or internal tool that would benefit from Claude integration. Design the feature by answering: 1. What does the user do? (Input) 2. What does Claude do? (Processing) 3. What does the user see? (Output) 4. Which model would you use and why? 5. Would you need tool use? For what?

Getting started with the API

Step 1: Create an account — Sign up at console.anthropic.com

Step 2: Get your API key — Generate a key in the dashboard. Store it securely — never commit it to code.

Step 3: Install the SDK — pip install anthropic (Python) or npm install @anthropic-ai/sdk (TypeScript)

Step 4: Make your first call — Use the code examples above to send your first prompt

Step 5: Build something real — Start with a simple use case and iterate

⚠️API key security
Your API key is like a password. Never share it, never commit it to version control, never put it in client-side code. Use environment variables or a secrets manager. If a key is compromised, rotate it immediately in the Anthropic dashboard.

?

Knowledge Check

1.What is the Claude API?

2.Why do you need to send the full conversation history with each API call?

3.What does 'tool use' (function calling) enable Claude to do?

4.Which model offers the best balance of capability and cost for most API use cases?

Previous

Extended Thinking & Deep Reasoning

Next

Claude Best Practices & Safety