深度

Anthropic Batch API 完全指南:大批量处理 Claude 请求节省 50% 成本

Anthropic Messages Batches API 完整教程:批量 API 是什么(异步批处理/24小时内完成)、与普通 API 的成本对比(50% 折扣)、Python/Node.js 创建批次请求、轮询批次状态、下载并处理结果、错误处理(部分失败的处理方式)、适合与不适合批量处理的场景、批次取消与数据保留策略,以及批量处理 1000 篇文章摘要的完整实战示例。

2026/3/204分钟 阅读ClaudeEagle

对于不需要实时响应的任务(数据处理、内容生成、批量分析), 使用 Messages Batches API 可节省 50% 成本,且不占用普通请求的速率限制。

批量 API vs 普通 API

对比普通 APIBatch API
响应方式同步(等待结果)异步(最多 24 小时完成)
成本标准价格50% 折扣
速率限制占用 RPM/TPM独立限额,不占用
适合实时交互离线批处理
最大批次大小-10,000 个请求 / 32 MB

Python 快速上手

创建批次

python
import anthropic

client = anthropic.Anthropic()

# 一次提交 3 个请求(实际可到 10,000 个)
batch = client.messages.batches.create(
    requests=[
        {
            "custom_id": "article-001",
            "params": {
                "model": "claude-haiku-4-5",
                "max_tokens": 256,
                "messages": [{
                    "role": "user",
                    "content": "用一句话总结:人工智能是一种..."
                }],
            },
        },
        {
            "custom_id": "article-002",
            "params": {
                "model": "claude-haiku-4-5",
                "max_tokens": 256,
                "messages": [{
                    "role": "user",
                    "content": "用一句话总结:机器学习是一种..."
                }],
            },
        },
    ]
)

print(f"批次 ID:{batch.id}")
print(f"状态:{batch.processing_status}")  # in_progress

轮询批次状态

python
import time

def wait_for_batch(batch_id: str, poll_interval: int = 30):
    # 轮询直到完成(不要轮询太频繁,最少 30s 一次)
    while True:
        batch = client.messages.batches.retrieve(batch_id)
        status = batch.processing_status

        print(f"状态: {status} | "
              f"成功: {batch.request_counts.succeeded} | "
              f"失败: {batch.request_counts.errored} | "
              f"处理中: {batch.request_counts.processing}")

        if status == "ended":
            break

        time.sleep(poll_interval)

    return batch

batch = wait_for_batch(batch.id)
print("批次处理完成!")

下载并处理结果

python
results = {}

for result in client.messages.batches.results(batch.id):
    custom_id = result.custom_id

    if result.result.type == "succeeded":
        text = result.result.message.content[0].text
        results[custom_id] = {"status": "ok", "text": text}
    else:
        error = result.result.error
        results[custom_id] = {"status": "error", "error": str(error)}

# 查看结果
for cid, r in results.items():
    if r["status"] == "ok":
        print(f"{cid}: {r['text'][:50]}...")
    else:
        print(f"{cid}: 失败 - {r['error']}")

Node.js 示例

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

const client = new Anthropic();

// 创建批次
const batch = await client.messages.batches.create({
  requests: articles.map((article, i) => ({
    customId: `article-${i}`,
    params: {
      model: "claude-haiku-4-5",
      maxTokens: 200,
      messages: [{
        role: "user",
        content: `一句话总结以下文章:

${article.content}`,
      }],
    },
  })),
});

// 等待完成
let batchStatus = batch;
while (batchStatus.processingStatus !== "ended") {
  await new Promise((r) => setTimeout(r, 30_000));
  batchStatus = await client.messages.batches.retrieve(batch.id);
}

// 处理结果
for await (const result of await client.messages.batches.results(batch.id)) {
  if (result.result.type === "succeeded") {
    console.log(result.customId, result.result.message.content[0].text);
  }
}

完整实战:批量处理 1000 篇文章摘要

python
import anthropic
import json
import time
from pathlib import Path

client = anthropic.Anthropic()

# 1. 读取文章数据
articles = json.loads(Path("articles.json").read_text())
print(f"共 {len(articles)} 篇文章需要处理")

# 2. 分批提交(每批最多 10,000 个)
BATCH_SIZE = 1000
all_batches = []

for i in range(0, len(articles), BATCH_SIZE):
    chunk = articles[i:i+BATCH_SIZE]
    batch = client.messages.batches.create(
        requests=[
            {
                "custom_id": f"article-{a['id']}",
                "params": {
                    "model": "claude-haiku-4-5",
                    "max_tokens": 200,
                    "messages": [{
                        "role": "user",
                        "content": f"用两句话概括这篇文章的核心内容:

{a['content'][:2000]}"
                    }],
                },
            }
            for a in chunk
        ]
    )
    all_batches.append(batch.id)
    print(f"批次 {len(all_batches)} 已提交,ID: {batch.id}")

# 3. 等待所有批次完成
results = {}
for batch_id in all_batches:
    while True:
        b = client.messages.batches.retrieve(batch_id)
        if b.processing_status == "ended":
            break
        time.sleep(60)  # 1 分钟轮询一次

    # 4. 收集结果
    for result in client.messages.batches.results(batch_id):
        if result.result.type == "succeeded":
            article_id = result.custom_id.replace("article-", "")
            results[article_id] = result.result.message.content[0].text

# 5. 保存结果
Path("summaries.json").write_text(json.dumps(results, ensure_ascii=False, indent=2))
print(f"完成!共处理 {len(results)} 篇,节省约 50% 成本")

适合/不适合批量 API 的场景

适合

  • 批量生成产品描述(电商场景)
  • 大量文档翻译或摘要
  • 训练数据生成和清洗
  • 每日报告自动生成
  • 内容审核和分类

不适合

  • 用户实时交互(聊天机器人)
  • 需要立即返回结果的 API
  • 多轮对话(每次请求依赖上次结果)

来源:Anthropic 官方文档 - docs.anthropic.com/en/docs/build-with-claude/message-batches

相关文章推荐

深度Claude API 批量处理完全指南:Message Batches API 大规模数据处理实战Claude API Message Batches 完整教程:批量 API 原理、与普通 API 的区别(50% 成本折扣)、Python/Node.js 提交批次代码示例、进度追踪与结果获取、错误处理策略、并发批次管理,以及文档摘要/数据分类/批量翻译等典型大规模处理场景实战。2026/3/16深度Claude API Prompt Caching 详解:让重复内容成本降低 90%Claude API Prompt Caching 完整指南:工作原理、缓存命中条件、supported models、代码示例(系统提示缓存/文档缓存/对话历史缓存)、成本对比计算、TTL 机制与最佳实践。2026/3/14深度Claude API 速率限制完全指南:限额说明、错误处理与优化策略Anthropic Claude API 速率限制完整说明:请求频率限制(RPM)、Token 用量限制(TPM/TPD)、不同使用层级的限额对比(免费层/Build/Scale/Enterprise)、429 错误的标准处理方式(指数退避重试)、提升限额的申请方法、Prompt Caching 和 Batch API 绕过限制的技巧,以及高并发场景的队列设计方案。2026/3/18深度Claude Extended Thinking 深度思考模式:复杂推理场景实战指南Claude Extended Thinking(扩展思考)完整指南:工作原理、支持模型、API 用法、thinking budget 配置、流式输出处理、适用场景(数学/架构/安全审计)、成本控制与常见误区。2026/3/15深度Anthropic Claude 模型全家族解析:从 Haiku 到 Opus 4 怎么选最划算Anthropic Claude 全系列模型解析:Haiku/Sonnet/Opus 三档定位与适用场景、2026 年主要模型版本对比、1M 超长上下文、价格对比与成本测算、三大选择策略(按需升降/硬性分层/混合路由)。2026/3/14深度Anthropic 责任扩展政策 v3.0 深度解读:两年反思、三大新机制与行业合作路径Anthropic RSP v3.0 深度解读:两年实施经验的诚实评估(成功:ASL-3 实施/行业带动;失败:能力阈值模糊/政府行动迟缓)、三大新机制(公司行动 vs 行业建议分离/前沿安全路线图/每季度风险报告+外部审查),以及从单边承诺转向透明化多边合作的深层战略转变。2026/3/3