Skip to main content
Version: 1.28 (Current)

Count Tokens

Audience: Low-code Engineers

Skill Prerequisites: Tokens

Counts how many AI model tokens a text uses for an OpenAI model, and saves the number in a token.

info

"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.

note

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 Truncate and Split strategies 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 Response output of Summarize Content.
  • Count tokens for Amazon Bedrock or Google Vertex AI models. They use different tokenizers.
Action NameDescription
ChatSends messages to OpenAI and returns the response.
Summarize ContentSummarizes text, and handles text that's too long for the model.
Create ArticlesSplits long content into topics and writes an article for each.

Input Parameter Reference​

ParameterDescriptionSupports TokensDefaultRequired
ModelThe 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.Yesnone selectedYes
ContentThe text to count, for example [Content].Yesempty stringYes

Output Parameters Reference​

ParameterDescription
Store Token CountToken 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:

EncodingModels in the list
cl100k_basegpt-4, gpt-3.5-turbo, text-embedding-ada-002
p50k_basetext-davinci-003, text-davinci-002, the code-davinci and code-cushman models, davinci-codex, cushman-codex
p50k_edittext-davinci-edit-001, code-davinci-edit-001
r50k_baseThe 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 Content is empty, the action fails with "Text and model name must not be null or empty." It doesn't return 0. 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-4 or gpt-3.5-turbo for current chat models, or enter the model name in expression mode.

Examples​

tip

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