adachsoft / agent-usage-report
Generates structured JSON reports for agent model usage across workflows, conversations, and tasks.
Requires
- php: ^8.3
- ext-bcmath: *
- adachsoft/model-usage-ledger: ^0.1.0
Requires (Dev)
- adachsoft/php-code-style: ^0.8.0
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13
- rector/rector: ^2.9
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
adachsoft/agent-usage-report builds a versioned JSON report from the model usage ledger and completed agent-run log. It is framework-independent and has no dependency on application, workflow, or task classes.
Main capabilities
- Generate reports for workflow runs, conversations (including recursively related child conversations), and tasks.
- Aggregate model and tool calls, token usage, exact decimal costs, cache misses, agent runs, and elapsed duration.
- Represent agent runs as a hierarchy with per-run and descendant-inclusive totals.
- Serialize reports to the documented JSON v1 format, with a matching JSON Schema in
schema/.
Installation
Install the package with Composer:
composer require adachsoft/agent-usage-report
Requirements
- PHP 8.3 or later.
- The BCMath extension.
adachsoft/model-usage-ledgerfor the ledger and completed-run log contracts.
Creating and serializing a report
Inject AgentUsageReportGeneratorInterface and AgentUsageReportJsonSerializer:
$report = $generator->forWorkflowRun($rootRunId);
$json = $serializer->serialize($report);
The generator also provides forConversation($conversationId) and forTask($taskId). Workflow-run and task scopes use their corresponding ledger identifiers. A conversation scope includes entries in that conversation and recursively includes entries whose parent_conversation_id points into the selected conversation subtree.
JSON format
The serializer emits agent-usage-report/v1; its JSON Schema is available at schema/agent-usage-report-v1.schema.json.
The report contains:
- A scope, generation timestamp, and period derived from usage entries and run records.
- A summary of model calls, tool calls, tokens, costs, agent runs, cache misses, and elapsed period duration.
- Per-agent totals, including subagents.
- A run tree grouped by
agent_run_id, with each node's own usage (self) and usage including descendants (total).
Costs are decimal strings summed exactly with BCMath and grouped by currency. A usage entry without a cost amount makes its aggregate's cost_is_complete false. A cache miss is a model call whose cached input is less than half the input-token count of the previous call in the same conversation, ordered by recorded_at; the first call in a conversation is not a miss. A missing cached-token count is treated as zero. cache_miss_cost contains the exact cost of calls classified as cache misses, grouped by currency. Legacy entries without an agent_run_id are combined into a synthetic unknown run for each conversation and agent pair. Missing run-log records do not prevent report generation; run metadata is null when unavailable.
Dependencies
The package depends only on adachsoft/model-usage-ledger and BCMath. Applications provide that package's ledger and run-log implementations; this package does not read application, workflow, or task repositories directly.