Skip to content

ContextWindowEstimator

Namespace AgentPrism · Assembly AgentPrism.Core.dll

Estimates a prompt’s token count and whether it would be rejected by the pre-flight context-window check, without calling a model provider.

public sealed class ContextWindowEstimator

objectContextWindowEstimator

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

The estimate is approximate: counting is done with a single fixed OpenAI encoding (o200k_base, via Tokenizers.TiktokenTokenizer) regardless of the bound provider — Anthropic and Google publish no equivalent offline tokenizer package. The count is close enough to size a prompt against a context window; it is not the exact count any specific provider would bill.

The tokenizer is built ONCE and reused: constructing one loads the vocab data package’s contents, and doing that per call would make even a cheap estimate expensive.

ContextWindowEstimator(IModelProviderRegistry, IOptionsMonitor<AgentPrismOptions>)

Section titled “ ContextWindowEstimator(IModelProviderRegistry, IOptionsMonitor<AgentPrismOptions>)”

Creates a new estimator.

public ContextWindowEstimator(IModelProviderRegistry registry, IOptionsMonitor<AgentPrismOptions> optionsMonitor)

registry IModelProviderRegistry

The registry used to look up the model’s context window.

optionsMonitor IOptionsMonitor<AgentPrismOptions>

The runtime settings, for AgentPrismPreflightOptions.ReserveRatio.

ArgumentNullException

A dependency is null.

Estimates prompt against binding’s context window.

public ContextWindowEstimate Estimate(ModelBinding binding, string? prompt)

binding ModelBinding

The model binding whose context window applies.

prompt string?

The prompt text to count. Empty when null.

ContextWindowEstimate

The estimate.

ArgumentNullException

binding is null.