calculateTokenWarningState function

TokenWarningState calculateTokenWarningState(
  1. int tokenUsage,
  2. String model,
  3. int contextWindow, {
  4. bool autoCompactEnabled = true,
})

Calculate the token warning state given current usage.

Implementation

TokenWarningState calculateTokenWarningState(
  int tokenUsage,
  String model,
  int contextWindow, {
  bool autoCompactEnabled = true,
}) {
  final autoCompactThreshold = getAutoCompactThreshold(model, contextWindow);
  final effectiveWindow = getEffectiveContextWindowSize(model, contextWindow);
  final threshold = autoCompactEnabled ? autoCompactThreshold : effectiveWindow;

  final percentLeft = max(
    0,
    ((threshold - tokenUsage) / threshold * 100).round(),
  );

  final warningThreshold = threshold - warningThresholdBufferTokens;
  final errorThreshold = threshold - errorThresholdBufferTokens;

  final isAboveWarningThreshold = tokenUsage >= warningThreshold;
  final isAboveErrorThreshold = tokenUsage >= errorThreshold;
  final isAboveAutoCompactThreshold =
      autoCompactEnabled && tokenUsage >= autoCompactThreshold;

  final defaultBlockingLimit = effectiveWindow - manualCompactBufferTokens;
  final isAtBlockingLimit = tokenUsage >= defaultBlockingLimit;

  return TokenWarningState(
    percentLeft: percentLeft,
    isAboveWarningThreshold: isAboveWarningThreshold,
    isAboveErrorThreshold: isAboveErrorThreshold,
    isAboveAutoCompactThreshold: isAboveAutoCompactThreshold,
    isAtBlockingLimit: isAtBlockingLimit,
  );
}