ContextHelper class

Helper class for managing model context and token limits.

Uses the 80% rule:
Only 80% of the total context window is used to leave a safety buffer and prevent truncation.

Example:

  • Context: 2048 tokens
  • Safe limit: 1638 tokens (80%)
  • Safety buffer: 410 tokens (20%)

Constructors

ContextHelper({required int contextSize, int maxMessagesToKeep = 15})
const

Properties

contextSize int
Total context window size.
final
hashCode int
The hash code for this object.
no setterinherited
maxMessagesToKeep int
Maximum number of messages to retain (including system prompt).
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
safeTokenLimit int
Returns the safe token limit (80% of total context).
no setter
safetyBuffer int
Returns the size of the safety buffer (20% of total context).
no setter

Methods

calculateSafeMaxTokens(int tokensUsed, int requestedMaxTokens) int
Calculates a safe number of tokens to generate based on available context.
estimateTokens(String text) int
Estimates the number of tokens in the given text.
getUsagePercentage(int tokensUsed) double
Returns the percentage of context used (0–100%).
isNearLimit(int tokensUsed) bool
Checks if context usage is approaching the safe limit (≥72% of total).
mustClear(int tokensUsed) bool
Checks if context must be cleared (≥80% of total).
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Constants

safeUsageLimit → const double
Maximum percentage of context allowed before trimming.