genai_estimate_tokens
This page explains how to use the genai_estimate_tokens function in APL.
The genai_estimate_tokens function estimates the number of tokens in a text string. This estimation helps you predict API costs, validate input sizes, and monitor token usage before making actual API calls to LLM services.
You can use this function to validate prompt sizes, estimate costs before API calls, monitor content length, or analyze token efficiency across different prompts.
Usage
Syntax
APL
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| text | string | Yes | The text string for which you want to estimate the token count. |
Returns
Returns a long integer representing the estimated number of tokens in the input text.
Example
Estimate the number of tokens in a GenAI conversation prompt.
Query
Output
| avg_tokens | max_tokens |
|---|---|
| 245 | 1024 |
This query analyzes prompt token usage patterns, helping you predict costs and validate input sizes.
List of related functions
- genai_cost: Calculates the actual cost based on token usage. Use this in combination with token estimates to predict costs.
- strlen: Returns string length in characters. Use this for a simpler character count without token estimation.
- string_size: Returns string length in characters. Use this when you need character count instead of token count.
- genai_input_cost: Calculates input token cost. Combine with token estimation to predict prompt costs.