Skip to content

API Reference parrot.observability.recorders.run_ledger

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.observability.recorders.run_ledger

View source

RunLedgerRecorder — the per-run usage ledger sink (FEAT-479 Module 4b).

An in-memory, append-only AbstractLogger sink. It is a SINK, not an EventProvider — it has no register(). The EXISTING UsageRecordingSubscriber (recorders/subscriber.py) owns the AfterClientCallEvent/ClientCallFailedEvent subscription and fans records out to this sink alongside any other configured recorders.

Being append-only is what fixes spec Finding 2 (retry cycles overwriting each other's token counts in session state): a second cycle for the same seat is appended, not merged, so by_seat() sums every cycle. Because seat is a free string (FEAT-479 Module 2), a pool-worker seat like "development.w1" is a first-class row here — fixing Finding 3 without widening the closed NodeId Literal.

Alias AbstractLogger

Re-exported from parrot.observability.recorders.base.AbstractLogger.

Class RunLedgerRecorder

In-memory, append-only per-run usage ledger.

Cycles accumulate because appends accumulate — nothing is ever overwritten. NOT an EventProvider: it has no register(): the existing UsageRecordingSubscriber owns the subscription and calls record() on this sink like any other AbstractLogger backend.

Parameter Type Default Description
run_id str The dev-loop / dev-flow run identifier this ledger belongs to.

Attributes:

Attribute Type Description
partial bool
partial_reason str
run_id str The run identifier this ledger belongs to.
records list[UsageRecord] Every retained record, in append order.

Method RunLedgerRecorder.next_cycle

Return the 1-based cycle number the next record for seat gets.

next_cycle(seat: str) -> int

Parameter Type Default Description
seat str

Returns: int

Method RunLedgerRecorder.record

Append record to the ledger, assigning cycle if unset.

Never raises: a ledger that throws would be logged as a broken backend by the subscriber's error-isolated fan-out on every call and silently lose everything. The incoming record is never mutated — other sinks in the same fan-out hold the same instance.

record(record: UsageRecord) -> None

Parameter Type Default Description
record UsageRecord

Method RunLedgerRecorder.aclose

No-op — nothing to flush; required by AbstractLogger.

aclose() -> None

Method RunLedgerRecorder.mark_partial

Mark this ledger as partial (§8 Q1: a resumed run found no prior ledger for its run_id). Idempotent — keeps the first reason.

mark_partial(reason: str) -> None

Parameter Type Default Description
reason str

Method RunLedgerRecorder.by_seat

Roll up every retained record into one SeatUsage per seat.

Sums skip records with usage_reported=False rather than coercing; a seat with no reported usage at all yields None for both token sums, never a fabricated 0. Rounds count distinct attempts, while records without an attempt_uid count individually.

by_seat() -> list[SeatUsage]

Returns: list[SeatUsage]

Class SeatUsage

Roll-up of one seat across its cycles. Report-facing only.

Fields:

Field Type Default Description
seat str The accounting seat ("qa", or a pool-worker seat like "development.w1").
node_id str The roll-up owner node id ("development.w1" rolls up to "development").
provider str '' The gen_ai.system provider of the seat's calls (taken from the first cycle).
model str '' The model identifier of the seat's calls (taken from the first cycle).
cycles list[UsageRecord] Every retained UsageRecord for this seat, in append order (1-based cycle numbering).
input_tokens int | None None Sum of input tokens across cycles that reported usage; None when every cycle was unreported (never a fabricated 0).
output_tokens int | None None Sum of output tokens, same convention.
rounds int | None None Number of distinct attempts for this seat; records without an attempt_uid count individually.
failures int 0 Number of cycles with status == "failed".

Alias UsageRecord

Re-exported from parrot.observability.recorders.models.UsageRecord.

Attribute logger

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