Python 88.3%
TypeScript 7.6%
Shell 4.1%
1#!/usr/bin/env bash2# STATUS: LIVE_VERIFIED 2026-09-18 — same sequence run via scripts/live.py (create → retrieve → list → poll → results → delete; cancel path).3# Model: claude-haiku-4-5-20251001, 2 requests of 8 max_tokens (~$0.0001 at batch prices).4# Usage: source scripts/lib.sh && bash examples/anthropic/batch/batch_lifecycle.sh5# Requires: jq6set -eo pipefail7cd "$(dirname "$0")/../../.."8# shellcheck disable=SC10919source scripts/lib.sh10OUTF=$(mktemp); trap 'rm -f "$OUTF"' EXIT11call() { ant "$@" > "$OUTF"; OUT="$(cat "$OUTF")"; } # runs a request, keeps body in $OUT and status in $ANT_STATUS1213MODEL="claude-haiku-4-5-20251001"14body=$(cat <<JSON15{"requests":[16 {"custom_id":"ok-1","params":{"model":"$MODEL","max_tokens":8,"messages":[{"role":"user","content":"Reply with OK."}]}},17 {"custom_id":"bad-model-1","params":{"model":"claude-does-not-exist-1","max_tokens":8,"messages":[{"role":"user","content":"Reply with OK."}]}}18]}19JSON20)2122# 1. create23created=$(ant POST /v1/messages/batches -d "$body"); log_request anthropic POST /v1/messages/batches "$ANT_STATUS" 0.0001 "batch example create"24bid=$(echo "$created" | jq -r .id); echo "created $bid status=$(echo "$created" | jq -r .processing_status)"2526# 2. retrieve + list27ant GET "/v1/messages/batches/$bid" | jq -c '{processing_status, request_counts}'; log_request anthropic GET "/v1/messages/batches/{id}" "$ANT_STATUS" 0 "batch example retrieve"28ant GET "/v1/messages/batches?limit=1" | jq -c '{n: (.data|length), has_more, first_id}'; log_request anthropic GET /v1/messages/batches "$ANT_STATUS" 0 "batch example list"2930# 3. poll until ended (≤ 5 min, every 20 s). Observed: ~100 s for 2 requests.31for _ in $(seq 1 15); do32 st=$(ant GET "/v1/messages/batches/$bid" | jq -r .processing_status); log_request anthropic GET "/v1/messages/batches/{id}" "$ANT_STATUS" 0 "batch example poll"33 echo " processing_status=$st"; [ "$st" = "ended" ] && break; sleep 2034done3536# 4. results (.jsonl — one line per request, unordered; match on custom_id)37ant GET "/v1/messages/batches/$bid/results" | while IFS= read -r line; do38 echo "$line" | jq -c '{custom_id, type: .result.type, text: (.result.message.content[0].text // null), error: (.result.error.error.message // null)}'39done; log_request anthropic GET "/v1/messages/batches/{id}/results" "$ANT_STATUS" 0 "batch example results"4041# 5. delete (only allowed once ended; 400 while in_progress/canceling)42ant DELETE "/v1/messages/batches/$bid" | jq -c .; log_request anthropic DELETE "/v1/messages/batches/{id}" "$ANT_STATUS" 0 "batch example delete"4344# 6. cancel path: create a second batch and cancel immediately → canceling → ended with request_counts.canceled=145c2=$(ant POST /v1/messages/batches -d "{\"requests\":[{\"custom_id\":\"cancel-1\",\"params\":{\"model\":\"$MODEL\",\"max_tokens\":8,\"messages\":[{\"role\":\"user\",\"content\":\"Reply with OK.\"}]}}]}")46log_request anthropic POST /v1/messages/batches "$ANT_STATUS" 0.0001 "batch example create #2"47bid2=$(echo "$c2" | jq -r .id)48ant POST "/v1/messages/batches/$bid2/cancel" | jq -c '{processing_status, cancel_initiated_at}'; log_request anthropic POST "/v1/messages/batches/{id}/cancel" "$ANT_STATUS" 0 "batch example cancel"49for _ in $(seq 1 12); do50 st=$(ant GET "/v1/messages/batches/$bid2" | jq -r .processing_status); log_request anthropic GET "/v1/messages/batches/{id}" "$ANT_STATUS" 0 "batch example poll #2"51 [ "$st" = "ended" ] && break; sleep 1552done53ant GET "/v1/messages/batches/$bid2" | jq -c .request_counts54ant DELETE "/v1/messages/batches/$bid2" | jq -c .; log_request anthropic DELETE "/v1/messages/batches/{id}" "$ANT_STATUS" 0 "batch example delete #2"55