Anthropic SDK Basics

Summary — Key Points of This Lesson

  • Anthropic provides official SDKs for Python and TypeScript. Installation is a single pip / npm command.
  • The SDK automatically handles authentication headers, retries, and type-safe response processing — no need to call raw REST endpoints.
  • The core API is client.messages.create(). Three fields are required: model, max_tokens, and messages.
  • If you set the ANTHROPIC_API_KEY environment variable, you do not need to specify the key in your code.
  • Use the official Anthropic GitHub repositories as the primary reference for code examples (Python / TypeScript).
目次 (7)

What Is an SDK?

An SDK (Software Development Kit) is a library that makes it easy to use an API. With Anthropic's official SDK, the library takes care of constructing HTTP requests, attaching authentication headers, handling errors, and managing retries on your behalf.

Official SDKs are provided for two languages — Python and TypeScript / JavaScript — and both are maintained by Anthropic source.

Installation

# Python
pip install anthropic

# TypeScript / JavaScript (Node.js)
npm install @anthropic-ai/sdk

Python — Minimal Code Example

The following is a minimal configuration based on the official documentation's code example source.

from anthropic import Anthropic

client = Anthropic()  # Automatically reads the ANTHROPIC_API_KEY environment variable

message = client.messages.create(
    model="claude-opus-4-7",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "こんにちは、Claude!"}
    ]
)

print(message.content[0].text)

TypeScript — Minimal Code Example

The following is a minimal configuration using TypeScript (ES Modules) source.

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

const client = new Anthropic();  // Automatically reads the ANTHROPIC_API_KEY environment variable

const message = await client.messages.create({
  model: "claude-opus-4-7",
  max_tokens: 1024,
  messages: [
    { role: "user", content: "こんにちは、Claude!" }
  ]
});

console.log(message.content);

Response Structure

The Message object returned by messages.create() contains the following main fields.

  • content — An array of text generation results. Normally, retrieve the body text via content[0].text.
  • model — The name of the model that was used.
  • stop_reason — The reason generation stopped ("end_turn" / "max_tokens" / "tool_use", etc.).
  • usage — The number of input and output tokens. Used for cost calculation.

Required Parameters Explained

Parameter Description
model The model ID to use. Example: "claude-opus-4-7"
max_tokens The maximum number of tokens to generate. Setting an upper limit is required for cost management.
messages An array of role ("user" / "assistant") and content pairs. Multiple turns can be passed as conversation history.

Detailed Article

For the full setup flow and practical examples, see "Getting Started with the Claude API — Python / TypeScript Examples in 5 Minutes".

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

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