Local approximation

By default countTokens() uses a fast approximation — ~4 chars per token for English. Close enough for prompt-size checks.

public int countTokens(String text) {
  return Math.max(1, text.length() / 4);
}
Advertisement

Precise counting

When precision matters (e.g., a hard budget), call the provider's remote count endpoint. Slower but exact.

Advertisement

Provider adapters override

The Gemini adapter overrides countTokens to use SentencePiece bundled with the client library. Gets you exact counts without a network call.