Expand description
latch-billing - Pure synchronous token billing core library.
This crate defines the core types, traits, and pricing models for token-based billing. It is purely synchronous and has zero I/O dependencies.
§Architecture
- Types:
UsageObservation,MeterSet,BillingSubject, etc. - Traits:
PricingSource,RatingEngine,ObservationStore,RatedRecordStore,RatedRecordExporter,QuotaAuthorizer, etc. - Design principle: Runtime-agnostic sync library. Async I/O should be implemented by downstream consumers (e.g., gateway applications).
§Quick start
ⓘ
use latch_billing::*;
// 1. Create an observation
let mut meter_set = MeterSet::new();
meter_set.accumulate(MeterKind::InputTokens, 1000).unwrap();
let observation = UsageObservation {
event_id: UsageEventId::from_attempt("req-123", 0, "openai").unwrap(),
subject: BillingSubject::default(),
meter_set,
model_ref: ModelRef { ... },
provider_ref: Some(ProviderRef { provider_id: "openai".to_string() }),
source: UsageSource::ProviderReported,
outcome: UsageOutcome::Success,
timing: UsageTiming { ... },
correlation: CorrelationIds::default(),
attributes: Attributes::new(),
};
// 2. Get a price snapshot (push mode - caller fethes)
let snapshot = ...; // fetched by caller (e.e., from DB)
// 3. Create rating context (for tier-based pricing)
let context = RatingContext::default();
// 4. Rate the observation
let engine = DefaultRatingEngine::new();
let rated = engine.rate(&observation, &snapshot, &context)?;
println!("Cost: {} {}", rated.rating.total_cost, rated.rating.currency.0);§Module structure
observation:UsageObservation,MeterSet,MeterKind,UsageSource,UsageOutcome,Attributesidentity:BillingSubject,UsageEventId,UsageEventIdBuilder,CorrelationIdspricing:ModelRef,ProviderRef,PriceSnapshot,PricingSourcetrait,TierConfig,TierBaselinerating:RatedUsageRecord,RatingResult,RatedLineItem,RatingEnginetrait,RatingContextstorage:ObservationStoretrait,RatedRecordStoretrait,StoreResultquota:QuotaAuthorizertrait,QuotaReservatortrait (Phase 2)export:RatedRecordExportertrait
Re-exports§
pub use observation::Attributes;pub use observation::AttributeError;pub use observation::CurrencyCode;pub use observation::CurrencyCodeError;pub use observation::MeterKind;pub use observation::MeterSet;pub use observation::MeterSetError;pub use observation::UsageObservation;pub use observation::UsageOutcome;pub use observation::UsageSource;pub use observation::UsageTiming;pub use identity::BillingSubject;pub use identity::CorrelationIds;pub use identity::UsageEventId;pub use identity::UsageEventIdBuilder;pub use identity::UsageEventIdError;pub use pricing::AccumulationScope;pub use pricing::MeterPrice;pub use pricing::ModelRef;pub use pricing::PriceSnapshot;pub use pricing::PricingError;pub use pricing::PricingSource;pub use pricing::ProviderRef;pub use pricing::TierBaseline;pub use pricing::TierBoundary;pub use pricing::TierConfig;pub use rating::RatedLineItem;pub use rating::RatedUsageRecord;pub use rating::RatingContext;pub use rating::RatingEngine;pub use rating::RatingError;pub use rating::RatingResult;pub use storage::ObservationStore;pub use storage::RatedRecordStore;pub use storage::StoreError;pub use storage::StoreResult;pub use quota::QuotaAuthorizer;pub use quota::QuotaDecision;pub use quota::QuotaError;pub use quota::QuotaRequest;pub use quota::QuotaReservator;pub use quota::Reservation;pub use quota::ReservationRequest;pub use quota::UsageAmount;pub use export::ExportError;pub use export::RatedRecordExporter;pub use chrono;pub use rust_decimal;
Modules§
- export
- Export module - defines the
RatedRecordExportertrait. - identity
- Identity module - defines billing subjects, correlation IDs, and idempotency keys.
- observation
- Observation module - defines the raw usage observation types.
- pricing
- Pricing module - defines pricing models and the
PricingSourcetrait. - quota
- Quota module - defines traits for quota enforcement.
- rating
- Rating module - defines rated usage records and the
RatingEnginetrait. - storage
- Storage module - defines traits for persisting observations and rated records.