Package com.tnsai.memory.strategy
Class TokenBudget
java.lang.Object
com.tnsai.memory.strategy.TokenBudget
Configuration for token budget management.
TokenBudget defines how the available context window should be allocated between different parts of the conversation.
Budget Allocation
Total Context Window (e.g., 8192 tokens) ├── System Prompt: ~500 tokens (reserved) ├── User Message: ~1000 tokens (estimated) ├── Tool Definitions: ~200 tokens (if any) ├── Response Reserve: ~2000 tokens (for LLM output) └── History Budget: ~4500 tokens (remaining)
Usage Example
TokenBudget budget = TokenBudget.builder()
.totalContextWindow(8192)
.systemPromptReserve(500)
.responseReserve(2000)
.build();
int available = budget.getHistoryBudget();
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic TokenBudget.Builderbuilder()static TokenBudgetforModel(int modelContextWindow) Creates a default budget for common models.intintReturns the token budget available for conversation history.intintintintintbooleanbooleantoString()static TokenBudgetwithHistoryLimit(int maxHistoryTokens) Creates a budget with a specific history limit.
-
Method Details
-
builder
-
getHistoryBudget
public int getHistoryBudget()Returns the token budget available for conversation history. This is calculated as: totalContextWindow - systemPromptReserve - currentMessageEstimate - toolDefinitionEstimate - responseReserve -
withHistoryLimit
Creates a budget with a specific history limit.- Parameters:
maxHistoryTokens- Maximum tokens for history- Returns:
- A new TokenBudget with the specified limit
-
forModel
Creates a default budget for common models.- Parameters:
modelContextWindow- The model's context window size- Returns:
- A balanced TokenBudget
-
getTotalContextWindow
public int getTotalContextWindow() -
getSystemPromptReserve
public int getSystemPromptReserve() -
getCurrentMessageEstimate
public int getCurrentMessageEstimate() -
getToolDefinitionEstimate
public int getToolDefinitionEstimate() -
getResponseReserve
public int getResponseReserve() -
isPreserveSystemPrompt
public boolean isPreserveSystemPrompt() -
isPreserveLastUserMessage
public boolean isPreserveLastUserMessage() -
getMinMessagesToKeep
public int getMinMessagesToKeep() -
toString
-