Lab Guide — Cost, Caching & Rate Limits
Three exercises exploring prompt caching, cost estimation, and the Batch API.
Setup: Open
lab.pdf in Jupyter. You need a valid API key. The notebook generates a long system prompt for caching exercises. Expect small API charges (~$0.05 total).
Exercise 1 — Prompt Caching
EX 1
Send cached requests and measure token savings
Use prompt caching with a long system prompt and observe the usage statistics.
- Create a system prompt of at least 1,500 tokens (the notebook provides one)
- Send a request with
cache_control— observecache_creation_input_tokens - Send the same request again — observe
cache_read_input_tokens - Send a request without caching for comparison
- Calculate the cost difference between cached and uncached requests
# Expected usage comparison:
# Call 1 (cache write): cache_creation_input_tokens=1500, input_tokens=50
# Call 2 (cache read): cache_read_input_tokens=1500, input_tokens=50
# Call 3 (no cache): input_tokens=1550
Checkpoint: You can show the cost difference between cache-write, cache-read, and no-cache requests.
Exercise 2 — Token Budget Checker
EX 2
Build a function that estimates cost before calling the API
Create a budget checker that warns if a request will exceed a cost threshold.
- Implement
estimate_cost(model, input_tokens, max_output_tokens) -> float - Implement
check_budget(model, messages, max_cost_usd) -> boolthat uses the Anthropic token counting API - Test with various message sizes and budget limits
# Usage:
cost = estimate_cost("claude-sonnet-4-20250514", input_tokens=5000, max_output_tokens=1000)
print(f"Estimated max cost: ${cost:.4f}")
ok = check_budget("claude-sonnet-4-20250514", messages, max_cost_usd=0.01)
if ok:
response = client.messages.create(...)
Checkpoint: Budget checker correctly estimates cost and blocks requests that exceed the limit.
Exercise 3 — Batch API
EX 3
Submit 20 tasks via the Batch API and process results
Use the Message Batches API to process a collection of tasks at 50% cost.
- Create 20 classification tasks (the notebook provides sample texts)
- Submit as a batch with
client.messages.batches.create() - Poll for completion
- Process results and calculate the total cost savings vs real-time
Checkpoint: All 20 tasks complete. You can show the 50% cost saving vs individual API calls.
Tip: The Batch API may take minutes to hours to complete depending on load. Start this exercise early. You can work on other tasks while waiting for results.