Cost Reporting & Dashboards

Daily and monthly cost summaries per agent and per company, the built-in cost chart views, writing custom cost queries, and exporting cost_events to CSV for finance teams.

Cost Reporting and Optimisation

What you'll learn

Build cost dashboards from cost_events, identify expensive agents and tasks, and apply four proven cost-reduction techniques.

The Cost Dashboard Query

-- Monthly cost summary dashboard
WITH monthly AS (
  SELECT
    a.name                          AS agent,
    a.role,
    ce.model,
    COUNT(*)                        AS invocations,
    SUM(ce.input_tokens)            AS total_input_tokens,
    SUM(ce.output_tokens)           AS total_output_tokens,
    SUM(ce.cost_usd)                AS total_cost,
    AVG(ce.cost_usd)                AS avg_cost_per_call,
    a.monthly_budget_usd            AS budget,
    ROUND(100 * SUM(ce.cost_usd)
      / a.monthly_budget_usd)       AS pct_of_budget
  FROM cost_events ce
  JOIN agents a ON a.id = ce.agent_id
  WHERE ce.company_id = 'your-company-id'
    AND date_trunc('month', ce.created_at) = date_trunc('month', now())
  GROUP BY a.id, a.name, a.role, ce.model, a.monthly_budget_usd
)
SELECT *
FROM monthly
ORDER BY total_cost DESC;

Identifying Expensive Tasks

-- Most expensive individual issues
SELECT
  i.title,
  a.name        AS agent,
  ce.model,
  ce.cost_usd,
  ce.input_tokens + ce.output_tokens AS total_tokens,
  i.created_at
FROM cost_events ce
JOIN issues i ON i.id = ce.issue_id
JOIN agents a ON a.id = ce.agent_id
WHERE ce.company_id = 'your-company-id'
ORDER BY ce.cost_usd DESC
LIMIT 20;

Four Cost-Reduction Techniques

1. Switch to a Cheaper Model for Simple Tasks

Not every agent needs Opus. A classification or summarisation agent can often use Haiku at 1/100th the cost:

{ "model": "claude-haiku-3-5" }   // $0.00025/1K input vs $0.015 for Opus

Savings: up to 98% for compatible tasks.

2. Trim the System Prompt

Every token in the system prompt is re-sent as input on every call. Audit your prompts:

SELECT name, length(system_prompt) AS prompt_chars
FROM agents
WHERE company_id = 'your-company-id'
ORDER BY prompt_chars DESC;

Reduce from 800 → 200 characters = 25% input token reduction per call.

3. Reduce Output Length

Instruct agents to be concise. Add to every system prompt:

Be concise. Maximum 3 paragraphs unless detail is explicitly requested.

4. Reduce Heartbeat Frequency

From every 5 minutes to every 15 minutes = 3x fewer invocations for low-urgency agents:

UPDATE companies
SET heartbeat_cron = '*/15 * * * *'
WHERE slug = 'my-company';

Cost Forecast

-- Project end-of-month spend based on current burn rate
SELECT
  SUM(cost_usd)                  AS mtd_spend,
  SUM(cost_usd)
    / EXTRACT(day FROM now())
    * EXTRACT(day FROM date_trunc('month', now())
      + interval '1 month' - interval '1 day') AS projected_month_total
FROM cost_events
WHERE company_id = 'your-company-id'
  AND date_trunc('month', created_at) = date_trunc('month', now());

Checkpoint ✓

Sign in for progress tracking & Q&A →