Skip to content

API Reference parrot.tools.compression.report

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

parrot.tools.compression.report

View source

Compression savings report — the rtk gain functional equivalent (spec Sec 3 Module 8).

"Measure, don't assume" (spec Sec 7): MINIMAL looks like a big byte win, but BPE tokenizers merge whitespace runs, so the real token saving is much smaller. Without a report nobody would ever find that out. Every saving travels alongside its cost (milliseconds spent) — a saving that cannot be checked against its cost is not evaluable.

CompressionReport aggregates :class:AfterToolCallEvent instances in-process (O(1) per event, no event storage) into a per-tool and per-session breakdown, including below-min_rows/skipped "no gain" cases — knowing which tools never benefit is how the default manifest gets tuned.

Class CompressionReport

Per-tool/per-session compression savings aggregator.

Feed it :class:AfterToolCallEvent instances via :meth:handle (e.g. from an event-bus subscription — wiring a live listener onto ToolManager is future work, out of this module's scope). Never raises: a malformed event is logged and swallowed, never propagated to the caller/event bus.

Aggregation is O(1) per event (running sums, not stored events). p50/p99 codec durations use a bounded per-tool rolling window and the same nearest-rank percentile function as :mod:parrot.tools.compression.budget's CircuitBreaker — reused directly (_percentile) rather than duplicated. CircuitBreaker itself is NOT reused: it is a routing/degrade state machine coupled to the latency-budget router, not a plain aggregator, so wrapping it here would pull in unrelated breaker semantics for no benefit.

Instances hold purely per-instance state (no shared/class-level mutable state) — safe for per-session ownership, mirroring how ToolManager.clone() already gives each session its own BudgetRouter/CompressionTee (TASK-1952/1953) rather than a process-wide singleton.

Parameter Type Default Description
max_retained_durations int _DEFAULT_MAX_RETAINED_DURATIONS Size of the per-tool rolling window of recent compression_duration_ms values used for p50/p99.

Method CompressionReport.handle

Record one tool call's outcome. Never raises.

handle(event: Any, skipped_reason: Optional[str] = None) -> None

Parameter Type Default Description
event Any An :class:AfterToolCallEvent (duck-typed — any object exposing the same attributes works). None/malformed input is logged and ignored.
skipped_reason Optional[str] None The compression_skipped reason, when compression did not run for this call (the event itself carries no such field — it lives in ToolResult.metadata, so the caller passes it explicitly). None means compression ran (possibly with zero gain).

Method CompressionReport.summary

Build a structured, independent snapshot of the current totals.

summary() -> CompressionSummary

Returns: CompressionSummary — class:CompressionSummary — mutating it never affects this report's internal state.

Method CompressionReport.render

Render a compact, human-readable text table for logs/CLI.

render() -> str

Returns: str — A multi-line string, always including the token-estimate caveat and milliseconds spent alongside every saving.

Class CompressionSummary

Structured savings report: per-tool breakdown + session total.

Fields:

Field Type Default Description
tools dict[str, ToolSavings]
session SessionSavings

Class SessionSavings

Session-wide totals, same shape as :class:ToolSavings minus the per-tool skipped/tool_name fields.

Fields:

Field Type Default Description
calls int 0
compressed_calls int 0
bytes_before int 0
bytes_after int 0
est_tokens_saved int 0
compression_ms_total float 0.0

Attributes:

Attribute Type Description
pct_saved float Percentage of bytes saved, or 0.0 when nothing was compressed yet (never divides by zero).

Class ToolSavings

Aggregated compression savings for a single tool.

Fields:

Field Type Default Description
tool_name str Name of the tool this breakdown covers.
calls int 0 Total tool calls observed (compressed AND skipped).
compressed_calls int 0 Calls where the compression stage actually ran (i.e. not skipped) — may include zero-gain outcomes.
skipped dict[str, int] compression_skipped reason -> count. Below-min_rows passthroughs and every other skip reason are recorded here rather than dropped.
bytes_before int 0 Sum of pre-compression sizes, across compressed calls.
bytes_after int 0 Sum of post-compression sizes, across compressed calls.
est_tokens_saved int 0 Sum of bytes/4-estimated tokens saved.
compression_ms_total float 0.0 Sum of time spent in the compression codec.
p50_compression_ms float 0.0 Rolling-window median codec duration.
p99_compression_ms float 0.0 Rolling-window p99 codec duration.

Attributes:

Attribute Type Description
pct_saved float Percentage of bytes saved, or 0.0 when nothing was compressed yet (never divides by zero).

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