SimpleTokenCounter¶
This docs was updated at: 2026-02-23
com.paragon.agents.context.SimpleTokenCounter ยท Class
Implements TokenCounter
A simple token counter that uses character-based estimation.
This implementation provides approximate token counts using the following heuristics:
- Text:
text.length() / 4(approximately 4 characters per token for English) - Images: Fixed costs based on
ImageDetaillevel: HIGH: 765 tokensLOW: 85 tokensAUTO: 170 tokens (estimated average)
This counter is thread-safe and stateless.
Usage Example¶
TokenCounter counter = new SimpleTokenCounter();
// Count text tokens
int tokens = counter.countText("Hello, world!"); // ~3 tokens
// Count with custom chars-per-token ratio
TokenCounter customCounter = new SimpleTokenCounter(3); // 3 chars per token
See Also
TokenCounter
Since: 1.0
Fields¶
DEFAULT_CHARS_PER_TOKEN¶
Default characters per token ratio (approximately 4 for English).
HIGH_DETAIL_IMAGE_TOKENS¶
Token cost for high-detail images.
LOW_DETAIL_IMAGE_TOKENS¶
Token cost for low-detail images.
AUTO_DETAIL_IMAGE_TOKENS¶
Token cost for auto-detail images (estimated average).
Methods¶
SimpleTokenCounter¶
Creates a SimpleTokenCounter with default settings.
SimpleTokenCounter¶
Creates a SimpleTokenCounter with a custom characters-per-token ratio.
Parameters
| Name | Description |
|---|---|
charsPerToken |
the number of characters to consider as one token |
Throws
| Type | Condition |
|---|---|
IllegalArgumentException |
if charsPerToken is less than 1 |
countMessage¶
Counts tokens in a message, including all content items.
countContent¶
Counts tokens for a single content item.
countToolOutput¶
Counts tokens in a tool call output.
charsPerToken¶
Returns the characters-per-token ratio used by this counter.
Returns
the chars per token ratio