CLAUDE.md Python Anthropic SDK ai-agent

Anthropic Python SDK — CLAUDE.md

Anthropic Python SDK公式CLAUDE.md。同期/非同期クライアント、ストリーミング、ツール使用、ビジョン、プロンプトキャッシュ。

CLAUDE.md · 101 lines
# Anthropic Python SDK — CLAUDE.md

## Overview

The official Python library for the Anthropic API. Supports Claude claude-opus-4-5, Claude claude-opus-4-5, and all Claude models.

## Installation

```bash
pip install anthropic
```

## Basic Usage

```python
import anthropic

client = anthropic.Anthropic()  # Uses ANTHROPIC_API_KEY env var

message = client.messages.create(
    model="claude-opus-4-5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Explain quantum entanglement."}],
)
print(message.content[0].text)
```

## Streaming

```python
with client.messages.stream(
    model="claude-opus-4-5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Write a poem."}],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
```

## Tool Use

```python
tools = [{
    "name": "get_weather",
    "description": "Get weather for a location.",
    "input_schema": {
        "type": "object",
        "properties": {
            "location": {"type": "string"},
        },
        "required": ["location"],
    },
}]

response = client.messages.create(
    model="claude-opus-4-5",
    tools=tools,
    messages=[{"role": "user", "content": "What's the weather in Paris?"}],
)
```

## Async Client

```python
import asyncio
from anthropic import AsyncAnthropic

async def main():
    client = AsyncAnthropic()
    response = await client.messages.create(
        model="claude-opus-4-5",
        max_tokens=100,
        messages=[{"role": "user", "content": "Hi!"}],
    )
    print(response.content[0].text)

asyncio.run(main())
```

## Prompt Caching

```python
# Mark large context blocks for caching
response = client.messages.create(
    model="claude-opus-4-5",
    system=[{
        "type": "text",
        "text": large_context,
        "cache_control": {"type": "ephemeral"}
    }],
    messages=[...],
)
```

## Development

```bash
pip install -e ".[dev]"
pytest
mypy anthropic
```
Share on X

こちらもおすすめ

ai-agent カテゴリの他のルール

もっとルールを探す

CLAUDE.md、.cursorrules、AGENTS.md、Image Prompts の全 223 ルールをチェック。