Skip to content

AgentRunBudget

Namespace AgentPrism · Assembly AgentPrism.Abstractions.dll

A run budget shared across an entire call tree.

public sealed class AgentRunBudget

objectAgentRunBudget

object.GetType(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()

This type is deliberately a class, not a record. The budget is shared mutable state: every run in the tree uses the same instance. A record invites copying; a copied budget would give each branch its own limit, and the limit would lose its meaning.

Counters increment lock-free (Threading.Interlocked). Child runs start concurrently: Microsoft Agent Framework’s background agents run without blocking, so the same budget is read and written from multiple threads.

The budget blocks new child runs; it does not interrupt a run in progress. A child run cut off midway would leave the model with an incomplete context and would also corrupt the root run.

public AgentRunBudget()

The tokens spent across the tree so far.

public long ConsumedTokens { get; }

long

Whether the token limit has been exceeded.

public bool IsTokenBudgetExhausted { get; }

bool

The maximum allowed call depth. The root run is 0, so the default value allows a three-layer tree.

public int MaxDepth { get; init; }

int

The maximum number of child runs that may start. The root run is not counted toward this number. If null, there is no count limit.

public int? MaxTotalRuns { get; init; }

int?

The maximum tokens spendable across the tree. If null, there is no token limit.

public long? MaxTotalTokens { get; init; }

long?

The number of child runs started so far.

public int StartedRuns { get; }

int

Produces a user-facing text describing the limit that was exceeded.

public string DescribeExhaustion()

string

Text stating which limit was exceeded.

The text is returned to the model as a tool result. Saying “budget exhausted” is not enough; unless the exceeded limit is named, the user cannot see which setting to raise.

Records the number of tokens spent into the budget.

public void RecordUsage(long tokens)

tokens long

The number of tokens to add. A negative value is ignored.

Reserves budget room for a new child run.

public bool TryReserveRun()

bool

true if room was reserved; false if the token or count limit has been exceeded.

The counter increments only when room is reserved. If a failed attempt incremented the counter, every new attempt in a tree that has hit its limit would keep growing the count, and the UI would show runs that never actually started.