Technical
Agent Cost Tracking: Knowing Your Spend Before The Bill
Agent runs cost real money. The individual calls feel cheap, but they add up fast when subagents spawn subagents. I went from 'probably fine' to 'I know every dollar' with a small tracker that plugs into my orchestrator.
The Data I Capture
For every API call: timestamp, model, input tokens, output tokens, cost, the task label, and the session id. Six fields, one row per call, one CSV file rolled daily.
COSTS = {'claude-opus-4': (15/1e6, 75/1e6), 'claude-sonnet-4': (3/1e6, 15/1e6)}
def log_call(model, in_tok, out_tok, task, session):
in_rate, out_rate = COSTS[model]
cost = in_tok * in_rate + out_tok * out_rate
csv_append('costs.csv', [now(), model, in_tok, out_tok, cost, task, session])
return costThirty lines with imports. Runs as a middleware around every call. Cost of the tracker itself: negligible.
What The Data Told Me
Seventy percent of my spend came from one task category: content generation with Opus. Moving that to Sonnet cut the bill by 60% with no measurable quality drop for that specific task. The data made the choice obvious. Without data I would have defended Opus for everything out of habit.
The Model Routing Rule
Haiku for exploration, Sonnet for implementation, Opus for architecture and complex review. This mapping came out of the cost data. Cheap models do 80% of the work. The expensive model does the 20% that needs it.
The Weekly Review
Friday afternoon I open the CSV and group by task. Any task trending up gets a closer look. Any task that exceeded its expected cost budget gets a 'why' annotation. The review takes fifteen minutes. It has paid for itself every week.
See the Anthropic pricing page for current rates. Track from day one. You cannot optimize what you do not measure.
RELATED READING
The Consulting Shift I Am Making In Year Two
After a year of writing and building, my consulting practice is changing shape. Shorter engagements. Sharper outcomes.
ReadThe Frontend Shift: Shipping Less JavaScript In Year Two
A year ago I reached for Next.js for everything. This year I often reach for nothing.
ReadThe Serverless Lesson I Would Write On A Sticky Note
After a year of shipping serverless projects, one rule explains most of the wins and all of the losses.
Read