estimateRequestOverheadTokens function

int estimateRequestOverheadTokens(
  1. String? systemPrompt,
  2. List<Tool> tools
)

Estimated token cost of the request parts that are NOT transcript messages: the system prompt and the tool schemas (name + description + JSON-encoded parameters), at pi's 4-chars-per-token heuristic.

Every wire request carries these, but estimateContextTokens never counts them — they only enter an ANCHORED estimate implicitly, through the provider-reported usage. An unanchored estimate (fresh or resumed session, provider without usage reporting) silently drops them: a 30 KB system prompt plus ~30 tool schemas is tens of thousands of tokens the ctx meter and the over-window guard then ignore.

Implementation

int estimateRequestOverheadTokens(String? systemPrompt, List<Tool> tools) {
  var chars = systemPrompt?.length ?? 0;
  for (final tool in tools) {
    chars +=
        tool.name.length +
        tool.description.length +
        _safeJsonEncode(tool.parameters).length;
  }
  return (chars / _charsPerToken).ceil();
}