Package com.tnsai.context.assembler
Class TokenEstimator
java.lang.Object
com.tnsai.context.assembler.TokenEstimator
Content-aware token estimator that improves on simple length/4 heuristics.
Accounts for whitespace density, punctuation density, structured content (JSON/XML), and model-specific tokenizer differences to produce more accurate token count estimates without requiring an actual tokenizer library.
Estimation Strategy
- Classify content type (natural language, JSON, code, etc.)
- Apply content-type-specific chars-per-token ratio
- Adjust for whitespace and punctuation density
- Apply model-family multiplier
Usage Example
// Simple estimation
int tokens = TokenEstimator.estimate("Hello, world!");
// Model-aware estimation
int tokens = TokenEstimator.estimate("Hello, world!", "claude-3-opus");
// Estimate message list
int tokens = TokenEstimator.estimateMessages(history);
- Since:
- 0.3.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumContent type classification. -
Method Summary
Modifier and TypeMethodDescriptionstatic TokenEstimator.ContentTypedetectContentType(String text) Detects the content type of the text.static intEstimates token count for text using content-aware heuristics.static intEstimates token count with model-aware adjustment.static intestimateMessage(Map<String, Object> message, String modelName) Estimates tokens for a single chat message.static intestimateMessages(List<Map<String, Object>> messages) Estimates tokens for a list of chat messages.static intEstimates tokens for a list of chat messages with model awareness.
-
Method Details
-
estimate
Estimates token count for text using content-aware heuristics.- Parameters:
text- text to estimate- Returns:
- estimated token count, 0 for null/empty
-
estimate
Estimates token count with model-aware adjustment.- Parameters:
text- text to estimatemodelName- model identifier (e.g., "gpt-4", "claude-3-opus"), nullable- Returns:
- estimated token count
-
estimateMessages
Estimates tokens for a list of chat messages.Each message incurs a small overhead for role and delimiter tokens, in addition to the content token count.
- Parameters:
messages- list of message maps (must contain "content" key)- Returns:
- total estimated token count
-
estimateMessages
Estimates tokens for a list of chat messages with model awareness.- Parameters:
messages- list of message mapsmodelName- model identifier, nullable- Returns:
- total estimated token count
-
estimateMessage
Estimates tokens for a single chat message.- Parameters:
message- message map with "role" and "content" keysmodelName- model identifier, nullable- Returns:
- estimated token count including overhead
-
detectContentType
Detects the content type of the text.- Parameters:
text- text to classify- Returns:
- detected content type
-