Class RedbCostBudgetStore

Assembly: redb.Route.Llm.dll

REDB-backed . One
row per conversation, keyed on the conversation id stored in
_objects.value_string (partial index on PostgreSQL/SQLite; MSSQL cannot
index NVARCHAR(MAX)) and, normalized, in _objects._value_unique — the
per-scheme unique index guarantees ONE budget row per conversation even under
concurrent first writes.

uses the framework's pessimistic-locking primitives
(redb.Context.ExecuteAtomicAsync + redb.LockForUpdateAsync) so
concurrent updates from multiple processes / nodes serialise on the row
instead of trampling each other. No raw SQL.


The store does not own an instance. A call with an
exchange resolves the exchange's service through
IRouteContext.GetRedbService(name, exchange); a call without one (the
budget enforcer's, made from every concurrent run) opens a scope of its own with
IRouteContext.CreateRedbScope(name) and releases it when the call ends. The
redb name is read from exchange.Properties[LlmKeys.RedbName] (set by the LLM
endpoint URI), falling back to the constructor-supplied default name and then to the
host's default unnamed instance.

public sealed class RedbCostBudgetStore : ICostBudgetStore

Inheritance

Object→RedbCostBudgetStore

Implements

Methods

AddAsync(string, AgentUsage, IExchange?, CancellationToken)

Atomically adds the iteration usage to the running total and returns the updated total.

public ValueTask<AgentUsage> AddAsync(string conversationId, AgentUsage delta, IExchange? exchange = null, CancellationToken ct = default)

GetUsageAsync(string, IExchange?, CancellationToken)

Returns the running total for the conversation, or when unknown.

public ValueTask<AgentUsage> GetUsageAsync(string conversationId, IExchange? exchange = null, CancellationToken ct = default)

ResetAsync(string, IExchange?, CancellationToken)

Resets the running total — used after a billing period rollover or by tests.

public ValueTask ResetAsync(string conversationId, IExchange? exchange = null, CancellationToken ct = default)

Constructors

RedbCostBudgetStore(IRouteContext, string?)

Creates the store. Scheme is synced by the host's redb.InitializeAsync().