pub struct BudgetEngine { /* private fields */ }Expand description
Enforces the configured LLM budgets over a [Store]. Built once per config (re)load and carried
on the proxy Runtime.
Implementations§
Source§impl BudgetEngine
impl BudgetEngine
Sourcepub fn build(cfg: &LlmCfg) -> Result<Option<BudgetEngine>>
pub fn build(cfg: &LlmCfg) -> Result<Option<BudgetEngine>>
Build from [llm] config when budgets are present. Returns Ok(None) when no budgets are
configured (the engine is then absent and the proxy skips enforcement entirely).
Sourcepub async fn reserve(&self, dims: Dims<'_>, estimate: Spend) -> Reserved
pub async fn reserve(&self, dims: Dims<'_>, estimate: Spend) -> Reserved
Reserve estimate against every budget that applies to dims. Reserves in order; on the first
denial, releases everything already reserved and returns Reserved::Denied, so a partial
reservation never leaks. Budgets that don’t match the scope are skipped. The returned
Reservation carries each admitted budget’s post-reserve consumed ratio for the near-limit gauge.
Sourcepub async fn reconcile(&self, reservation: &Reservation, actual: Spend) -> usize
pub async fn reconcile(&self, reservation: &Reservation, actual: Spend) -> usize
Reconcile a held reservation to the actual spend: for each budget, apply actual − reserved
in that budget’s unit (releasing the over-estimate, or charging a low one). Idempotent per
reservation (the settle marker dedupes a retry). Returns the number of budgets whose settle
failed against the store — non-zero means the distributed counter has drifted, which the
caller records to edgeguard_llm_budget_reconcile_failures_total.
Sourcepub async fn release(&self, reservation: &Reservation) -> usize
pub async fn release(&self, reservation: &Reservation) -> usize
Release a reservation in full (actual spend was zero — upstream error / no usage produced).
Returns the count of failed settles (drift), like Self::reconcile.