[GH-ISSUE #1533] [Feature]: Add tokenizer-backed estimates for lorebook and knowledge budgets #777

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

Originally created by @Xelvanis on GitHub (May 29, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/1533

Originally assigned to: @SpicyMarinara on GitHub.

What problem does this solve?

Lorebook prompt injection and agent knowledge retrieval still use rough character-count token estimates. That can mis-budget prompts for Unicode/multibyte text and boundary cases.

Proposed behavior

Add a tokenizer-backed estimation path when tokenizer support is available, with regression coverage for Unicode/multibyte text, boundary strings, and budget behavior across lorebook and knowledge retrieval paths.

Expected owner or impact area

  • App shell, navigation, startup, or providers
  • Catalog resources: chats, characters, personas, lorebooks, presets, connections, agents, gallery, or knowledge
  • Chat mode
  • Roleplay mode
  • Game mode
  • Shared mode UI
  • Runtime systems: generation, world-state, visuals, tracker, or haptics
  • Professor Mari or shell tools
  • React-free engine behavior, contracts, agents, or capability ports
  • Shared API wrapper or remote runtime routing
  • Tauri/Rust storage, assets, LLM, integrations, security, imports, or HTTP runtime
  • Docs, contributor workflow, or templates

Refactor branch considerations

Known heuristic sites from the local audit:

  • src/engine/generation-core/lorebooks/prompt-injector.ts estimates tokens from totalChars / 4.
  • src/engine/agents-runtime/knowledge/knowledge-retrieval.ts uses Math.ceil(text.length / 4).

Keep this in React-free engine code and avoid pulling UI/provider dependencies into the estimator.

Alternatives considered

Keep the current heuristic until tokenizer support exists, but keep the limitation publicly tracked so prompt-budget bugs are easier to triage.

Additional context

Low priority unless prompt-budget behavior starts causing visible truncation or retrieval-quality failures.

Template check

Please uncheck the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @Xelvanis on GitHub (May 29, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/1533 Originally assigned to: @SpicyMarinara on GitHub. ## What problem does this solve? Lorebook prompt injection and agent knowledge retrieval still use rough character-count token estimates. That can mis-budget prompts for Unicode/multibyte text and boundary cases. ## Proposed behavior Add a tokenizer-backed estimation path when tokenizer support is available, with regression coverage for Unicode/multibyte text, boundary strings, and budget behavior across lorebook and knowledge retrieval paths. ## Expected owner or impact area - [ ] App shell, navigation, startup, or providers - [ ] Catalog resources: chats, characters, personas, lorebooks, presets, connections, agents, gallery, or knowledge - [ ] Chat mode - [ ] Roleplay mode - [ ] Game mode - [ ] Shared mode UI - [ ] Runtime systems: generation, world-state, visuals, tracker, or haptics - [ ] Professor Mari or shell tools - [x] React-free engine behavior, contracts, agents, or capability ports - [ ] Shared API wrapper or remote runtime routing - [ ] Tauri/Rust storage, assets, LLM, integrations, security, imports, or HTTP runtime - [ ] Docs, contributor workflow, or templates ## Refactor branch considerations Known heuristic sites from the local audit: - `src/engine/generation-core/lorebooks/prompt-injector.ts` estimates tokens from `totalChars / 4`. - `src/engine/agents-runtime/knowledge/knowledge-retrieval.ts` uses `Math.ceil(text.length / 4)`. Keep this in React-free engine code and avoid pulling UI/provider dependencies into the estimator. ## Alternatives considered Keep the current heuristic until tokenizer support exists, but keep the limitation publicly tracked so prompt-budget bugs are easier to triage. ## Additional context Low priority unless prompt-budget behavior starts causing visible truncation or retrieval-quality failures. ## Template check Please uncheck the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
Author
Owner

@cha1latte commented on GitHub (May 29, 2026):

Triage note: labeling this as scope-risk because tokenizer-backed estimates affect lorebook activation and agent knowledge budget contracts across prompt assembly paths.

A good next step is to choose the tokenizer availability/fallback contract and test Unicode plus boundary cases before replacing rough estimates.

<!-- gh-comment-id:4578269481 --> @cha1latte commented on GitHub (May 29, 2026): Triage note: labeling this as `scope-risk` because tokenizer-backed estimates affect lorebook activation and agent knowledge budget contracts across prompt assembly paths. A good next step is to choose the tokenizer availability/fallback contract and test Unicode plus boundary cases before replacing rough estimates.
Author
Owner

@SpicyMarinara commented on GitHub (Jun 1, 2026):

We would need to include tokenizers with our files for that. I, in all honesty, have no clue where to find the updated ones for models such as new GPTs or Gemini. I don't think they're even released.

<!-- gh-comment-id:4591226400 --> @SpicyMarinara commented on GitHub (Jun 1, 2026): We would need to include tokenizers with our files for that. I, in all honesty, have no clue where to find the updated ones for models such as new GPTs or Gemini. I don't think they're even released.
Author
Owner

@SpicyMarinara commented on GitHub (Jun 1, 2026):

Closed as not planned for the refactor branch for now. Added docs/token-budget-estimates.md and linked it from the README to record the current heuristic-estimate behavior, why provider-exact tokenizer bundles are not safe to add without maintained model-family assets, and the requirements for a future tokenizer-backed implementation.

<!-- gh-comment-id:4592069926 --> @SpicyMarinara commented on GitHub (Jun 1, 2026): Closed as not planned for the refactor branch for now. Added docs/token-budget-estimates.md and linked it from the README to record the current heuristic-estimate behavior, why provider-exact tokenizer bundles are not safe to add without maintained model-family assets, and the requirements for a future tokenizer-backed implementation.
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#777
No description provided.