LLM Governance: The First Thing to Build in Any Agent System
Why cost observability should be your agent's first capability, not its last.
The Concept
Agent systems have a cost problem that traditional software doesn't.
With SaaS, you pay per seat. With infrastructure, you pay per instance. The pricing is predictable, visible, and budgetable. Agent systems break this model entirely. Every autonomous decision an agent makes is a billable event. Every new capability you add is a new cost surface. And unlike a monthly AWS bill with clear line items, LLM costs compound invisibly across thousands of micro-decisions per day.
Think of it like running a fleet of taxis without meters. You know the drivers are working. You can see them on the road. But you have no idea what each ride costs until the credit card statement arrives at the end of the month.
This is why governance should be the first thing you build, not the last.
When to apply this: Any system making more than a few hundred LLM calls per day, or any system with autonomous background processes that run without human oversight.
Common pitfalls:
- "We'll add monitoring later." By the time you notice runaway costs, you've already burned through budget. Governance is cheaper to build on day one than to retrofit on day ninety.
- Logging without analysis. Recording every call is necessary but not sufficient. Without aggregation, anomaly detection, and trend analysis, logs are just expensive storage.
- One model for everything. Routing every task to your most capable model is like sending a senior engineer to fix a typo. Tiered routing (cheap models for classification, expensive models for creative work) can cut costs 60-80% with no quality loss on simple tasks.
Quick Win: Claude Skill
Here's a prompt you can use right now to analyze your LLM usage and generate a governance report:
You are an LLM cost analyst. I will provide a log of recent LLM API calls.
Analyze them and produce a governance report with these sections:
1. **Spend Summary**: Total cost, total calls, average cost per call
2. **Breakdown by Model**: Cost and call count per model, sorted by spend
3. **Top Cost Drivers**: The 3 most expensive task categories
4. **Anomaly Flags**: Any task type where cost exceeds 2x the average for its category
5. **Optimization Suggestions**: 3 specific, actionable recommendations to reduce cost
Format the output as a structured report with clear headers.
Here is my usage log:
[PASTE YOUR LOG HERE - format: timestamp, model, input_tokens, output_tokens, cost_usd, task_category]
Example input (paste after the prompt):
2026-05-19T08:00:00Z, claude-sonnet-4, 1200, 800, 0.012, intent_classification
2026-05-19T08:01:00Z, claude-sonnet-4, 4500, 2000, 0.039, meeting_brief
2026-05-19T08:05:00Z, gpt-4o-mini, 500, 200, 0.001, auto_triage
2026-05-19T08:10:00Z, claude-sonnet-4, 8000, 4000, 0.072, code_generation
2026-05-19T08:15:00Z, gpt-4o-mini, 300, 100, 0.0005, status_check
2026-05-19T08:20:00Z, claude-sonnet-4, 6000, 3500, 0.057, post_draft
What you'll get back: A structured report showing that claude-sonnet-4 accounts for 99% of spend, that intent_classification is overspending (using a premium model for a simple task), and a recommendation to route classification to a cheaper model.
Iteration tips:
- Add your actual provider pricing table to get exact costs instead of estimates.
- Run this weekly and diff against last week's report to spot cost drift before it compounds.
Full System Specification
Problem Statement
Agent systems lack real-time cost visibility, leading to uncontrolled spend, inability to optimize routing decisions, and no early warning for cost anomalies.
Architecture
Four components, each independently deployable:
-
Call Logger — Middleware that intercepts every LLM API call and records: model name, input/output tokens, cost, latency, intent tag (what the call was for), and daemon/process name.
-
Cost Calculator — A service that maps logged calls to current provider pricing. Fetches live pricing from provider APIs on a daily schedule and flags any drift from the rates you budgeted for.
-
Analyzer — Runs on a schedule (daily and weekly). Aggregates call data, computes baselines (30-day rolling average per intent/daemon), detects anomalies (flags at 2x baseline, alerts at 5x), and generates optimization suggestions.
-
Reporter — Formats analysis into structured reports delivered via your preferred channel (Slack, email, in-app notification, or all three).
Data Model
LLMCall {
id, timestamp, model, inputTokens, outputTokens,
cost, intentTag, daemonName, latencyMs
}
GovernanceReport {
id, periodStart, periodEnd, totalCost, totalCalls,
reportJson, anomalies, suggestions, generatedAt
}
Phased Rollout
MVP: Call logger middleware that records every LLM call to a database table. A weekly cron job that sums total cost and call count, and sends a one-paragraph summary.
v1: Per-daemon and per-intent cost breakdown. Anomaly detection with configurable thresholds (2x warning, 5x critical). Optimization suggestions generated by the LLM itself ("you're spending 51% of budget on one daemon, consider reducing its polling frequency").
v2: Live pricing drift detection (compare what you're paying vs. current provider rates). Model availability smoke tests (periodically call each model with a trivial prompt to verify it responds). Self-healing: auto-switch to a backup model when the primary is unavailable or degraded. Historical trend charts for week-over-week cost comparison.
Technology Recommendations
- Storage: Any SQL database (PostgreSQL recommended). The
LLMCalltable will grow fast; partition by month. - Scheduler: A cron job or background worker (PM2 process, Kubernetes CronJob, or a simple setInterval loop).
- Alerting: Webhook to Slack/Teams/GChat for anomaly notifications.
- Cost source: Provider pricing APIs where available, otherwise a manually maintained pricing table updated monthly.