Scaling Up without Breaking the Bank › Implementing Cost Controls

Michael BrenndoerferAugust 28, 20253 min read
AI Agent HandbookAll chapters
Part 15 of 15Performance and Cost Optimization - Doing More with Less
Chapter 44Scaling Up without Breaking the Bank › Implementing Cost Controls
Reading section 6 of 13 · Implementing Cost Controls · 3 minSection 6 of 13 · 3 min

Book contents

Section 6 of 13 · 3 min read

Scaling Up without Breaking the Bank › Implementing Cost ControlsOne section · Sections in this chapter6 / 13 ↓

Now let's talk about the other side of scaling: cost. As usage grows, so do your API bills. Without controls, costs can spiral out of control.

The most important cost control is setting limits. Every major AI provider lets you set spending caps. On Anthropic's platform, you can set a monthly budget. Once you hit that limit, API calls fail rather than continuing to charge you.

Here's how to implement your own cost tracking:

In[2]:
Code
## Using Claude Sonnet 4.5 for agent operations
from anthropic import Anthropic
from datetime import datetime


class CostTracker:
    def __init__(self, monthly_budget_usd=100):
        self.monthly_budget = monthly_budget_usd
        self.costs = []

        # Pricing for Claude Sonnet 4.5 (example rates)
        self.input_cost_per_1k = 0.003  # $0.003 per 1K input tokens
        self.output_cost_per_1k = 0.015  # $0.015 per 1K output tokens

    def record_usage(self, input_tokens, output_tokens):
        """Record token usage and calculate cost"""
        input_cost = (input_tokens / 1000) * self.input_cost_per_1k
        output_cost = (output_tokens / 1000) * self.output_cost_per_1k
        total_cost = input_cost + output_cost

        self.costs.append(
            {
                "timestamp": datetime.now(),
                "input_tokens": input_tokens,
                "output_tokens": output_tokens,
                "cost": total_cost,
            }
        )

        return total_cost

    def get_monthly_cost(self):
        """Calculate costs for the current month"""
        now = datetime.now()
        month_start = now.replace(
            day=1, hour=0, minute=0, second=0, microsecond=0
        )

        monthly_costs = [
            entry["cost"]
            for entry in self.costs
            if entry["timestamp"] >= month_start
        ]

        return sum(monthly_costs)

    def check_budget(self):
        """Check if we're within budget"""
        current_cost = self.get_monthly_cost()
        remaining = self.monthly_budget - current_cost

        return {
            "current_cost": current_cost,
            "budget": self.monthly_budget,
            "remaining": remaining,
            "within_budget": remaining > 0,
        }


## Use the cost tracker
tracker = CostTracker(monthly_budget_usd=100)

client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))


def process_with_cost_tracking(query):
    """Process a request and track its cost"""
    # Check budget first
    budget_status = tracker.check_budget()
    if not budget_status["within_budget"]:
        raise Exception(
            f"Monthly budget exceeded: ${budget_status['current_cost']:.2f}"
        )

    response = client.messages.create(
        model="claude-sonnet-4-5",
        max_tokens=1024,
        messages=[{"role": "user", "content": query}],
    )

    # Record usage
    cost = tracker.record_usage(
        response.usage.input_tokens, response.usage.output_tokens
    )

    print(f"Request cost: ${cost:.4f}")
    print(f"Monthly total: ${tracker.get_monthly_cost():.2f}")

    return response.content[0].text


## Process a query
result = process_with_cost_tracking("What's the capital of France?")

This tracker monitors spending and enforces your budget. Before processing each request, it checks if you're still within budget. If you've exceeded your limit, it refuses the request.

You can extend this with more sophisticated controls:

Per-user limits: Track spending by user and limit individual users to prevent one person from consuming your entire budget.

Rate limiting: Limit how many requests a user can make per hour or day. This prevents accidental or malicious overuse.

Priority tiers: Give some users higher limits than others. Your executive team might get unlimited access while regular employees have daily caps.

Alerts: Send notifications when you hit 50%, 75%, and 90% of your budget, so you can take action before hitting the limit.