Skip to main content
from praisonaiagents import Agent

agent = Agent(name="estimator", instructions="Estimate and validate context token usage.")
agent.start("How many tokens will this 5000-word document use?")
Token estimation validation compares heuristic estimates against accurate counts, logging mismatches for debugging. The user monitors context size; validated estimation compares heuristics to accurate counts and logs mismatches.

How It Works

Quick Start

1

Enable validated estimation

from praisonaiagents import ContextManager, ManagerConfig, EstimationMode

config = ManagerConfig(
    estimation_mode=EstimationMode.VALIDATED,
    log_estimation_mismatch=True,
    mismatch_threshold_pct=15.0,
)

manager = ContextManager(model="gpt-4o-mini", config=config)
tokens, metrics = manager.estimate_tokens(text, validate=True)
2

Review mismatch logs

praisonai chat
> /context config

Estimation Modes

ModeDescriptionPerformance
HEURISTICFast character-based estimateFastest
ACCURATEUse tiktoken if availableSlower
VALIDATEDCompare both, log mismatchesSlowest

Configuration

config = ManagerConfig(
    estimation_mode=EstimationMode.VALIDATED,
    log_estimation_mismatch=True,      # Log when mismatch > threshold
    mismatch_threshold_pct=15.0,       # 15% threshold
)

Environment Variables

export PRAISONAI_CONTEXT_ESTIMATION_MODE=validated
export PRAISONAI_CONTEXT_LOG_MISMATCH=true

EstimationMetrics

@dataclass
class EstimationMetrics:
    heuristic_estimate: int    # Fast estimate
    accurate_estimate: int     # Tiktoken count
    error_pct: float          # Percentage error
    estimator_used: EstimationMode

Mismatch Logging

When log_estimation_mismatch=True and error exceeds threshold:
WARNING: Token estimation mismatch: heuristic=1250, accurate=1100, error=13.6%

Estimation Caching

Estimates are cached by content hash:
# First call - computes estimate
tokens1, _ = manager.estimate_tokens(text)

# Second call - uses cache
tokens2, _ = manager.estimate_tokens(text)

# Cache key is MD5 hash of text

Heuristic Algorithm

The heuristic uses character-based estimation:
# ASCII characters: ~0.25 tokens per char
# Non-ASCII: ~1.3 tokens per char
# Plus overhead for message structure

Accurate Estimation

When tiktoken is available:
# Uses model-specific tokenizer
# Falls back to heuristic if unavailable

CLI Usage

# View estimation mode in config
praisonai chat
> /context config

# Shows:
# Estimation:
#   estimation_mode:        validated
#   log_mismatch:           True

Best Practices

Heuristic estimation is fast and sufficient for most runs — reserve validated mode for debugging.
Fifteen to twenty percent is a typical threshold before logging estimation drift.
Spikes often indicate unusual Unicode, code blocks, or tool payloads — fix content, not just the estimator.
Model-specific tokenisers improve accuracy for billing-sensitive workloads.

Token Estimation

Fast offline token counting

Context Observability

Track optimisation events and history