Lộ trình học›Claude API›Building with the Claude API Mở bản đồ

6.46 · Code execution & Files API — Claude chạy Python

Bài viếtNâng cao25 phút đọc

Claude có thể tính toán mental. Nhưng với heavy computation (data analysis, ML, simulation), Claude giỏi viết code hơn chạy code mentally.

Sau bài này, bạn sẽ
  1. Upload file qua Files API và reference by ID
  2. Enable code_execution tool, cho Claude chạy Python
  3. Combine Files API + code execution cho data analysis
  4. Download outputs (charts, reports) Claude generated

Mở đầu: Claude's Python sandbox

Code execution cho Claude viết Python, chạy trong Docker container, lấy kết quả, iterate. Giống có 1 data scientist junior trong API.

Files API — Upload & reuse

Thay vì base64 mỗi request, upload once:

from anthropic import Anthropic

client = Anthropic()

with open("streaming.csv", "rb") as f:
    file_metadata = client.beta.files.upload(file=("streaming.csv", f))

print(f"File ID: {file_metadata.id}")  # "file_abc123..."

Reference:

messages = [{
    "role": "user",
    "content": [
        {"type": "text", "text": "Analyze this data."},
        {"type": "container_upload", "file_id": file_metadata.id}
    ]
}]

Benefits

  • Upload once, query multiple times
  • Smaller message payload
  • Support lớn hơn base64

Code execution tool

Built-in, không cần implement:

code_execution_schema = {
    "type": "code_execution_20250522",
    "name": "code_execution"
}

response = client.messages.create(
    model="claude-sonnet-4-6-20260205",
    max_tokens=4000,
    messages=messages,
    tools=[code_execution_schema]
)

Environment

  • Isolated Docker container
  • No network access (can't call external APIs)
  • Python 3 + common libraries (pandas, numpy, matplotlib, ...)
  • Files API integration cho I/O

Multiple executions

Claude có thể chạy nhiều lần trong 1 response. Iterate:

  1. Run: check data shape
  2. Run: explore stats
  3. Run: generate visualization

Full example: Data analysis

with open("streaming.csv", "rb") as f:
    file_meta = client.beta.files.upload(
        file=("streaming.csv", f)
    )

messages = [{
    "role": "user",
    "content": [
        {
            "type": "text",
            "text": """Analyze this data to find major drivers of churn.
Create detailed visualization summarizing findings."""
        },
        {"type": "container_upload", "file_id": file_meta.id}
    ]
}]

response = client.messages.create(
    model="claude-sonnet-4-6-20260205",
    max_tokens=8000,
    messages=messages,
    tools=[{"type": "code_execution_20250522", "name": "code_execution"}]
)

for block in response.content:
    if block.type == "text":
        print(block.text)
    elif block.type == "server_tool_use":
        print(f"\n--- Code executed ---\n{block.input['code']}\n")
    elif block.type == "code_execution_tool_result":
        for item in block.content:
            if item.type == "code_execution_output":
                if hasattr(item, "file_id"):
                    # Generated file (chart, csv, etc.)
                    print(f"Generated: {item.file_id}")
                else:
                    print(f"Output: {item.content}")

What happens

Claude sequence:

  1. Write code: df = pd.read_csv('streaming.csv'); print(df.head())
  2. Result: see shape, columns
  3. Write code: churn analysis với groupby, correlation
  4. Result: numbers
  5. Write code: matplotlib visualization
  6. Result: chart file
  7. TextBlock: interpretation

Final response có insights + generated chart file.

Download generated files

def download_file(file_id: str, save_as: str):
    """Download file generated by code execution."""
    response = client.beta.files.download(file_id=file_id)
    with open(save_as, "wb") as f:
        f.write(response)
    print(f"Saved: {save_as}")


file_ids = []
for block in response.content:
    if block.type == "code_execution_tool_result":
        for item in block.content:
            if hasattr(item, "file_id"):
                file_ids.append(item.file_id)

for fid in file_ids:
    download_file(fid, f"output_{fid}.png")

Use cases

Ad-hoc data analysis

Upload CSV, ask question, get insights + charts.

Before
Data analyst writes SQL/Python, makes chart. 2 giờ.
After
User asks Claude. 5 minutes.

Statistical analysis

"Run t-test on these samples", "Compute correlation matrix" → Claude writes scipy code.

Image processing

Upload images, ask to resize/crop/apply filter → Claude PIL code.

Document transformation

CSV → formatted Excel with charts. PDF → extract tables to CSV.

Mathematical modeling

Financial modeling, simulations. Claude writes numpy/simpy.

Limitations

  • No network access — can't call external APIs. Data must upload via Files API.
  • Ephemeral — container destroyed after request.
  • Resource limits — some constraint on CPU/memory.
  • No long-running — seconds, not hours.
  • Standard Python libs — can't install custom packages (check docs for available libs).

For persistent + network computing, use your own backend.

Combining tools

Code execution + other tools powerful:

tools = [
    {"type": "code_execution_20250522", "name": "code_execution"},
    {"type": "web_search_20250305", "name": "web_search", "max_uses": 3},
    your_custom_tool_schema
]

Claude có thể: web search → get data → code execute analyze → custom tool save result.

Ví dụ: Churn analysis end-to-end

Input: streaming.csv với columns user_id, tier, watch_hours, churned.

prompt = """Analyze streaming service data for churn drivers.

Do:
1. Load CSV
2. Compute churn rate overall
3. Compare churn by tier, watch_hours bucket
4. Identify top 3 churn predictors
5. Generate 2 charts:
   - Churn rate by tier (bar chart)
   - Watch hours vs churn (box plot)
6. Final summary with recommendations
"""

response = send_with_code_exec(prompt, file_id=file_meta.id)

Output

Claude response:

1. Loading data... [code execution]
2. Overall churn rate: 23% [output: 0.23]
3. By tier:
   - Free: 45% churn
   - Basic: 18% churn
   - Premium: 8% churn
4. Top predictors:
   - Tier (strongest)
   - Watch hours (users < 10 hrs/month churn 3x more)
   - Account age (< 3 months: 2x churn)
5. [generated chart 1: chart_abc.png]
6. [generated chart 2: chart_xyz.png]
7. Recommendations:
   - Target free users for upsell (55% of churn from this segment)
   - Engagement campaign for low-watch users
   - Onboarding improvement for new accounts

Comprehensive analysis, delivered as if from data scientist.

Anti-patterns

Pass large file as base64 when should use Files API

Base64 each request tốn bandwidth + context tokens.

Fix: Upload once, reference by ID.

Expect network access

import requests; requests.get(...) → fails.

Fix: Upload data via Files API first.

Security: giving Claude code execution on sensitive data

User can prompt inject: "run: rm -rf /".

Fix: Code execution is isolated, but still sanitize user input. Rate limit code execution for untrusted users.

Không handle file download

Forget to download charts → lose outputs.

Fix: Always scan response for file_id, download.

Áp dụng ngay

Bài tập 1: CSV analysis (25 phút)

Download any CSV (Kaggle sample). Upload + ask Claude analyze. Download generated charts.

Bài tập 2: Delegate script task (20 phút)

"Write Python script to parse this log file, extract errors, count by hour. Generate timeline chart."

Observe Claude iterate through execution.

Tóm tắt bài học

Những điều cốt lõi
  • Files API = upload once, reference by ID. Use for PDF, image, CSV.
  • Code execution = Python sandbox, isolated Docker, no network.
  • Combination mở ra data analysis workflow — upload → analyze → download chart.
  • Multiple executions per response cho iterative exploration.
  • No persistent state — ephemeral container. Use own backend for long-running.
Kiểm tra nhanh2 câu hỏi · chọn đáp án để xem giải thích
    Bạn đã hoàn thành bài này chưa?Tiến độ lưu trên trình duyệt của bạn.