Token Estimation Validation

Validate token estimates and track estimation accuracy

Diagram

Token Estimation Validation

📋 User Request

⚙️ Token Estimation Validation

✅ Result

Diagram
Token Estimation ValidationAgentUserToken Estimation ValidationAgentUserRequestProcess requestResultResponse
python
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.

Diagram

Token Estimation Validation

📝 Text

⚡ Heuristic Estimate

🔍 Validate vs Accurate

🤖 Agent

✅ Logged Metrics

How It Works

Diagram
TiktokenContextManagerAgentUserTiktokenContextManagerAgentUserSend messageestimate_tokens(validate=True)Accurate countCompare vs heuristicEstimate + mismatch log

Quick Start

  1. Enable validated estimation
    python
    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
    bash
    praisonai chat
    > /context config

Estimation Modes

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

Configuration

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

Environment Variables

bash
export PRAISONAI_CONTEXT_ESTIMATION_MODE=validated
export PRAISONAI_CONTEXT_LOG_MISMATCH=true

EstimationMetrics

python
@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

Caching depends on the estimation mode.

ModeCache behaviourWhy
Heuristic (default)Not cached, returned directlyHashing the text costs more than recomputing the heuristic
Validated (validate=True or EstimationMode.VALIDATED)Cached by SHA-256 content hashTiktoken counting is expensive enough that caching pays off
python
# Default (heuristic) - no cache, no hashing of text
tokens, _ = manager.estimate_tokens(long_tool_output)

# Validated - cached by SHA-256 of the input text
tokens, metrics = manager.estimate_tokens(long_tool_output, validate=True)

The cache holds up to 1000 entries; call manager.reset() to clear it.

Heuristic Algorithm

The heuristic uses character-based estimation:

python
# 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:

python
# Uses model-specific tokenizer
# Falls back to heuristic if unavailable

CLI Usage

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

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

Best Practices

Use heuristic mode in production

Heuristic estimation is fast and sufficient for most runs — reserve validated mode for debugging.

Set a sensible mismatch threshold

Fifteen to twenty percent is a typical threshold before logging estimation drift.

Monitor mismatch logs

Spikes often indicate unusual Unicode, code blocks, or tool payloads — fix content, not just the estimator.

Enable tiktoken when available

Model-specific tokenisers improve accuracy for billing-sensitive workloads.

Related