{# R7 · ANALYTICS center view (live) — usage & cost (CTO item 8 rework). The SUBSTANCE is the usage + estimated-cost breakdowns, computed for the SELECTED PROJECT from its agents × each agent's effective model × token usage × the providers pricing catalog (built in main._analytics_usage_cost): 1. Total usage BY MODEL (bar + table) 2. Usage BY MODEL × PROVIDER (provider totals + their model rows) 3. MODEL USAGE PER AGENT (agent · model · token usage table) 4. est. API COST BY AGENT (tokens × model price/Mtok; 'n/a' unpriced) 5. est. API COST BY PROJECT (Σ per-agent est. cost) A slim headline KPI row (events/24h, active tasks, decisions, recent tokens) is kept above the breakdowns. COST NOTE: the CTO is on SUBSCRIPTIONS now, so every cost here is the counterfactual "what it WOULD cost on metered API tokens" (priced from providers.py per-Mtok), NOT a real charge. Real cost arrives with metered per-call telemetry. Token usage is best-effort from /history `token_count` frames (server-capped window) — agents with no frames show "n/a" and are NOT zero-filled. TODO(usage-telemetry) noted in the footnote. Context (built in main._analytics_view / _analytics_usage_cost): headline KPIs : an_events_24h, an_active_tasks, an_pending_handoffs, an_recent_decisions (+ an_window_days), an_decisions_total, an_handoffs_total, an_lessons_total, an_tokens usage/cost : an_usage = { rows[], agent_count, agents_with_usage, total_tokens(_h), by_model_bars[], by_model_table[], model_count, by_provider[], by_provider_bars[], provider_count, cost_rows[], project_cost(_h), priced_agent_count } an_catalog_priced : did the providers catalog load (pricing available) #} {% macro stat(label, value, unit="", na_note="n/a") -%}
{{ label }}
{% if value is not none %}
{{ value }}{% if unit %}{{ unit }}{% endif %}
{% else %}
{{ na_note }}
{% endif %}
{%- endmacro %} {% macro barlist(rows, swatch="var(--accent)") -%} {% if rows %} {% else %}

No usage recorded yet for this project — the breakdown fills in once agent runs write to the usage store.

{% endif %} {%- endmacro %} {% if selected %}
{# ---------- header ---------- #}
Usage & Cost
live {{ selected.display_name or selected.project_key }}
{# ---------- App-DB store status (E007) ---------- Usage/cost now read from the App-DB usage_events (separate operational store), NOT Cortex. Two graceful states: * store NOT connected → the harness-appdb container isn't up. * connected but NO rows → no agent runs recorded yet. Either way the breakdowns below still render (empty) — never a 500. #} {% if not an_usage.store_connected %}
Usage store not connected. The App-DB (harness-appdb) isn't reachable, so usage & cost can't be read. The console + chat keep working — runs are simply not recorded until the store is up. Bring it up with docker compose -f .agents/docker-compose.cortex.yml up -d harness-appdb and apply the schema.
{% elif an_usage.total_runs == 0 %}
No usage recorded yet. The usage store is connected but has no rows for this project yet — agent runs will populate this. Each chat / dispatch run writes its token usage + estimated cost here.
{% endif %} {# ---------- HEADLINE KPI ROW (slim) ---------- #}
{{ stat("Events · 24h", an_events_24h) }} {{ stat("Active tasks", an_active_tasks) }} {{ stat("Decisions · " ~ an_window_days ~ "d", an_recent_decisions) }} {{ stat("Tokens · recent", an_tokens.total_h if an_tokens.total else none, na_note="n/a") }}
{# ================= COST HERO ================= #}
Est. API cost · this project est.
{{ an_usage.project_cost_h }}
{% if an_usage.project_cost is not none %} summed across {{ an_usage.priced_agent_count }} agent{{ '' if an_usage.priced_agent_count == 1 else 's' }} with priced usage · {{ an_usage.total_tokens_h }} tokens {% elif not an_usage.store_connected %} usage store not connected — cost is recorded once the App-DB is up {% else %} no usage recorded yet — agent runs will populate this {% endif %}
Total tokens {{ an_usage.total_tokens_h or "n/a" }}
Agents w/ usage {{ an_usage.agents_with_usage }}/ {{ an_usage.agent_count }}
Models · providers {{ an_usage.model_count }}· {{ an_usage.provider_count }}
{# subscription / best-effort banner #}
Estimated, not billed. The team runs on subscriptions today, so cost = per-run token usage priced at each model's per-Mtok rate (or the harness's reported cost) — what it would cost metered. Usage is captured per run into the App-DB; agents with no recorded runs show n/a.
{# ================= 1 + 2 — usage by model / by model×provider ================= #}
{# 1. total usage BY MODEL #}
Total usage by model {{ an_usage.model_count }} model{{ '' if an_usage.model_count == 1 else 's' }} · tokens
{{ barlist(an_usage.by_model_bars, "var(--accent)") }} {% if an_usage.by_model_table %}
{% for r in an_usage.by_model_table %} {% endfor %}
ModelProviderTokens
{{ r.model }} {{ r.provider }} {{ r.tokens_h }}
{% endif %}
{# 2. usage BY MODEL × PROVIDER #}
Usage by provider model × provider · {{ an_usage.provider_count }} provider{{ '' if an_usage.provider_count == 1 else 's' }}
{{ barlist(an_usage.by_provider_bars, "#5B7A8C") }} {% if an_usage.by_provider %}
{% for p in an_usage.by_provider %}
{{ p.label }} {{ p.tokens_h }}
    {% for m in p.models %}
  • {{ m.model }}{{ m.tokens_h }}
  • {% endfor %}
{% endfor %}
{% endif %}
{# ================= 3 — model usage PER AGENT ================= #}
Model usage per agent {{ an_usage.agent_count }} agent{{ '' if an_usage.agent_count == 1 else 's' }} · effective model · token usage
{% if an_usage.rows %}
{% for r in an_usage.rows %} {% endfor %}
AgentModelProvider Tokensin · out / Mtok
{{ r.display }} {% if r.model_known %}{{ r.model }} {% else %}no model configured{% endif %} {{ r.provider or "—" }} {% if r.tokens is not none %}{{ r.tokens_h }}{% else %}n/a{% endif %} {% if r.priced %}{{ r.price_in_h }} · {{ r.price_out_h }}{% else %}{% endif %}
{% else %}

No agents resolved for this project.

{% endif %}
{# ================= 4 + 5 — est. cost by agent (+ project total) ================= #}
Est. API cost by agent est. tokens × model price · {{ an_usage.priced_agent_count }} priced
{% if an_usage.cost_rows %}
{% for r in an_usage.cost_rows %} {% endfor %}
AgentModel TokensEst. cost
{{ r.display }} {% if r.model_known %}{{ r.model }} {% else %}{% endif %} {% if r.tokens is not none %}{{ r.tokens_h }}{% else %}n/a{% endif %} {% if r.cost is not none %}{{ r.cost_h }} {% else %}n/a{% endif %}
Project total · est. {{ an_usage.project_cost_h }}
{% else %}

No agents to cost for this project.

{% endif %}
{# ---------- provenance footnote ---------- #}
Usage is rolled up from per-run rows in the App-DB (usage_events) — the separate operational store, NOT Cortex (E007 data separation: Cortex stays agent memory + coordination). Each run records its resolved model · provider · token in/out, and an estimated cost (the harness's reported cost, else the run's tokens priced at the model's per-Mtok rate from the Providers & Models catalog — {{ "loaded" if an_catalog_priced else "unavailable — pricing n/a" }}). Cost is an estimate of metered API spend — the team is on subscriptions, so nothing here is billed; it becomes real on metered API tokens. Where a project / agent has no recorded runs, usage and cost show n/a (never fabricated). {# TODO(usage-telemetry): per-harness routing (codex turn.completed.usage etc.) lands token capture for the non-claude lanes; the App-DB schema already carries provider/model/tokens/cost for them. #}
{% else %}

Select a project to view its usage & cost analytics.

{% endif %}