> ## Documentation Index
> Fetch the complete documentation index at: https://praison.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Estimate Tokens • AI Agent SDK

> estimate_tokens: Estimate tokens with optional validation.

# estimate\_tokens

<div className="flex items-center gap-2">
  <Badge color="purple">Method</Badge>
</div>

> This is a method of the [**ContextManager**](../classes/ContextManager) class in the [**manager**](../modules/manager) module.

Estimate tokens with optional validation.

## Signature

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
def estimate_tokens(text: str, validate: bool) -> Tuple[int, Optional[EstimationMetrics]]
```

## Parameters

<ParamField query="text" type="str" required={true}>
  Text to estimate
</ParamField>

<ParamField query="validate" type="bool" required={false} default="False">
  Whether to validate against accurate count
</ParamField>

### Returns

<ResponseField name="Returns" type="Tuple[int, Optional[EstimationMetrics]]">
  Tuple of (token\_count, metrics)
</ResponseField>

## Uses

* `hexdigest`
* `hashlib.md5`
* `text.encode`
* `estimate_tokens_heuristic`
* `estimate_tokens_accurate`
* `EstimationMetrics`
* `warning`
* `logging.getLogger`

## Used By

* [`ContextManager.truncate_tool_output`](../functions/ContextManager-truncate_tool_output)

## Source

<Card title="View on GitHub" icon="github" href="https://github.com/MervinPraison/PraisonAI/blob/main/src/praisonai-agents/praisonaiagents/context/manager.py#L707">
  `praisonaiagents/context/manager.py` at line 707
</Card>
