CDX-301i · Module 2
Cost Attribution & Chargebacks
3 min read
Enterprise agent operations require cost attribution — knowing not just how much the system costs in aggregate, but how much each team, project, pipeline, and task type costs individually. Cost attribution enables chargebacks (billing internal teams for their usage), optimization (identifying the most expensive task types), and budgeting (forecasting future costs based on historical patterns).
Cost attribution at the agent level is straightforward: each API call returns token counts (input and output) and the model used. Multiply tokens by the model's per-token price and you have the cost per agent invocation. Aggregating these costs requires tagging each invocation with metadata: team, project, pipeline, task type, priority. The tagging must happen at submission time — by the time the agent runs, it is too late to add business context to the cost record.
from dataclasses import dataclass
# Per-million-token pricing (as of 2026)
MODEL_COSTS = {
"o3": {"input": 10.00, "output": 40.00},
"codex-1": {"input": 2.50, "output": 10.00},
"gpt-4.1": {"input": 2.00, "output": 8.00},
}
@dataclass
class CostRecord:
trace_id: str
agent: str
model: str
input_tokens: int
output_tokens: int
# Business metadata for attribution
team: str
project: str
task_type: str
@property
def cost_usd(self) -> float:
rates = MODEL_COSTS.get(self.model, {"input": 5.0, "output": 20.0})
return (
(self.input_tokens / 1_000_000) * rates["input"]
+ (self.output_tokens / 1_000_000) * rates["output"]
)
def monthly_report(records: list[CostRecord]) -> dict:
"""Aggregate costs by team and project."""
report: dict[str, float] = {}
for r in records:
key = f"{r.team}/{r.project}"
report[key] = report.get(key, 0) + r.cost_usd
return dict(sorted(report.items(), key=lambda x: -x[1]))
Do This
- Tag every task at submission with team, project, and task type for attribution
- Track both token counts and dollar costs — token counts for optimization, dollars for chargebacks
- Generate weekly cost reports by team and project to catch spending anomalies early
- Compare cost-per-task across models to validate model selection policies
Avoid This
- Track only aggregate API spend — you cannot optimize what you cannot attribute
- Skip tagging metadata at submission — retroactive attribution is unreliable
- Assume expensive models drive costs — high-volume cheap models often dominate
- Wait for monthly invoices to discover cost overruns — weekly reports catch anomalies sooner