#!/usr/bin/env bash # Prompt caching with an explicit breakpoint on a ~5.8k-token system block (Haiku 4.5 minimum is 4,096 tokens), # called twice: first call writes (cache_creation_input_tokens), second reads (cache_read_input_tokens). # STATUS: LIVE_VERIFIED 2026-09-18 — cold run: call 1 cache_creation 5809 / read 0, call 2 creation 0 / read 5809 (~$0.008); # a re-run within 5 minutes of another example using the same prefix shows read-only on both calls (observed 5251/5251). # Run: bash examples/anthropic/prompt-caching/cache_system_prompt.sh set -eo pipefail cd "$(dirname "$0")/../../.." && source scripts/lib.sh OUTF="$(mktemp)"; trap 'rm -f "$OUTF"' EXIT # `ant` sets ANT_STATUS; calling it inside $(...) would lose it, so write the body to a file instead. call() { ant "$@" > "$OUTF"; OUT="$(cat "$OUTF")"; } MODEL="${ANTHROPIC_MODEL:-claude-haiku-4-5-20251001}" FILLER="$(python3 -c "print(' '.join(f'Section A-{i}: The quarterly logistics review covers warehouse throughput, carrier performance, customs documentation, cold-chain compliance, and regional demand forecasting for the coming period.' for i in range(150)))")" BODY="$(jq -n --arg m "$MODEL" --arg f "$FILLER" '{model:$m, max_tokens:8, system:[{type:"text", text:$f, cache_control:{type:"ephemeral"}}], messages:[{role:"user", content:"Reply with OK."}]}')" for i in 1 2; do call POST /v1/messages -d "$BODY" echo "call $i HTTP $ANT_STATUS: $(echo "$OUT" | jq -c '.usage | {input_tokens, cache_creation_input_tokens, cache_read_input_tokens, cache_creation}')" log_request anthropic POST /v1/messages "$ANT_STATUS" 0.004 "example prompt-caching/cache_system_prompt.sh call $i $MODEL" done