aeat.application.ledger._llm_diagnostics module¶
LLM usage, cost, and classification-confidence diagnostics aggregation.
Folds the two metric stores the system already persists into one typed, operator-facing report:
the encrypted LLM usage log written by
UsageRecorder(per call: provider, model, input/output tokens, estimated cost, cache-hit flag); andthe classification confidence stamped on each ledger
Transactionwhose active decision came from an LLM classifier (classified_byshapedllm:<provider>:<model>with aclassification_confidencein[0, 1]), loaded from the active bucket’sTransactionCatalogueRepositorywhen callers do not inject transactions directly.
No new tracking is introduced here: every figure is aggregated from records
already on disk. The usage store and the classification store use distinct
provider namespaces (the completion adapter’s LLMProvider versus the
subprocess classifier provenance), so the report presents them as two parallel
sections rather than a lossy cross-namespace join.
The report reads only accounting metadata (token counts, cost, confidence
scores, provider labels); it never surfaces the persisted response text or any
financial content, honouring sensitive-financial-data-secure-storage-only.
See also
build_llm_diagnostics_report():Public aggregator that folds the usage log and ledger confidence rows.
UsageRecorder:Storage boundary for provider/token/cost accounting records.
TransactionCatalogueRepository:Bucket-scoped ledger catalogue reader used for confidence diagnostics.
- DEFAULT_LOW_CONFIDENCE_THRESHOLD¶
Default confidence floor below which an LLM classification is reported as low-confidence. Operator-facing display threshold, overridable per call.
- class LlmUsageProviderMetrics(**data)[source]¶
Bases:
BaseModelPer-provider aggregate of the LLM usage/cost log.
Aggregated from
UsageRecordrows for a singleprovider.callscounts every recorded call (cache hits included);cache_hitscounts the subset served from the local cache.- Parameters:
- provider: str¶
- calls: int¶
- cache_hits: int¶
- input_tokens: int¶
- output_tokens: int¶
- total_tokens: int¶
- cost_estimate_usd: Decimal¶
- class LlmConfidenceProviderMetrics(**data)[source]¶
Bases:
BaseModelPer-provider confidence distribution over LLM-classified transactions.
Aggregated from the active ledger catalogue’s transactions whose
classified_bycarries anllm:provenance and a non-nullclassification_confidence.low_confidence_countis the number of those decisions below the report’s tunable threshold.high_confidence_count(>= 0.8) andmedium_confidence_count([0.5, 0.8)) are fixed-floor distribution buckets; the remaining decisions fall below0.5.- Parameters:
- provider: str¶
- classified_count: int¶
- low_confidence_count: int¶
- high_confidence_count: int¶
- medium_confidence_count: int¶
- min_confidence: Decimal | None¶
- max_confidence: Decimal | None¶
- mean_confidence: Decimal | None¶
- class LlmDiagnosticsReport(**data)[source]¶
Bases:
BaseModelTyped LLM usage / cost / confidence diagnostics report.
Produced by
build_llm_diagnostics_report(). The usage section folds the encrypted usage log; the confidence section folds the classification confidence stamped on ledger transactions.has_dataisFalsewhen neither store carries any LLM activity, so callers can print an instructive empty message.- Parameters:
since (date | None)
until (date | None)
low_confidence_threshold (Decimal)
usage_providers (tuple[LlmUsageProviderMetrics, ...])
total_calls (int)
total_cache_hits (int)
total_input_tokens (int)
total_output_tokens (int)
total_cost_estimate_usd (Decimal)
confidence_providers (tuple[LlmConfidenceProviderMetrics, ...])
total_classified (int)
total_low_confidence (int)
- since: date | None¶
- until: date | None¶
- low_confidence_threshold: Decimal¶
- usage_providers: tuple[LlmUsageProviderMetrics, ...]¶
- total_calls: int¶
- total_cache_hits: int¶
- total_input_tokens: int¶
- total_output_tokens: int¶
- total_cost_estimate_usd: Decimal¶
- confidence_providers: tuple[LlmConfidenceProviderMetrics, ...]¶
- total_classified: int¶
- total_low_confidence: int¶
- build_llm_diagnostics_report(*, since=None, until=None, low_confidence_threshold=Decimal('0.5'), usage_recorder=None, bucket_id=None, transactions=None)[source]¶
Aggregate the existing usage and confidence metric stores into a report.
- Parameters:
since (
date|None) – Inclusive lower usage-record date bound, orNone.until (
date|None) – Inclusive upper usage-record date bound, orNone.low_confidence_threshold (
Decimal) – Confidence floor below which a classification counts as low-confidence.usage_recorder (
UsageRecorder|None) – Injected recorder; defaults to the active-bucketUsageRecorder.bucket_id (
str|None) – Ledger bucket whoseTransactionCatalogueRepositorysupplies confidence rows; defaults to the active bucket. Ignored whentransactionsis supplied.transactions (
Iterable[Transaction] |None) – Injected transaction iterable; whenNonethe active (orbucket_id) ledger catalogue is loaded.
- Return type:
- Returns:
The populated
LlmDiagnosticsReport.