Skip to content

API Reference parrot.flows.dev_loop.usage_report

github-actions[bot] edited this page Nov 5, 2026 · 4 revisions

parrot.flows.dev_loop.usage_report

View source

UsageReport — per-agent-seat usage view (FEAT-405 Module 7, rebuilt on the FEAT-479 per-run ledger).

run_bundle.py already renders a per-node table with token columns, but there is no per-agent view: which seat spent what, on which model, over how many rounds/cycles. This module introduces :class:UsageReport as the single source of truth for usage.json, the markdown section folded into the run bundle, and the standalone HTML page (TASK-2091) — all three views are rendered from the same model so they cannot disagree.

FEAT-479 Module 7a: the builder now reads from a :class:~parrot.observability.recorders.run_ledger.RunLedgerRecorder instead of session-state's Snapshot. The ledger is append-only (retry cycles accumulate rather than overwrite — Finding 2) and keys seats by a free string (pool-worker seats like "development.w1" are first-class — Finding 3), so the previous single-worker-model heuristic (_single_worker_summary_for_node) is obsolete: the model now arrives as real per-cycle data, never a guess.

Pure consumer: no filesystem, no Redis, no network — only summing already-final per-seat numbers already accumulated by the ledger.

Class AgentUsage

One agent seat's usage, rolled up across its cycles.

seat is the run's accounting seat — a node id ("qa") or a pool-worker id ("development.w1"); node_id is the roll-up owner ("development.w1" rolls up to "development"). Unlike the pre-FEAT-479 session-state-sourced report, pool-worker seats are first-class rows here, each with its own real model (Finding 3). None (never 0) when a field is unreported across every cycle.

Attributes:

Attribute Type Description
seat str
node_id str
backend str
model str
rounds int | None
input_tokens int | None
output_tokens int | None
duration_seconds float | None
cycles list[CycleUsage]
failures int
cost_usd float | None
equivalent_usd float | None

Class CycleUsage

One retained ledger record for a seat — one dispatch/LLM-call cycle.

cycle is the ledger's 1-based attempt index within (run_id, seat) (RunLedgerRecorder.next_cycle); appends never overwrite, so every retry round for a seat is retained here (Finding 2).

Attributes:

Attribute Type Description
cycle int
model str
input_tokens int | None
output_tokens int | None
duration_seconds float | None
status str
error_type str

Class UsageReport

Single source of truth for usage.json, markdown and HTML.

Attributes:

Attribute Type Description
run_id str
generated_at float
agents list[AgentUsage]
total_input_tokens int | None
total_output_tokens int | None
total_rounds int | None
partial bool
partial_reason str
total_cost_usd float | None
total_equivalent_usd float | None

Function build_spec_usage_report

Per-feature sibling of :func:build_usage_report, grouping SpecUsageRecorder rows by task, model or roster seat.

Reuses :class:CycleUsage/:class:AgentUsage and the existing renderers so the views cannot disagree. Costs sum cost_usd (post-policy) and equivalent_usd; source_cost_usd and the process-cumulative cumulative_cost_usd are NEVER summed (AC-5). Pure — no filesystem.

build_spec_usage_report(rows: Iterable[UsageRecord], feat_id: str, *, by: str = 'task') -> UsageReport

Parameter Type Default Description
rows Iterable[UsageRecord] Usage rows (rows of other features are ignored).
feat_id str The feature id; also used as run_id and node_id.
by str 'task' Grouping — "task", "model" or "seat".

Returns: UsageReport — The assembled :class:UsageReport.

Raises:

  • ValueError — When by is not a supported grouping.

Function build_usage_report

Assemble a :class:UsageReport from a run's usage ledger.

Pure — no filesystem, no Redis, no network. One :class:AgentUsage per seat the ledger retained a record for; a run with an empty ledger (nothing dispatched, or nothing reported usage) still returns a valid, empty report rather than raising.

Node → cycle → worker granular (FEAT-479, superseding the FEAT-405 node-granular-only shape): pool-worker seats ("development.w1"/"development.w2"/...) are retained as their own rows, each carrying its own real model — the ledger's seat is a free string (spec Module 2), not the closed session_state.NodeId Literal that silently dropped them before. Every retained cycle for a seat is exposed via AgentUsage.cycles (retry rounds accumulate, never overwrite — Finding 2's fix).

Sums (AgentUsage.input_tokens/.output_tokens/UsageReport. total_*) come straight from RunLedgerRecorder.by_seat(), which already skips usage_reported=False cycles rather than coercing — an all-unreported seat totals None, never a fabricated 0.

build_usage_report(ledger: RunLedgerRecorder, run_id: str) -> UsageReport

Parameter Type Default Description
ledger RunLedgerRecorder The run's :class:RunLedgerRecorder. Callers detecting a missing ledger (e.g. a cross-process resume that lost the in-memory ledger, spec §8 Q1) should pass a fresh, empty ledger with mark_partial(...) already called, rather than omitting the argument — this function has no "missing ledger" branch of its own; ledger.partial/.partial_reason are read straight through onto the report.
run_id str The run id (also present on ledger.run_id, threaded explicitly so a caller can build a report for a differently-run_id-tagged view if ever needed).

Returns: UsageReport — The assembled :class:UsageReport.

Function render_usage_html

Render report as a fully self-contained HTML usage report.

The page inlines all styling and references no external asset (no <link>/<script src>, no @import, no CDN) — it can be opened from disk or attached to a PR comment without breaking. Column set and the —-for-unreported convention match :func:render_usage_markdown exactly, and no pricing/cost figure appears anywhere (spec Non-Goal). A partial report (§8 Q1) carries a visible marker, above the table and again on the totals row.

Node → cycle → worker (FEAT-479 Module 7b): each seat's <tr> is followed by one indented <tr class="cycle"> per retained cycle — suppressed when the seat has exactly one cycle. A Failures section lists every failed cycle; omitted entirely when there are none.

render_usage_html(report: UsageReport) -> str

Parameter Type Default Description
report UsageReport The assembled :class:UsageReport.

Returns: str — A complete <!doctype html> … </html> document.

Function render_usage_markdown

Render report as a markdown section (folded into the run bundle).

Never renders None/unreported values as 0 — they render as — (em dash), including in the totals row. No pricing/cost figures appear anywhere (spec Non-Goal). A partial report (§8 Q1) carries a visible marker, above the table and again on the totals row, so a short total is never presented as complete.

Node → cycle → worker (FEAT-479 Module 7b): each seat's row is followed by one indented └ cycle N row per retained cycle — suppressed when the seat has exactly one cycle, since the parent row already carries that cycle's numbers. A Failures section lists every failed cycle with its error_type (never the message — see the module's privacy contract) and the tokens burned before failing; omitted entirely when there are no failures.

render_usage_markdown(report: UsageReport) -> str

Parameter Type Default Description
report UsageReport The assembled :class:UsageReport.

Returns: str — The markdown section, including its own ## Usage heading.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally