Repository navigation
Closed
Description
Summary
Passing a CreditLedger into LLMProviderFactory currently only gates dispatch with ledger.checkRateLimit(). Successful requests are recorded into the factory's defaultCostTracker, not into the caller-supplied ledger, so the ledger never observes spend or request/token usage from normal factory calls.
This breaks the public contract in the README: ledger, // Factory enforces rate limits and tracks spend.
Fractal Decomposition
Macro: Cost controls and trust
- The package is positioned as the canonical LLM infrastructure layer for cost tracking, budget alerts, and rate-limit enforcement.
- If the ledger is not updated after successful calls, consumers can believe they have active budget/rate-limit protection while all counters remain stale.
- This is especially risky for Cloudflare Worker deployments where the factory may sit on the hot path for every tenant request.
Meso: Factory, CostTracker, and CreditLedger wiring
ProviderFactoryConfig.ledgeris checked before dispatch insrc/factory.ts, but the factory constructor always assignsthis.costTracker = defaultCostTracker.CostTrackeralready supports delegating to aCreditLedgerwhen constructed with one, but the factory does not use that path.generateResponse()callsthis.costTracker.trackCost(providerName, response)only whencostOptimizationis enabled, so even cost recording itself is coupled to optimization mode.
Micro: Concrete code paths
src/factory.ts:90-95: constructor ignoresconfig.ledgerwhen creating the cost tracker.src/factory.ts:164-176: rate limits are checked before dispatch.src/factory.ts:216-219: successful usage is recorded only throughthis.costTracker.trackCost().README.md:155-160: documents that passingledgermakes the factory track spend.
Impact
- RPM/RPD windows configured on
CreditLedgernever advance from factory-generated requests, so rate-limit enforcement can remain permanently permissive. - Budget thresholds and depletion projections do not fire from factory traffic.
ledger.snapshot()will not reflect actual LLM usage unless consumers manually duplicate recording outside the factory.
Suggested Fix
- Make the factory's tracker instance ledger-aware, for example
new CostTracker({}, config.ledger, this.logger), or explicitly callconfig.ledger.record()after a successful provider response. - Decouple ledger recording from
costOptimization; spend and rate-limit accounting should work wheneverledgeris configured. - Consider checking and recording
tpm/tpdas well asrpm/rpd, sinceCreditLedgersupports all four dimensions. - Add tests proving that a successful
LLMProviderFactory.generateResponse()increments ledger request counts, token totals, rate-limit windows, and threshold events.
Verification
npm run typecheckpasses.npm testpasses: 169 tests.
Metadata
Metadata
Assignees
Labels
No labels
Activity