Lesson 14 Cost, Caching & Rate Limits
Theme:

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).

📥 Lab Notebooks

⬇ lab.pdf ⬇ lab_answer.pdf

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.

  1. Create a system prompt of at least 1,500 tokens (the notebook provides one)
  2. Send a request with cache_control — observe cache_creation_input_tokens
  3. Send the same request again — observe cache_read_input_tokens
  4. Send a request without caching for comparison
  5. 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.

  1. Implement estimate_cost(model, input_tokens, max_output_tokens) -> float
  2. Implement check_budget(model, messages, max_cost_usd) -> bool that uses the Anthropic token counting API
  3. 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.

  1. Create 20 classification tasks (the notebook provides sample texts)
  2. Submit as a batch with client.messages.batches.create()
  3. Poll for completion
  4. 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.