CountLLMTokens Processor
Overviewβ
The CountLLMTokens
processor estimates the number of AI model tokens in an incoming request. This is particularly useful for tracking usage and cost of AI interactions in flows leveraging large language models (LLMs) such as OpenAI's GPT, Anthropic's Claude, or Google's Gemini.
This processor analyzes the request body, counts the tokens based on the configured model type and encoding, and stores the result in a custom request header for downstream processors or logging.
Input and Outputβ
- Input Stream:
Request
β Accepts the incoming request. - Output Stream:
Request
β Forwards the request downstream, optionally enriched with the token estimate.
Parametersβ
store_count_headerβ
Type: string
Required: False
Default: x-lunar-estimated-tokens
Sets the custom header in which to store the calculated token count.
Example:
- key: store_count_header
value: "x-custom-token-count"
model_typeβ
Type: string
Required: False
Specifies the type of AI model. Supported values include ChatGPT
, Claude
, Gemini
. This helps the processor choose the correct tokenization logic.
Example:
- key: model_type
value: "ChatGPT"
modelβ
Type: string
Required: False
Defines the specific model or wildcard pattern. Helps to distinguish among different versions (e.g., gpt-4
, gpt-4o-*
).
Example:
- key: model
value: "gpt-4o-*"
encodingβ
Type: string
Required: False
Defines the type of encoding used for tokenization. Useful when the tokenizer requires an explicit encoding type.
Example:
- key: encoding
value: "cl100k_base"
Best Practicesβ
The encoding
parameter can significantly impact token count accuracy. Use the correct tokenizer for your model (e.g., cl100k_base for GPT-4 or GPT-3.5). When in doubt, refer to the model providerβs tokenizer documentation.
CountLLMTokens Templateβ
CountTokens:
processor: CountLLMTokens
parameters:
- key: store_count_header
value: "x-lunar-estimated-tokens"
- key: model_type
value: "ChatGPT"
- key: model
value: "gpt-4o"
- key: encoding
value: "cl100k_base"
Use Caseβ
You can use the CountLLMTokens processor to:
- Track token usage across API requests to AI models
- Store token counts in logs for observability or cost reporting
This processor is used in flows that integrate with LLMs and need visibility into token-level usage for performance, pricing, or governance.