Count Tokens
Audience:
Low-code EngineersSkill Prerequisites:
Tokens
Counts how many AI model tokens a text uses for an OpenAI model, and saves the number in a token.
"Tokens" means two different things here. AI model tokens are the pieces of text an OpenAI model reads and writes. A token is often a short word or part of a word, about 4 characters of English text. OpenAI's limits and prices are counted in them. PAA tokens, such as [Content], are placeholders that hold values in your actions. This action counts AI model tokens in a text, and saves the count in a PAA token.
The count is calculated on your server. Nothing is sent to OpenAI, no connector is needed, and there's no cost.
This action is part of the AI add-on (PlantAnApp.OpenAi). The add-on is installed separately and needs the AI feature in your license. If it isn't licensed, the action fails with an "AI package is unlicensed" error. If you don't see the OpenAi actions, the add-on isn't installed.
Typical Use Cases
- Check whether a text fits a model's context window before you send it to Chat
- Choose between the
TruncateandSplitstrategies of Summarize Content - Estimate the cost of a request before running it
- Stop a request, or warn the user, when the input is too long
Don't use it to
- Count words or characters. AI model tokens don't match either.
- Get the exact number of tokens billed for a request. The provider also counts the instructions and message formatting. Use the usage data in the provider's response instead, for example the
Store JSON Responseoutput of Summarize Content. - Count tokens for Amazon Bedrock or Google Vertex AI models. They use different tokenizers.
Related Actions
| Action Name | Description |
|---|---|
| Chat | Sends messages to OpenAI and returns the response. |
| Summarize Content | Summarizes text, and handles text that's too long for the model. |
| Create Articles | Splits long content into topics and writes an article for each. |
Input Parameter Reference
| Parameter | Description | Supports Tokens | Default | Required |
|---|---|---|---|---|
| Model | The OpenAI model name, for example gpt-4 or gpt-3.5-turbo. It decides which tokenizer is used. See How tokens are counted. In expression mode you can enter any model name. | Yes | none selected | Yes |
| Content | The text to count, for example [Content]. | Yes | empty string | Yes |
Output Parameters Reference
| Parameter | Description |
|---|---|
| Store Token Count | Token that receives the number of AI model tokens in Content, for example TokenCount. If it's empty, the action does nothing. |
How tokens are counted
The action uses the same tokenizer as the model. Models use different encodings, so the same text can give different counts:
| Encoding | Models in the list |
|---|---|
cl100k_base | gpt-4, gpt-3.5-turbo, text-embedding-ada-002 |
p50k_base | text-davinci-003, text-davinci-002, the code-davinci and code-cushman models, davinci-codex, cushman-codex |
p50k_edit | text-davinci-edit-001, code-davinci-edit-001 |
r50k_base | The older models, such as text-davinci-001, davinci, curie, babbage, ada, and the text-similarity and text-search models |
If the tokenizer doesn't know the model name, cl100k_base is used. This is the case for newer models such as gpt-4o, which use a different encoding, so their count is an estimate.
Considerations
- Only the text is counted. Chat requests add a few tokens for each message, plus any instructions. Leave some room when you compare the count with a model's limit.
- Empty content fails. If
Contentis empty, the action fails with "Text and model name must not be null or empty." It doesn't return0. Add a condition such as"[Content]" != "". - The count is saved as text. Conditions compare it as a number, for example
[TokenCount] > 3000. - Most models in the list are retired by OpenAI. They're kept for counting. Use
gpt-4orgpt-3.5-turbofor current chat models, or enter the model name in expression mode.
Examples
To understand how to use the below examples, please see Running Examples.
1. Count the tokens in a document
This action counts the AI model tokens in the DocumentText token for gpt-4, and saves the number in the TokenCount token. A later action can use a condition such as [TokenCount] > 3000 to decide how to process the text.
{
"Title": "Count Tokens",
"ActionType": "OpenAi.CountTokens",
"Description": "Count the tokens in the document",
"Condition": "\"[DocumentText]\" != \"\"",
"Parameters": {
"ModelName": {
"Expression": "",
"Value": "gpt-4",
"IsExpression": false,
"Parameters": {}
},
"Content": "[DocumentText]",
"StoreTokenCount": "TokenCount"
}
}
Revised 09/27/2026