PrismContextBudget class

Keeps a conversation inside a model's context window.

Every chat app eventually hits the same bug: a long thread is sent whole, the provider rejects it or silently truncates the middle, and the assistant starts forgetting the system prompt. This class estimates the cost, trims the oldest turns first, and always keeps the system message and the most recent exchange.

final trimmed = PrismContextBudget(
  maxTokens: 8000,
  reserveForReply: 1000,
).fit(history);

The estimate is a heuristic, not a tokenizer: roughly four characters per token for English prose. It is deliberately pessimistic (it rounds up), so it under-fills rather than over-fills. Swap in a real tokenizer by overriding estimateTokens if your provider exposes one.

Constructors

PrismContextBudget({required int maxTokens, int reserveForReply = 1024, bool keepSystemMessages = true, int minimumRecentMessages = 2})
Creates a context budget.
const

Properties

availableTokens → int
Tokens actually available for the conversation.
no setter
hashCode → int
The hash code for this object.
no setterinherited
keepSystemMessages → bool
Whether system messages are always kept, even when trimming aggressively. Losing the system prompt changes the model's behaviour entirely, which is worse than losing an old turn.
final
maxTokens → int
The model's context window, in tokens.
final
minimumRecentMessages → int
The most recent messages that are never dropped.
final
reserveForReply → int
Tokens to leave free for the model's reply. A budget that consumes the whole window leaves no room to answer.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

estimateConversationTokens(Iterable<AiChatMessage> messages) → int
Estimates the total for a conversation.
estimateMessageTokens(AiChatMessage message) → int
Estimates the token count of one message.
estimateTokens(String text) → int
Estimates the token count of text.
fit(List<AiChatMessage> messages) → List<AiChatMessage>
Trims messages to fit, dropping the oldest turns first.
fits(Iterable<AiChatMessage> messages) → bool
Whether messages fit inside availableTokens.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited
usageRatio(Iterable<AiChatMessage> messages) → double
How much of the budget the conversation is using, in 0..1.

Operators

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