Skip to content

FALLBACK_CHARS_PER_TOKEN

const FALLBACK_CHARS_PER_TOKEN: 4 = 4

Defined in: providers/constants.ts:41

Character-to-token ratio for fallback token estimation.

Rationale: When native token counting APIs fail, we estimate tokens using a rough heuristic of 4 characters per token. This is based on empirical observations across multiple LLM providers:

  • OpenAI’s GPT models average ~4 chars/token for English text
  • Anthropic’s Claude models have similar characteristics
  • Gemini models also approximate this ratio

This is intentionally conservative to avoid underestimating token usage. While not perfectly accurate, it provides a reasonable fallback when precise tokenization is unavailable.

Reference: https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them