Path: redb-route/redb.Route.Llm/Engine/Storage/ICostBudgetStore.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Llm/Engine/Storage/ICostBudgetStore.cs
----
using System.Collections.Concurrent;
using redb.Route.Abstractions;
using redb.Route.Llm.Engine.Governance;
namespace redb.Route.Llm.Engine.Storage;
///
/// Persists running cost / token usage per conversation so that an
/// implementation can enforce budgets across multiple agent runs
/// (not just within one).
///
/// is that consumer: AddRedbLlmStorage() registers it in
/// place of the shipped per-run defaults, and it reads this store before each run and adds to it after
/// each iteration. Implementations must be safe for concurrent updates.
///
///
/// The optional exchange parameter on every method carries the route
/// pipeline's current exchange; REDB-backed implementations resolve a
/// per-exchange through
/// IRouteContext.GetRedbService(name, exchange) using the named-redb
/// hint stored in . In-memory implementations
/// ignore it.
/// Without an exchange a redb-backed implementation opens a redb scope of its own for the call. That scope joins an
/// ambient (a , a
/// route's .Transacted()) as every redb connection does, but not a transaction begun on the context's shared
/// IRedbService (ExecuteAtomicAsync, BeginTransaction), which it does not see: pass the exchange,
/// or use a TransactionScope, when the call must commit or roll back with that work. In-memory implementations
/// take part in no transaction.
///
///
public interface ICostBudgetStore
{
/// Returns the running total for the conversation, or when unknown.
ValueTask GetUsageAsync(string conversationId, IExchange? exchange = null, CancellationToken ct = default);
/// Atomically adds the iteration usage to the running total and returns the updated total.
ValueTask AddAsync(string conversationId, AgentUsage delta, IExchange? exchange = null, CancellationToken ct = default);
/// Resets the running total — used after a billing period rollover or by tests.
ValueTask ResetAsync(string conversationId, IExchange? exchange = null, CancellationToken ct = default);
}
/// In-memory cost/token store. Replace with the redb-backed implementation in production.
public sealed class InMemoryCostBudgetStore : ICostBudgetStore
{
private readonly ConcurrentDictionary _totals = new(StringComparer.Ordinal);
///
public ValueTask GetUsageAsync(string conversationId, IExchange? exchange = null, CancellationToken ct = default) =>
ValueTask.FromResult(_totals.TryGetValue(conversationId, out var u) ? u : AgentUsage.Zero);
///
public ValueTask AddAsync(string conversationId, AgentUsage delta, IExchange? exchange = null, CancellationToken ct = default)
{
var updated = _totals.AddOrUpdate(conversationId, delta, (_, current) => current.Add(delta));
return ValueTask.FromResult(updated);
}
///
public ValueTask ResetAsync(string conversationId, IExchange? exchange = null, CancellationToken ct = default)
{
_totals.TryRemove(conversationId, out _);
return ValueTask.CompletedTask;
}
}