deriveContextBudgetForPrompt static method

int deriveContextBudgetForPrompt({
  1. required int fullPromptBudget,
  2. required String query,
  3. String? systemInstruction,
  4. bool useStrictMode = true,
  5. PromptBudgetOptions options = const PromptBudgetOptions(),
})

Derive how many tokens can be safely assigned to context.text when the downstream consumer budgets the full prompt instead.

Implementation

static int deriveContextBudgetForPrompt({
  required int fullPromptBudget,
  required String query,
  String? systemInstruction,
  bool useStrictMode = true,
  PromptBudgetOptions options = const PromptBudgetOptions(),
}) {
  final promptOverheadTokens = switch (options.targetPromptTokenCounter) {
    final targetCounter? => targetCounter(
        _buildPromptText(
          instruction: _resolveSystemInstruction(
            systemInstruction: systemInstruction,
            useStrictMode: useStrictMode,
          ),
          contextText: '',
          query: query,
        ),
      ),
    null => options.fixedPromptOverheadTokens ?? 0,
  };

  final budget =
      fullPromptBudget - promptOverheadTokens - options.safetyMarginTokens;
  return budget < 0 ? 0 : budget;
}