TokenCounter class
High-level token counting, truncation, splitting, and cost estimation.
Constructors
- TokenCounter({TokenEncoder? encoder})
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
countMessageTokens(
List< Map< messages) → intString, String> > -
Counts total tokens for a list of chat
messages. -
countTokens(
String text, {String? model}) → int -
Counts the tokens in
textusing the configured encoder. -
countToolUseTokens(
String tool, String input) → int - Estimates tokens for a tool-use invocation.
-
estimateCost(
int inputTokens, int outputTokens, {String model = 'claude-sonnet-4-20250514', int cacheRead = 0, int cacheWrite = 0}) → CostEstimate - Estimates the cost of an API call given input and output token counts.
-
estimateImageTokens(
int width, int height, {String detail = 'auto'}) → int - Estimates the token cost of an image based on its dimensions and detail level.
-
estimatePdfTokens(
int pageCount) → int -
Estimates tokens for a PDF with the given
pageCount. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
splitByTokens(
String text, int chunkSize, {int overlap = 0}) → List< String> -
Splits
textinto chunks of approximatelychunkSizetokens. -
toString(
) → String -
A string representation of this object.
inherited
-
truncateToTokens(
String text, int maxTokens) → String -
Truncates
textto at mostmaxTokens, returning the truncated string.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited