Lesson 14 Cost, Caching & Rate Limits
Theme:

Homework

Three tasks applying cost optimization, budget controls, and batch processing to real projects.

Complete all three lab exercises before starting the homework. These tasks extend the cost-awareness patterns from the lab.

📥 Reference Solution

⬇ lab_answer.pdf

Task 1 — Audit a Project for Caching Opportunities

Objective

Review an existing project (or your lab work from previous lessons) and identify opportunities to add prompt caching.

Instructions

  1. Pick a project or previous lab (e.g., the agent from Lesson 10, the multi-agent pipeline from Lesson 11)
  2. Identify all API calls and their system prompts / tool definitions
  3. Estimate the current token cost per run (use the pricing table)
  4. Add cache_control to the static portions of the prompts
  5. Measure the cost before and after caching
  6. Document: which prompts were cached, the savings per call, and the projected monthly savings at 1,000 requests/day

Deliverable

A notebook with the before/after analysis, code changes, and a cost projection table.

Self-check: Cache read tokens appear in the usage object. Projected savings are at least 50% on cached tokens.

Task 2 — Build a Budget Checker

Objective

Build a reusable budget checker that prevents accidental overspend.

Instructions

  1. Create a BudgetTracker class that:
    • Tracks cumulative spend across all API calls in a session
    • Estimates cost before each call using token counting
    • Raises a warning when approaching a budget limit (80% threshold)
    • Blocks calls that would exceed the budget
  2. Wrap the Anthropic client with budget checking
  3. Test with a budget of $0.10 and demonstrate the warning and blocking behaviour

Deliverable

A notebook with the BudgetTracker class, integration with the API client, and test demonstrating all states (ok, warning, blocked).

Self-check: The tracker correctly blocks calls that would exceed budget. Cumulative spend is accurately tracked.

Task 3 — Batch vs Real-Time Comparison

Objective

Compare the Batch API against real-time API calls for the same workload.

Instructions

  1. Create 20 identical classification tasks
  2. Run all 20 via real-time API calls (sequentially), recording time and cost
  3. Run all 20 via the Batch API, recording time and cost
  4. Compare:
    • Total cost (batch should be ~50% cheaper)
    • Total wall-clock time (batch will be slower)
    • Quality/accuracy (should be identical)
  5. Write a recommendation: when to use batch vs real-time

Deliverable

A notebook with both runs, a comparison table, and a written recommendation.

Self-check: Cost savings are approximately 50%. Output quality is equivalent. Your recommendation is supported by the data.

Submission Checklist