Package com.tnsai.utils
Class TokenUtils
java.lang.Object
com.tnsai.utils.TokenUtils
Utility for estimating token counts.
Heuristic-based (1 token ≈ 4 chars). Not a substitute for a real tokeniser — use only for budgeting, context-window fit checks, and rough UI hints.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intConservative per-audio-part token cost.static final intConservative per-image token cost. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic intestimateHistoryTokens(List<Map<String, Object>> history) Estimates total tokens in a conversation history (rough estimate).static intestimateMessageTokens(Map<String, Object> msg) Estimates tokens for a single message map.static intestimateTokens(String text) Estimates the number of tokens in a text string.
-
Field Details
-
IMAGE_TOKEN_ESTIMATE
public static final int IMAGE_TOKEN_ESTIMATEConservative per-image token cost. Providers vary — OpenAI gpt-4o charges 85-1105 tokens depending on resolution; Anthropic bills ~1500 per image regardless of size. Use the Anthropic-style ceiling so budgeting never under-counts; callers can override by passing a custom estimator if they know their provider's precise formula.- See Also:
-
AUDIO_TOKEN_ESTIMATE
public static final int AUDIO_TOKEN_ESTIMATEConservative per-audio-part token cost. Used when a multi-modal part declarestype=audioortype=input_audio. Real tokenisation is provider-specific; this is a placeholder ceiling.- See Also:
-
-
Constructor Details
-
TokenUtils
public TokenUtils()
-
-
Method Details
-
estimateTokens
Estimates the number of tokens in a text string. Uses the heuristic: length / 4. -
estimateHistoryTokens
Estimates total tokens in a conversation history (rough estimate).Recognises structured message content:
- String content → plain text estimate.
List<Map>content parts → sum of per-part estimates (supportstext,image,input_image,audio,input_audio,tool_use,tool_result).
Unknown part types contribute a single-token baseline so they don't silently vanish from the budget.
-
estimateMessageTokens
Estimates tokens for a single message map. Exposed so callers who maintain their own history shape can reuse the per-message logic without building a wrapper list.- Parameters:
msg- message map with at minimum acontentkey- Returns:
- estimated token count
-