Claude API Getting Started | The Fastest Way to Run It in 10 Lines of Python

For developers who want to integrate the Claude API into real-world applications, this guide covers the fastest path from issuing an API key to getting "Hello Claude" back in Python or TypeScript. We walk through the official SDK's minimal 10-line code, connectivity testing with curl, and key tips for keeping costs down with prompt caching and the Batch API — all in an order that prepares you for production deployment.

AI-Generated Article Summarypowered by Claude
結論powered by Claude

The Claude API starts working the moment you register an API key issued in the Anthropic Console as an environment variable and install the official SDK. With pip install anthropic and 10 lines of Python, you'll get "Hello Claude" back — laying the foundation for app integration in as little as 10 minutes.

The core of implementation lies in the parameter design of client.messages.create. Master model, max_tokens, and messages, and remember that the system prompt is passed as a top-level argument. The TypeScript SDK follows the same structure and installs directly on Node.js 18+ with npm install @anthropic-ai/sdk.

Operational costs can be compressed in two stages. For repeated system prompts, apply prompt caching for up to 90% off; for asynchronous processing, route it through the Batch API for a 50% discount. Since rate-limit and input-overflow exceptions are thrown as SDK-specific types, building in exponential backoff from the start is the safe play for production.

目次 (15)

Step 1: Issue an API Key and Set Up — Done in 3 Steps

Here is a summary of what this section covers:

  1. Go to console.anthropic.com and create an account
  2. Issue a new key from the "API Keys" menu on the left
  3. Set it as an environment variable: export ANTHROPIC_API_KEY=sk-ant-...

Creating a Console Account and Designing Your Workspace

You can sign up for the Anthropic Console using an email address, Google, or GitHub. For team development, it is best practice to separate Workspaces from the start — you can independently manage keys, rate limits, and billing for development, staging, and production, which prevents production keys from accidentally ending up in local scripts. For individual testing, the Default Workspace is perfectly fine. You receive free credits immediately after signing up, so verify things are working with those before switching to a paid plan.

Issuing an API Key and Registering It in Your Local Environment

On the "API Keys" screen, click "Create Key" and give it a descriptive name (e.g., local-dev-mac or staging-server). The key itself is only visible once, so save it immediately in a password manager or .env file. For local development with Bash/Zsh, add export ANTHROPIC_API_KEY=sk-ant-... to ~/.zshrc. For Node.js projects, the standard approach is .env + the dotenv package; for Python, use python-dotenv. Don't forget to add .env to .gitignore right away to keep it out of your repository.

Connectivity Check with curl (Before Installing the SDK)

Running a connectivity check with curl before installing the SDK makes it much easier to isolate issues that come up later.

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-4-6",
    "max_tokens": 64,
    "messages": [{"role":"user","content":"ping"}]
  }'

If you get a 200 back and a response in content, your network, key, and billing status are all clear. A 401 suggests a key problem, 429 suggests a rate limit, and 403 suggests billing hasn't been configured — check the Billing and Limits sections in the Console.

Step 2: Run "Hello, Claude!" in 10 Lines of Python

On Python 3.8 or later, one command — pip install anthropic — installs the official SDK. It is recommended to create a virtual environment first with python -m venv .venv && source .venv/bin/activate to keep your project clean. The Anthropic() class automatically reads the ANTHROPIC_API_KEY environment variable when called with no arguments, so you never need to hardcode keys in your code. The sample below is nearly identical to the minimal configuration shown in the official SDK README, and it serves directly as the foundation for a production app.

# Installation
# pip install anthropic

from anthropic import Anthropic

client = Anthropic()

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Hello, Claude!"}
    ]
)

print(message.content[0].text)

Key Parameters for messages.create

  • model: The model ID to use (e.g., claude-sonnet-4-6 / claude-opus-4-8 / claude-haiku-4-5-20251001)
  • max_tokens: Maximum number of output tokens. 256–1024 is sufficient for short responses
  • messages: A list of conversation history. role alternates between user and assistant
  • system: The system prompt. The correct approach is to pass it as a top-level argument, not inside messages
  • temperature: Closer to 0 is more deterministic; closer to 1 is more varied. Use 0 for code generation, around 0.7 for creative tasks

Reading the Response and Handling Errors

The return value message.content is an array where each element has the form {type: "text", text: "..."}. To extract the body text, message.content[0].text is sufficient. message.usage contains input_tokens and output_tokens, which you can use to estimate billing costs. When the rate limit is exceeded, anthropic.RateLimitError is raised; when the input is too large, anthropic.BadRequestError is raised. The standard production practice is to implement exponential backoff with something like tenacity and also catch APIConnectionError for network-related issues.

Step 3: Running It in TypeScript (Same 10-Line Structure)

The TypeScript side follows exactly the same philosophy as Python. Install with npm install @anthropic-ai/sdk (also works with pnpm add or bun add) and run on Node.js 18 or later. The SDK supports both ESM and CommonJS. If you use top-level await, enable "type": "module" in package.json or remember to wrap it in an async function.

// Installation
// npm install @anthropic-ai/sdk

import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic();

const message = await client.messages.create({
  model: "claude-sonnet-4-6",
  max_tokens: 1024,
  messages: [
    { role: "user", content: "Hello, Claude!" }
  ]
});

console.log(message.content);

Notes by Runtime: Node.js / Bun / Edge

  • Node.js 18+: Works out of the box. fetch is built in, so no additional polyfills are needed
  • Bun: Works with native ESM and starts up fast — great for CLI use cases
  • Cloudflare Workers / Vercel Edge: The SDK supports Edge runtimes. Register the API key as a Secret and retrieve it via bindings (e.g., env.ANTHROPIC_API_KEY) rather than process.env

Don't Call the API Directly from the Browser (Use a BFF Pattern Instead)

It is an absolute rule that API keys must always be held on the server side. If you call the Anthropic client directly from frontend JavaScript, the key will be fully visible in DevTools, leading to immediate leakage and a skyrocketing bill. Use a BFF layer — an API Route or Server Action in Next.js, +server.ts in SvelteKit, or a loader/action in Remix — and return only Claude's response to the browser. If you want to stream responses, convert them to SSE or a Web Stream on the server side before sending them.

Cost Reduction Tips — Up to 95% Savings with Prompt Caching + Batch API

For workloads that don't require immediate responses, combining prompt caching and the Batch API can compress costs to a fraction of the standard price. Let's review how each works, when to apply them, and how to combine them. Both are activated by adding just one line to the request, making them very low-effort to adopt.

Prompt Caching — Up to 90% Off for Reused Prefixes

When the same prompt prefix (system prompt, large reference documents, tool definitions, etc.) is reused across multiple requests, simply adding cache_control: {"type": "ephemeral"} to the target block enables caching. A good rule of thumb for estimation: cache hits get up to 90% off on input tokens, while cache writes cost 1.25x the standard price. The default TTL is 5 minutes — any request reusing the same prefix within that window gets a significant discount. Measuring usage.cache_read_input_tokens and cache_creation_input_tokens in the response to visualize hit rates will reveal opportunities to adjust cache placement.

Batch API — A Flat 50% Off for Async Processing, Ideal for Overnight Jobs

The Batch API accepts requests in bulk and returns results within 24 hours, offering a flat 50% discount on both input and output. Since you can submit large volumes of requests in a single job, it is well-suited for workloads where users don't need to wait immediately — tagging historical logs, summarizing a knowledge base, classifying internal documents, bulk-scoring evaluation datasets, and so on. You can also combine it with prompt caching: caching a shared system prompt while sending it through the Batch API further compresses the cost. The standard pattern is to submit jobs overnight and retrieve results by morning.

Model Selection — When to Use Haiku 4.5, Sonnet 4.6, and Opus 4.8

  • Haiku 4.5: Fastest and cheapest. Ideal for routine tasks like FAQ responses, classification, light summarization, and formatting
  • Sonnet 4.6: Balanced quality and cost. The most widely used as the primary model for application backends
  • Opus 4.8: Highest performance. Supports a 1M-token long context and is the top-tier flagship for complex reasoning, long-form generation, and the hardest final steps (model ID: claude-opus-4-8)

The recommended approach is to start with Sonnet 4.6 for everything, then move routine processing down to Haiku 4.5 and escalate only the difficult cases where quality falls short to Opus 4.8 — a three-tier strategy that minimizes total cost without waste.

Sources (Primary References)

The following primary sources were directly referenced in writing this article. Always check each link for the latest accurate information.

参考になったら ♡
Clauder Navi 編集部
@clauder_navi

Anthropic の Claude / Claude Code を中心に、日本のエンジニア向けに最新動向と実務 を毎日発信。 運営方針 は メディアについて をご覧ください。