Estimate Tokens • AI Agent SDK

estimate_tokens: Estimate tokens with optional validation.

estimate_tokens

Method

</div>

This is a method of the ContextManager class in the manager module.

Estimate tokens with optional validation.

Signature

python
def estimate_tokens(text: str, validate: bool) -> Tuple[int, Optional[EstimationMetrics]]

Parameters

ParamField str

Text to estimate

ParamField bool

Whether to validate against accurate count

Returns

ResponseField Tuple[int, Optional[EstimationMetrics]]

Tuple of (token_count, metrics)

Uses

  • estimate_tokens_heuristic
  • hexdigest
  • hashlib.sha256
  • text.encode
  • estimate_tokens_accurate
  • EstimationMetrics
  • warning
  • get_logger

Used By

Source

View on GitHub

praisonaiagents/context/manager.py at line 647