AI & LLM APIs cookbook · Beginner
Track token usage and cost per feature and customer
Know which features and customers drive the AI bill, and catch runaway usage early.
Approach
Every response includes usage. Record input, output and cache tokens with the model, feature and customer
for each call, compute cost with your price table, and alert on unusual spend.
Solution
import anthropic
from dataclasses import dataclass
client = anthropic.Anthropic()
# $ per million tokens; keep this table in config and update it when prices change
PRICES = {"claude-sonnet-5-5": {"in": 3.00, "out": 15.00, "cache_read": 0.30, "cache_write": 3.75}}
@dataclass
class UsageRecord:
feature: str
customer_id: str
model: str
input_tokens: int
output_tokens: int
cache_read: int
cache_write: int
@property
def cost(self) -> float:
p = PRICES[self.model]
return (self.input_tokens * p["in"] + self.output_tokens * p["out"]
+ self.cache_read * p["cache_read"] + self.cache_write * p["cache_write"]) / 1e6
def tracked_create(feature: str, customer_id: str, **params):
response = client.messages.create(**params)
u = response.usage
record = UsageRecord(feature, customer_id, params["model"], u.input_tokens, u.output_tokens,
u.cache_read_input_tokens or 0, u.cache_creation_input_tokens or 0)
save_usage(record) # write to your metrics or database
return response
Pitfalls
- Hard-coding prices in many places; keep one table.
- Ignoring cache tokens, which are billed at different rates.
- Only looking at the monthly invoice, after the damage is done.
Variations
- Enforce per-customer daily budgets before calling the model.
- Export usage as metrics (Prometheus, OpenTelemetry) and build a dashboard by feature.
Further reading: Pricing
Reference: https://docs.anthropic.com/en/docs/about-claude/pricing
More AI & LLM APIs recipes
- Answer with citations that point at the source text
- Build hybrid RAG search on PostgreSQL with pgvector
- Cache a long system prompt and documents
- Cache answers to frequent questions safely
- Classify thousands of items with the Batch API
- Compare two prompt versions with an unbiased model judge
- Expose an internal API as an MCP server
- Extract structured data with a schema