🔥 고급2026-09-285~7분
Batch API로 LLM 비용 50% 절감: 처리량·지연·실패율 트레이드오프 실전 분석
Anthropic Batch API는 동기 호출 대비 최대 50% 비용을 절감하지만, 최대 24시간 지연과 부분 실패 처리를 설계에 반영해야 한다. 운영 환경에서 실제로 동작하는 배치 파이프라인 패턴을 수치와 함께 정리한다.
batch-apicost-optimizationproduction
Batch API를 써야 하는 상황과 쓰면 안 되는 상황
Batch API는 요청당 비용이 동기 API의 50%이지만, 결과 반환까지 최대 24시간이 걸린다. 운영 환경에서 적합한 워크로드는 다음과 같다.
- 적합: 야간 데이터 파이프라인, 대량 문서 분류, 오프라인 임베딩 생성, A/B 평가 데이터셋 생성
- 부적합: 사용자 대면 실시간 응답, SLA 1분 이내 요구 작업, 순서 의존적 체인
실측 기준: claude-3-5-haiku 기준 동기 $0.80/MTok → 배치 $0.40/MTok. 월 10억 토큰 처리 시 월 40만 원 이상 절감 가능.
실전 배치 파이프라인 구현
import anthropic
import json
from pathlib import Path
client = anthropic.Anthropic()
def submit_batch(requests: list[dict]) -> str:
batch = client.messages.batches.create(
requests=[
{
"custom_id": req["id"],
"params": {
"model": "claude-3-5-haiku-20241022",
"max_tokens": 512,
"messages": [{"role": "user", "content": req["prompt"]}],
},
}
for req in requests
]
)
return batch.id
def poll_and_collect(batch_id: str) -> dict:
import time
while True:
batch = client.messages.batches.retrieve(batch_id)
status = batch.processing_status
if status == "ended":
break
print(f"[{status}] req_counts={batch.request_counts}")
time.sleep(60) # 60초 폴링 — 과도한 호출 방지
results, errors = {}, []
for result in client.messages.batches.results(batch_id):
if result.result.type == "succeeded":
results[result.custom_id] = result.result.message.content[0].text
else: # errored | expired
errors.append({
"id": result.custom_id,
"error": result.result.error.type,
})
return {"results": results, "errors": errors}
핵심 포인트: custom_id를 원본 데이터 PK와 1:1 매핑해 두어야 부분 실패 시 재처리가 쉽다. 폴링 간격은 60초 이상 권장 — 과도한 폴링은 rate limit 소모.
실패 모드와 운영 체크리스트
실패 모드 3가지
errored: 개별 요청 오류 (invalid params 등) → 해당 ID만 동기로 재처리expired: 24시간 초과 미완료 → 전체 배치 재제출, 원인은 대개 과부하 시간대- 네트워크 단절로 batch_id 유실 → 제출 즉시 DB에 batch_id 저장 필수
운영 체크리스트
- [ ] batch_id를 영속 스토리지(DB/S3)에 즉시 기록
- [ ] 에러율 > 5% 시 알림 설정 (request_counts.errored / total)
- [ ] 배치 크기는 요청 10,000건 또는 32MB 이하로 분할
- [ ] 재처리 로직에서 멱등성 보장 (custom_id 중복 방지)
- [ ] 비용 모니터링: 배치 완료 후 usage 집계를 별도 테이블에 적재