[GH-ISSUE #289] [Feature]: Add a Token-Based Context Limit alongside the existing Message Limit #115

Closed
opened 2026-10-20 17:25:19 +07:00 by bsakaguchi · 3 comments
Owner

Originally created by @Navgner on GitHub (Apr 28, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/289

What problem does this solve?

Currently, Marinara Engine relies on a fixed "Message Limit" to manage how much context is sent to the model. However, since message lengths vary wildly during a roleplay, this approach is highly unpredictable. Sometimes a limit of X messages underutilizes my available context window, while other times it completely overflows it, causing my local LLM to bug out or truncate unexpectedly.

Describe the solution you'd like

I would love to see a "Context Limit in Tokens" option, ideally placed right below the Message Limit setting.
Instead of pulling a fixed number of messages, the engine would pull messages dynamically until it hits this user-defined token ceiling.

Why is this important? (Use Case)

If I am running a local model with a strict 16,000 token context window, I need precise control. I want to be able to set a token limit of, say, 14,500 tokens. This acts as a strict ceiling, leaving a guaranteed safe "buffer" of 1,500 tokens for the AI's generation. This ensures the engine maximizes the context without ever risking an overflow/crash on local hardware. Otherwise when the limit is hit, I notice the AI forgetting much more to do the thinking before generating a message.

Additional context

I am unsure if the lack of this feature is an intentional design choice due to the way context injection is coded, but having a token-based knapsack approach (filling up to a token threshold rather than a message count) would be a massive quality-of-life improvement for local users who need to manage VRAM strictly.

  • [] I DID NOT read this template and provide the requested details.
Originally created by @Navgner on GitHub (Apr 28, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/289 ## What problem does this solve? Currently, Marinara Engine relies on a fixed "Message Limit" to manage how much context is sent to the model. However, since message lengths vary wildly during a roleplay, this approach is highly unpredictable. Sometimes a limit of X messages underutilizes my available context window, while other times it completely overflows it, causing my local LLM to bug out or truncate unexpectedly. ## Describe the solution you'd like I would love to see a "Context Limit in Tokens" option, ideally placed right below the Message Limit setting. Instead of pulling a fixed number of messages, the engine would pull messages dynamically until it hits this user-defined token ceiling. ## Why is this important? (Use Case) If I am running a local model with a strict 16,000 token context window, I need precise control. I want to be able to set a token limit of, say, 14,500 tokens. This acts as a strict ceiling, leaving a guaranteed safe "buffer" of 1,500 tokens for the AI's generation. This ensures the engine maximizes the context without ever risking an overflow/crash on local hardware. Otherwise when the limit is hit, I notice the AI forgetting much more to do the thinking before generating a message. ## Additional context I am unsure if the lack of this feature is an intentional design choice due to the way context injection is coded, but having a token-based knapsack approach (filling up to a token threshold rather than a message count) would be a massive quality-of-life improvement for local users who need to manage VRAM strictly. - [] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-20 17:25:19 +07:00
Author
Owner

@kinthaiofficial commented on GitHub (Apr 28, 2026):

Token-based context limits are essential for any AI character engine. Message-count limits are a legacy approach that works poorly with variable-length messages.

What we learned implementing this at KinthAI:

1. Token counting must be model-specific
Different models tokenize differently. GPT-4 and Claude use different tokenizers. A "4K token limit" means different amounts of text depending on the model. Use the actual tokenizer for the active model, not a universal estimate.

2. Dynamic allocation within the token budget
Not all context is equal. We divide the budget:

  • System prompt: fixed allocation (e.g., 1K tokens)
  • Character card / World Info: fixed allocation (e.g., 2K tokens)
  • Memory / retrieved facts: capped allocation (e.g., 500 tokens)
  • Conversation history: remaining budget (everything left)

This way, conversation history flexes based on how much other context needs.

3. Smart truncation when over budget
When the total exceeds the context window:

  • Priority 1: Keep the system prompt and character card (identity)
  • Priority 2: Keep the most recent 2-3 messages (immediate context)
  • Priority 3: Keep retrieved memories (persistent knowledge)
  • Priority 4: Keep older messages (trim from the middle, not the end)

Trimming from the middle preserves both the character setup and the recent conversation flow.

4. The "overflow bug" you describe is common:
Local LLMs silently truncate when you exceed the context window. The fix: always leave a 10% buffer below the context limit. If the model supports 8K, target 7.2K max.

This is fundamentally the same problem as the sliding context window that causes Character.AI users to lose their conversation history. The fix is not a bigger window — it is smarter context management. We wrote about this: Why Character.AI Forgets You — Persistent Memory Architecture

<!-- gh-comment-id:4337009906 --> @kinthaiofficial commented on GitHub (Apr 28, 2026): Token-based context limits are essential for any AI character engine. Message-count limits are a legacy approach that works poorly with variable-length messages. **What we learned implementing this at KinthAI:** **1. Token counting must be model-specific** Different models tokenize differently. GPT-4 and Claude use different tokenizers. A "4K token limit" means different amounts of text depending on the model. Use the actual tokenizer for the active model, not a universal estimate. **2. Dynamic allocation within the token budget** Not all context is equal. We divide the budget: - System prompt: fixed allocation (e.g., 1K tokens) - Character card / World Info: fixed allocation (e.g., 2K tokens) - Memory / retrieved facts: capped allocation (e.g., 500 tokens) - Conversation history: remaining budget (everything left) This way, conversation history flexes based on how much other context needs. **3. Smart truncation when over budget** When the total exceeds the context window: - Priority 1: Keep the system prompt and character card (identity) - Priority 2: Keep the most recent 2-3 messages (immediate context) - Priority 3: Keep retrieved memories (persistent knowledge) - Priority 4: Keep older messages (trim from the middle, not the end) Trimming from the middle preserves both the character setup and the recent conversation flow. **4. The "overflow bug" you describe is common:** Local LLMs silently truncate when you exceed the context window. The fix: always leave a 10% buffer below the context limit. If the model supports 8K, target 7.2K max. This is fundamentally the same problem as the sliding context window that causes Character.AI users to lose their conversation history. The fix is not a bigger window — it is smarter context management. We wrote about this: [Why Character.AI Forgets You — Persistent Memory Architecture](https://blog.kinthai.ai/why-character-ai-forgets-you-persistent-memory-architecture)
Author
Owner

@SpicyMarinara commented on GitHub (Apr 28, 2026):

v1.5.6.

<!-- gh-comment-id:4337083974 --> @SpicyMarinara commented on GitHub (Apr 28, 2026): v1.5.6.
Author
Owner

@Navgner commented on GitHub (Apr 28, 2026):

I'm confused, I do not see the added feature in the 1.5.6 update? Is for a later version of 1.5.6?

<!-- gh-comment-id:4339518170 --> @Navgner commented on GitHub (Apr 28, 2026): I'm confused, I do not see the added feature in the 1.5.6 update? Is for a later version of 1.5.6?
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#115
No description provided.