Path: redb-route/redb.Route.Llm/Engine/Governance/IBudgetEnforcer.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/Governance/IBudgetEnforcer.cs ---- using redb.Route.Abstractions; namespace redb.Route.Llm.Engine.Governance; /// /// Enforces token and cost budgets on agent runs. Called before and after each /// provider call so the engine can stop early or refuse an iteration when the /// budget is exhausted. /// public interface IBudgetEnforcer { /// /// Checks whether the agent run is allowed to continue at the start of an /// iteration. Implementations may inspect /// against a configured budget and return . /// /// Conversation identifier (null when no persistence). /// Per-run budget (zero or negative components mean unlimited). /// Aggregated usage across iterations completed so far. /// Cancellation token. ValueTask PreCheckAsync( string? conversationId, AgentBudget budget, AgentUsage usageSoFar, CancellationToken ct = default); /// /// Records usage produced by the iteration and returns a decision whether the agent may /// continue. The engine passes per-iteration and running totals; accumulating them on an /// is up to the implementation — see /// , the one shipped enforcer that does. /// ValueTask RecordAndCheckAsync( string? conversationId, AgentBudget budget, AgentUsage iterationUsage, AgentUsage totalUsage, CancellationToken ct = default); /// /// with the run's exchange, which /// the engine calls: an enforcer that stores usage reads it from the instance the endpoint chose /// (?redb=). The default ignores the exchange, so an enforcer that stores nothing implements only the /// member without it. /// ValueTask PreCheckAsync( string? conversationId, AgentBudget budget, AgentUsage usageSoFar, IExchange? exchange, CancellationToken ct = default) => PreCheckAsync(conversationId, budget, usageSoFar, ct); /// /// with the /// run's exchange, which the engine calls: an enforcer that stores usage writes it to the instance the endpoint /// chose (?redb=). The default ignores the exchange. /// ValueTask RecordAndCheckAsync( string? conversationId, AgentBudget budget, AgentUsage iterationUsage, AgentUsage totalUsage, IExchange? exchange, CancellationToken ct = default) => RecordAndCheckAsync(conversationId, budget, iterationUsage, totalUsage, ct); } /// Per-run budget. Components ≤ 0 disable that dimension of enforcement. public readonly record struct AgentBudget(int MaxInputTokens, int MaxOutputTokens, decimal MaxCostUsd) { /// Unbounded budget — used when caller didn't supply one. public static readonly AgentBudget Unbounded = new(0, 0, 0m); /// True when every dimension is disabled (Unbounded). public bool IsUnbounded => MaxInputTokens <= 0 && MaxOutputTokens <= 0 && MaxCostUsd <= 0m; } /// Running usage measured against an . public readonly record struct AgentUsage(int InputTokens, int OutputTokens, decimal CostUsd) { /// Zero usage — used as the running total starting point. public static readonly AgentUsage Zero = new(0, 0, 0m); /// Adds two usages component-wise. public AgentUsage Add(AgentUsage other) => new(InputTokens + other.InputTokens, OutputTokens + other.OutputTokens, CostUsd + other.CostUsd); } /// Outcome of a budget enforcement decision. public readonly record struct BudgetDecision(bool Continue, string? Reason) { /// Allows the iteration to proceed. public static readonly BudgetDecision Allow = new(true, null); /// Stops the agent run with a human-readable reason. public static BudgetDecision Stop(string reason) => new(false, reason); } /// Default no-op budget enforcer — always allows execution. public sealed class NoopBudgetEnforcer : IBudgetEnforcer { /// public ValueTask PreCheckAsync( string? conversationId, AgentBudget budget, AgentUsage usageSoFar, CancellationToken ct = default) => ValueTask.FromResult(BudgetDecision.Allow); /// public ValueTask RecordAndCheckAsync( string? conversationId, AgentBudget budget, AgentUsage iterationUsage, AgentUsage totalUsage, CancellationToken ct = default) => ValueTask.FromResult(BudgetDecision.Allow); } /// /// In-process budget enforcer — checks components against the running /// total. Does NOT persist between runs and takes no store: cross-run budgets need a custom /// over an . /// public sealed class InMemoryBudgetEnforcer : IBudgetEnforcer { /// public ValueTask PreCheckAsync( string? conversationId, AgentBudget budget, AgentUsage usageSoFar, CancellationToken ct = default) { if (budget.IsUnbounded) return ValueTask.FromResult(BudgetDecision.Allow); return ValueTask.FromResult(Check(budget, usageSoFar)); } /// public ValueTask RecordAndCheckAsync( string? conversationId, AgentBudget budget, AgentUsage iterationUsage, AgentUsage totalUsage, CancellationToken ct = default) { if (budget.IsUnbounded) return ValueTask.FromResult(BudgetDecision.Allow); return ValueTask.FromResult(Check(budget, totalUsage)); } private static BudgetDecision Check(AgentBudget budget, AgentUsage usage) { if (budget.MaxInputTokens > 0 && usage.InputTokens >= budget.MaxInputTokens) return BudgetDecision.Stop($"input-token budget exceeded ({usage.InputTokens}/{budget.MaxInputTokens})"); if (budget.MaxOutputTokens > 0 && usage.OutputTokens >= budget.MaxOutputTokens) return BudgetDecision.Stop($"output-token budget exceeded ({usage.OutputTokens}/{budget.MaxOutputTokens})"); if (budget.MaxCostUsd > 0m && usage.CostUsd >= budget.MaxCostUsd) return BudgetDecision.Stop($"cost budget exceeded ({usage.CostUsd:F4}/{budget.MaxCostUsd:F4} USD)"); return BudgetDecision.Allow; } }