Skip to content

davila7/claude-code-templates / cli-tool/components/skills/development/claude-api

claude-api

Unverified

Build, debug, and optimize Claude API / Anthropic SDK apps. Apps built with this skill should include prompt caching. Also handles migrating existing Claude API code between Claude model versions (4.5 → 4.6, 4.6 → 4.7, retired-model replacements). TRIGGER when: code imports `anthropic`/`@anthropic-ai/sdk`; user asks for the Claude API, Anthropic SDK, or Managed Agents; user adds/modifies/tunes a Claude feature (caching, thinking, compaction, tool use, batch, files, citations, memory) or model (Opus/Sonnet/Haiku) in a file; questions about prompt caching / cache hit rate in an Anthropic SDK project. SKIP: file imports `openai`/other-provider SDK, filename like `*-openai.py`/`*-generic.py`, provider-neutral code, general programming/ML.

FileKindSize
LICENSE.txtmarkdown11.1 KB
SKILL.mdskill md32.3 KB
csharp/claude-api.mdmarkdown14.1 KB
curl/examples.mdmarkdown6.3 KB
curl/managed-agents.mdmarkdown7.2 KB
go/claude-api.mdmarkdown13.5 KB
go/managed-agents/README.mdmarkdown18.0 KB
java/claude-api.mdmarkdown14.7 KB
java/managed-agents/README.mdmarkdown16.2 KB
php/claude-api.mdmarkdown10.9 KB
php/managed-agents/README.mdmarkdown12.9 KB
python/agent-sdk/README.mdmarkdown12.4 KB
python/agent-sdk/patterns.mdmarkdown9.0 KB
python/claude-api/README.mdmarkdown11.9 KB
python/claude-api/batches.mdmarkdown5.0 KB
python/claude-api/files-api.mdmarkdown3.9 KB
python/claude-api/streaming.mdmarkdown5.1 KB
python/claude-api/tool-use.mdmarkdown16.5 KB
python/managed-agents/README.mdmarkdown9.6 KB
ruby/claude-api.mdmarkdown2.9 KB
ruby/managed-agents/README.mdmarkdown9.9 KB
shared/agent-design.mdmarkdown8.1 KB
shared/error-codes.mdmarkdown7.5 KB
shared/live-sources.mdmarkdown16.2 KB
shared/managed-agents-api-reference.mdmarkdown23.2 KB
shared/managed-agents-client-patterns.mdmarkdown9.2 KB
shared/managed-agents-core.mdmarkdown12.8 KB
shared/managed-agents-environments.mdmarkdown10.4 KB
shared/managed-agents-events.mdmarkdown9.4 KB
shared/managed-agents-memory.mdmarkdown9.4 KB
shared/managed-agents-multiagent.mdmarkdown6.4 KB
shared/managed-agents-onboarding.mdmarkdown7.9 KB
shared/managed-agents-outcomes.mdmarkdown6.0 KB
shared/managed-agents-overview.mdmarkdown9.0 KB
shared/managed-agents-tools.mdmarkdown13.2 KB
shared/managed-agents-webhooks.mdmarkdown4.9 KB
shared/model-migration.mdmarkdown64.0 KB
shared/models.mdmarkdown7.4 KB
shared/prompt-caching.mdmarkdown9.6 KB
shared/tool-use-concepts.mdmarkdown16.4 KB
typescript/agent-sdk/README.mdmarkdown10.6 KB
typescript/agent-sdk/patterns.mdmarkdown4.6 KB
typescript/claude-api/README.mdmarkdown9.7 KB
typescript/claude-api/batches.mdmarkdown2.5 KB
typescript/claude-api/files-api.mdmarkdown2.2 KB
typescript/claude-api/streaming.mdmarkdown5.5 KB
typescript/claude-api/tool-use.mdmarkdown15.0 KB
typescript/managed-agents/README.mdmarkdown9.2 KB

python/claude-api/files-api.md

View on GitHub
# Files API — Python

The Files API uploads files for use in Messages API requests. Reference files via `file_id` in content blocks, avoiding re-uploads across multiple API calls.

**Beta:** Pass `betas=["files-api-2025-04-14"]` in your API calls (the SDK sets the required header automatically).

## Key Facts

- Maximum file size: 500 MB
- Total storage: 100 GB per organization
- Files persist until deleted
- File operations (upload, list, delete) are free; content used in messages is billed as input tokens
- Not available on Amazon Bedrock or Google Vertex AI

---

## Upload a File

```python
import anthropic

client = anthropic.Anthropic()

uploaded = client.beta.files.upload(
    file=("report.pdf", open("report.pdf", "rb"), "application/pdf"),
)
print(f"File ID: {uploaded.id}")
print(f"Size: {uploaded.size_bytes} bytes")
```

---

## Use a File in Messages

### PDF / Text Document

```python
response = client.beta.messages.create(
    model="claude-opus-4-7",
    max_tokens=16000,
    messages=[{
        "role": "user",
        "content": [
            {"type": "text", "text": "Summarize the key findings in this report."},
            {
                "type": "document",
                "source": {"type": "file", "file_id": uploaded.id},
                "title": "Q4 Report",           # optional
                "citations": {"enabled": True}   # optional, enables citations
            }
        ]
    }],
    betas=["files-api-2025-04-14"],
)
for block in response.content:
    if block.type == "text":
        print(block.text)
```

### Image

```python
image_file = client.beta.files.upload(
    file=("photo.png", open("photo.png", "rb"), "image/png"),
)

response = client.beta.messages.create(
    model="claude-opus-4-7",
    max_tokens=16000,
    messages=[{
        "role": "user",
        "content": [
            {"type": "text", "text": "What's in this image?"},
            {
                "type": "image",
                "source": {"type": "file", "file_id": image_file.id}
            }
        ]
    }],
    betas=["files-api-2025-04-14"],
)
```

---

## Manage Files

### List Files

```python
files = client.beta.files.list()
for f in files.data:
    print(f"{f.id}: {f.filename} ({f.size_bytes} bytes)")
```

### Get File Metadata

```python
file_info = client.beta.files.retrieve_metadata("file_011CNha8iCJcU1wXNR6q4V8w")
print(f"Filename: {file_info.filename}")
print(f"MIME type: {file_info.mime_type}")
```

### Delete a File

```python
client.beta.files.delete("file_011CNha8iCJcU1wXNR6q4V8w")
```

### Download a File

Only files created by the code execution tool or skills can be downloaded (not user-uploaded files).

```python
file_content = client.beta.files.download("file_011CNha8iCJcU1wXNR6q4V8w")
file_content.write_to_file("output.txt")
```

---

## Full End-to-End Example

Upload a document once, ask multiple questions about it:

```python
import anthropic

client = anthropic.Anthropic()

# 1. Upload once
uploaded = client.beta.files.upload(
    file=("contract.pdf", open("contract.pdf", "rb"), "application/pdf"),
)
print(f"Uploaded: {uploaded.id}")

# 2. Ask multiple questions using the same file_id
questions = [
    "What are the key terms and conditions?",
    "What is the termination clause?",
    "Summarize the payment schedule.",
]

for question in questions:
    response = client.beta.messages.create(
        model="claude-opus-4-7",
        max_tokens=16000,
        messages=[{
            "role": "user",
            "content": [
                {"type": "text", "text": question},
                {
                    "type": "document",
                    "source": {"type": "file", "file_id": uploaded.id}
                }
            ]
        }],
        betas=["files-api-2025-04-14"],
    )
    print(f"\nQ: {question}")
    text = next((b.text for b in response.content if b.type == "text"), "")
    print(f"A: {text[:200]}")

# 3. Clean up when done
client.beta.files.delete(uploaded.id)
```
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 319kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 319kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 319kUpdated Claude CodeCodexVerifiedInstall
skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy.Stars: 196kUpdated Claude CodeCodexVerifiedInstall