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.pdfTask 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
- Pick a project or previous lab (e.g., the agent from Lesson 10, the multi-agent pipeline from Lesson 11)
- Identify all API calls and their system prompts / tool definitions
- Estimate the current token cost per run (use the pricing table)
- Add
cache_controlto the static portions of the prompts - Measure the cost before and after caching
- 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
- Create a
BudgetTrackerclass 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
- Wrap the Anthropic client with budget checking
- 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
- Create 20 identical classification tasks
- Run all 20 via real-time API calls (sequentially), recording time and cost
- Run all 20 via the Batch API, recording time and cost
- Compare:
- Total cost (batch should be ~50% cheaper)
- Total wall-clock time (batch will be slower)
- Quality/accuracy (should be identical)
- 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
- Task 1: Caching audit with before/after costs and monthly projection
- Task 2: BudgetTracker class with warning and blocking; test cases for all states
- Task 3: Batch vs real-time comparison with cost, time, and quality metrics
- All code runs without error after kernel restart + run all