How do I track cost per agent run?
Add a run ID header to your agent workflow so Cognocient shows total cost, step count, and model breakdown for each individual execution.
Goal: See exactly what each agent run cost — not just an aggregate monthly total — and set a per-run budget so a single runaway execution can't consume your entire feature budget.
Time: 15 minutes to instrument. Data appears immediately.
Step 1 — Generate a unique run ID per execution
The key change: generate a fresh UUID at the start of each agent execution and pass it as X-Cost-Run-ID on every API call within that run.
Generate run_id once per agent execution, not once per step. Every step in the same run gets the same ID — that's how Cognocient groups them into a single cost entry.
Step 2 — View runs in the dashboard
Go to Dashboard → Sessions (or Workstreams). Each X-Cost-Run-ID value appears as a row showing:
- Total cost for the entire run
- Number of API calls (steps)
- Models used and their individual costs
- Duration from first to last call
- Any waste flags (e.g. "Context bloat after step 3")
Step 3 — Set a per-run spending limit (recommended)
Create a budget with a per-session limit so a single runaway run can't consume your entire monthly feature budget.
Go to Dashboard → Budgets → New Budget:
| Field | Value |
|---|---|
| Name | document-processor per run |
| Scope | Feature |
| Scope value | document-processor |
| Monthly limit | Your normal monthly budget |
| Per-run limit | $0.50 (or whatever a normal run should cost) |
| Enforcement | Block |
When a single run exceeds $0.50, only that run is blocked — other concurrent runs with different IDs continue normally.
Step 4 — Check budget before each step (for long-running agents)
For agents that run dozens of steps, check the budget before each step so you can exit gracefully instead of being cut off mid-execution:
Related: Debug Runaway Agent Loops · Hierarchical Budgets · MCP / A2A Attribution
Related articles
Tag Your First AI Call
Add 2 headers to your existing code and see per-feature spend in under 5 minutes.
Set a Monthly Spending Limit
Create a hard budget enforced at the proxy before charges reach your provider bill.
Cut Your AI Bill with One Click
Use AI Advisor recommendations to apply model downgrades and caching without code changes.