Context Estimation
The SDK estimates how much of the model’s context window is in use so it can trigger compression before overflow. Three pieces work together: the context window limit, token counting, and utilization estimation.
Context window limit
Section titled “Context window limit”The threshold check requires the model’s context window
size. The SDK auto-populates
context_window_limitcontextWindowLimit
model = BedrockModel( model_id="my-custom-model", context_window_limit=128_000,)import { BedrockModel } from '@strands-agents/sdk'
const model = new BedrockModel({ modelId: 'my-custom-model', contextWindowLimit: 128_000,})Token estimation
Section titled “Token estimation”The agent estimates input tokens using the following strategy:
- Known baseline: reads
from the last assistant message’s usage metadatainput_tokens + output_tokensinputTokens + outputTokens - Delta estimation: estimates tokens for new messages
added since that baseline using the model’s
methodcount_tokens()countTokens() - Cold start fallback: when no prior usage metadata
exists (first call or after session restore), estimates
all messages via
count_tokens()countTokens()
The
count_tokens()countTokens()
Utilization estimation
Section titled “Utilization estimation”The Model base class provides an
estimate_utilization(input_tokens)estimateUtilization(inputTokens)
ratio = model.estimate_utilization( input_tokens=projected_tokens,)# ratio is 0-1+ (above 1.0 means overflow)// Section "utilization_imports" not found in user-guide/concepts/context-management/context-estimation_imports.ts
const ratio = model.estimateUtilization(projectedTokens)// ratio is 0-1+ (above 1.0 means overflow)The method divides
input_tokensinputTokenscontext_window_limitcontextWindowLimit
Both the built-in context management modes and the conversation managers use this internally for compression decisions. You can also call it directly when building custom logic.