adachsoft / model-usage-ledger-bridge
Adapters that record AI model usage into model-usage-ledger: chat facade, agent facade and workflow step decorators
Package info
gitlab.com/a.adach/model-usage-ledger-bridge
pkg:composer/adachsoft/model-usage-ledger-bridge
Requires
- php: ^8.3
- adachsoft/agent-usage-report: ^0.1.0
- adachsoft/ai-agent: ^0.14
- adachsoft/ai-agent-config-repository: ^0.6
- adachsoft/ai-agent-factory: ^0.3
- adachsoft/ai-agent-run-contracts: ^0.1.0
- adachsoft/ai-conversation-model: ^0.2
- adachsoft/ai-integration: ^0.11
- adachsoft/ai-tool-call: ^2.3
- adachsoft/model-usage-ledger: ^0.1.0
- adachsoft/workflow: ^7.0
- adachsoft/workflow-symfony-cli: ^0.8
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/model-usage-ledger-bridge connects model usage capture to the AI integration, agent, and workflow packages. It records successful model responses with their provider, model, token counts, cost metadata, request duration, and the correlation values active at the time of the request. Failed model calls that return no response are not recorded. Ledger write failures do not change a successful chat response; an optional error callback can observe those failures.
Key capabilities
- Record successful model responses with token, cost, duration, provider, and model metadata.
- Attach shared conversation, agent, project, origin, and workflow correlation to usage records.
- Decorate agent factories and workflow steps while preserving the interfaces supported by the wrapped implementations.
- Keep successful chat responses successful when a ledger write fails; optionally observe write failures through a callback.
Installation
Install the package with Composer:
composer require adachsoft/model-usage-ledger-bridge
Requirements
- PHP 8.3 or later.
- Composer.
- The AdachSoft AI integration, agent, workflow, and model usage ledger packages required by the integrations you use.
Wiring
Register one shared UsageScope in dependency injection and use it for all bridge decorators:
- Decorate
ToolCallingChatFacadeInterfacewithUsageRecordingToolCallingChatFacadeso every successful chat response is written toModelUsageLedgerInterface. - Decorate agents produced by
AiAgentFromConfigFactoryInterfacewithUsageScopedAiAgentFromConfigFactory. ItsUsageScopedAiAgentFacadeadds the conversation and agent identifiers, plus optional project and origin values, while an agent operation is running. - Apply
UsageScopedStepDecoratorFactoryto registered workflow steps. The factory preserves the step'sConfigDependentStepDefinitionInterfaceandInteractiveStepInterfacecontracts when present.
Use the same UsageScope instance in the chat, agent, and workflow decorators. Bind the resulting decorators at the corresponding interfaces in the application's DI container so calls pass through the recording facade and scoped wrappers.
Workflow correlation
Each decorated step pushes its workflow run identifier and step name. The first run in a scope becomes workflowRootRunId; nested runs replace workflowRunId and stepId while retaining that root. Subworkflows executed in the same process therefore inherit the root workflow correlation through the scope stack. Frames are always popped in a finally block, including when a step or agent throws.