Claw Mart
← All issuesClaw Mart Daily
Issue #101June 17, 2026

Stop guessing what your agent costs — here's the metering pattern that tracks every dollar

Your agent just burned through $47 overnight and you have no idea why.

Maybe it got stuck in a loop. Maybe it decided to analyze your entire codebase. Maybe it's just Wednesday and Claude is expensive. You check your OpenAI dashboard, see a bunch of API calls, and shrug.

This is backwards. Your agent is infrastructure now. You need to meter it like infrastructure.

The pattern: wrap every agent call with cost tracking

Don't rely on your LLM provider's billing dashboard. By the time you see those charges, the damage is done. Instead, track costs in real-time at the application level.

Here's the wrapper I use for every agent call:

def metered_call(model, messages, task_id=None, user_id=None):
    start_time = time.time()
    
    # Estimate input cost
    input_tokens = count_tokens(messages)
    estimated_input_cost = input_tokens * MODEL_PRICING[model]['input']
    
    response = openai.chat.completions.create(
        model=model,
        messages=messages
    )
    
    # Calculate actual costs
    actual_input_cost = response.usage.prompt_tokens * MODEL_PRICING[model]['input']
    actual_output_cost = response.usage.completion_tokens * MODEL_PRICING[model]['output']
    total_cost = actual_input_cost + actual_output_cost
    
    # Log to your metrics system
    log_agent_cost({
        'task_id': task_id,
        'user_id': user_id,
        'model': model,
        'input_tokens': response.usage.prompt_tokens,
        'output_tokens': response.usage.completion_tokens,
        'cost': total_cost,
        'duration': time.time() - start_time,
        'timestamp': datetime.utcnow()
    })
    
    return response

Now every agent interaction gets tracked with context. You know which tasks are expensive, which users are burning budget, and which models are worth the premium.

Set spending limits that actually work

API rate limits are about requests per minute. Agent spending limits are about dollars per task, per user, per day.

class SpendingGovernor:
    def __init__(self):
        self.limits = {
            'task_max': 5.00,      # $5 per task
            'user_daily': 25.00,    # $25 per user per day
            'system_hourly': 100.00 # $100 system-wide per hour
        }
    
    def check_limits(self, task_id, user_id, proposed_cost):
        current_task_cost = get_task_cost(task_id)
        current_user_daily = get_user_daily_cost(user_id)
        current_hourly = get_system_hourly_cost()
        
        if current_task_cost + proposed_cost > self.limits['task_max']:
            raise SpendingLimitError(f"Task would exceed ${self.limits['task_max']} limit")
            
        if current_user_daily + proposed_cost > self.limits['user_daily']:
            raise SpendingLimitError(f"User would exceed ${self.limits['user_daily']} daily limit")
            
        if current_hourly + proposed_cost > self.limits['system_hourly']:
            raise SpendingLimitError(f"System would exceed ${self.limits['system_hourly']} hourly limit")
        
        return True

The key insight: check limits before making the call, not after. Use your token estimation to predict cost and block expensive operations before they happen.

Warning: Don't just log costs — act on them. Set up alerts when spending spikes, automatic model downgrades when budgets are tight, and circuit breakers that pause agents before they drain your account.

The dashboard you actually need

Track three metrics that matter:

  • Cost per completed task — Is your agent getting more or less efficient over time?
  • Cost per user per day — Who's driving your bill and why?
  • Model utilization — Are you using GPT-4 for work that GPT-3.5 could handle?

I run a simple daily report that shows me the most expensive tasks, the most expensive users, and any anomalous spending patterns. Takes 30 seconds to review and has caught multiple runaway loops before they became budget disasters.

Your agent is a business expense now. Treat it like one. Meter it, limit it, and optimize it like you would any other piece of infrastructure that touches your bottom line.

Paste into your agent's workspace

Claw Mart Daily

Get tips like this every morning

One actionable AI agent tip, delivered free to your inbox every day.