Skip to content

fix: wire factory CreditLedger into spend and rate-limit recording #31

Closed

Description

@stackbilt-admin

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.ledger is checked before dispatch in src/factory.ts, but the factory constructor always assigns this.costTracker = defaultCostTracker.
  • CostTracker already supports delegating to a CreditLedger when constructed with one, but the factory does not use that path.
  • generateResponse() calls this.costTracker.trackCost(providerName, response) only when costOptimization is enabled, so even cost recording itself is coupled to optimization mode.

Micro: Concrete code paths

  • src/factory.ts:90-95: constructor ignores config.ledger when 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 through this.costTracker.trackCost().
  • README.md:155-160: documents that passing ledger makes the factory track spend.

Impact

  • RPM/RPD windows configured on CreditLedger never 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 call config.ledger.record() after a successful provider response.
  • Decouple ledger recording from costOptimization; spend and rate-limit accounting should work whenever ledger is configured.
  • Consider checking and recording tpm / tpd as well as rpm / rpd, since CreditLedger supports 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 typecheck passes.
  • npm test passes: 169 tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions