estimateRequestTokens function
Full next-request estimate — the ONE basis the ctx meter, the loop's over-window guard, and the compaction threshold all enforce.
estimateContextTokens over messages, plus
estimateRequestOverheadTokens when no provider-usage anchor prices
the system prompt and tool schemas in. An anchored estimate already
includes them (the reported usage is the whole previous request), so
adding the overhead there would double-count.
Implementation
int estimateRequestTokens(
List<Message> messages, {
String? systemPrompt,
List<Tool> tools = const [],
}) {
final estimate = estimateContextTokens(messages);
if (estimate.lastUsageIndex != null) return estimate.tokens;
return estimate.tokens + estimateRequestOverheadTokens(systemPrompt, tools);
}