TokenEstimator
Simple token estimator for LLM context management. Uses character-based estimation (4 chars ≈ 1 token for English). This is an approximation - actual tokenization varies by model. For more accurate counting, integrate with: - tiktoken (OpenAI's tokenizer) - Anthropic's token counting API
TokenEstimator::estimateTokens()
public function estimateTokens(string $text): intEstimate token count for a string.
Parameters
| Name | Type | Description |
|---|---|---|
| $text | string |
TokenEstimator::estimateToolTokens()
public function estimateToolTokens(array $tool): intEstimate tokens for a tool definition.
Parameters
| Name | Type | Description |
|---|---|---|
| $tool | array{name: string, description: string, inputSchema: array<mixed>} |
TokenEstimator::estimateToolsTokens()
public function estimateToolsTokens(array $tools): arrayEstimate tokens for multiple tools.
Parameters
| Name | Type | Description |
|---|---|---|
| $tools | array<array{name: string, description: string, inputSchema: array<mixed>}> |
TokenEstimator::getModelLimit()
public function getModelLimit(string $model): intGet context limit for a model.
Parameters
| Name | Type | Description |
|---|---|---|
| $model | string |
TokenEstimator::getToolBudget()
public function getToolBudget(string $model): intGet recommended token budget for tools based on model.
Parameters
| Name | Type | Description |
|---|---|---|
| $model | string |
TokenEstimator::checkToolBudget()
public function checkToolBudget(array $tools, string $model): arrayCheck if tools fit within budget.
Parameters
| Name | Type | Description |
|---|---|---|
| $tools | array<array{name: string, description: string, inputSchema: array<mixed>}> | |
| $model | string |
TokenEstimator::selectToolsWithinBudget()
public function selectToolsWithinBudget(array $tools, string $model): arraySelect tools that fit within budget, prioritizing by order.
Parameters
| Name | Type | Description |
|---|---|---|
| $tools | array<array{name: string, description: string, inputSchema: array<mixed>}> | |
| $model | string |
TokenEstimator::getUsageReport()
public function getUsageReport(array $tools, string $model): stringGet a summary report of token usage.
Parameters
| Name | Type | Description |
|---|---|---|
| $tools | array<array{name: string, description: string, inputSchema: array<mixed>}> | |
| $model | string |