---
title: How a 280,000-token lorebook plays inside a 64,000-token context window
subtitle: "Which lorebook entries Yumina sends to the model each turn, how many tokens they may take, and which ones are dropped first, read from the engine's code."
description: How Yumina picks lorebook entries each turn in a world larger than the context window, with the defaults, the token budget, the drop order and an example.
author: Yumina
date: 2026-12-04
category: engineering
tags: lorebook, context window, world info, AI roleplay, SillyTavern, open source
lang: en
url: https://yumina.io/blog/how-a-world-larger-than-the-context-window-plays
canonical: https://yumina.io/blog/how-a-world-larger-than-the-context-window-plays
---

# How a 280,000-token lorebook plays inside a 64,000-token context window

*Which lorebook entries Yumina sends to the model each turn, how many tokens they may take, and which ones are dropped first, read from the engine's code.*

In October 2026 the largest world on Yumina held about 280,000 tokens of lore in 276 entries. A player on a 64,000-token window can play it, because on a normal turn the engine sends a few percent of that. By default it reads the last two messages, and an entry goes out only if one of its keywords is in them or its variable conditions hold.

The whole decision is one class, `LorebookMatcher`, about 360 lines of TypeScript with no model call in it. The overall prompt order and how chat history is trimmed are in [how Yumina builds a prompt](/blog/how-yumina-builds-a-prompt).

## What kinds of lorebook entry are there?

An entry is text plus a rule for when the model sees it. The matcher sorts every entry into one of these:

| Kind | Goes out when | Counts against the entry budget |
|---|---|---|
| Always sent | every turn | no |
| Keyword | a keyword appears in the scanned messages | yes |
| Conditional | its variable conditions hold | yes |
| Keyword and conditional | both are true | yes |
| Standby | a behavior has switched it on, then every turn | no |
| Bound to an interface slot | that slot is open in the world's custom UI | yes |

Three details in that sorting are easy to get wrong when you write a world.

Conditions beat the always-sent flag. An entry marked always sent that also carries a condition is treated as conditional, and stays out until the condition is true.

An entry with conditions is matched even when its own switch is off. The matcher skips a disabled entry only if no variable gates it.

An entry with no keywords, no conditions and no always-sent flag never goes out by itself. That is a standby entry, and it waits for a behavior to enable it.

Before any of this, modules are filtered. A world can group entries into modules that are on always, on while a condition holds, on from a chosen opening, switched by hand, or latched on by a keyword. Entries in a module that is off are removed before matching starts.

## What makes a keyword entry go out on a turn?

The server takes the last `lorebookScanDepth` messages of the current run, joins them into one string, and tests every keyword against it. The default depth is 2: the player's new message and the reply before it. The editor's stepper goes from 1 to 50, and the Studio assistant stops at 20. A comment in the code gives the reason for the lower ceiling: every turn rescans that many messages, so a large depth keeps old lore firing long after the story has moved on.

Matching is a case-insensitive substring test. Any one primary keyword is enough. A creator can change that per entry:

- **Whole words.** `matchWholeWords` stops "inn" from matching "inner". Chinese, Japanese, Korean and Thai have no spaces between words, so a keyword in those scripts matches wherever it appears.
- **Regex.** A keyword written as `/pattern/flags` is run as a regular expression.
- **Secondary keywords.** A second list with one of four rules: any of them present, all present, none present, or not all present.

Recursion is off by default. Set `lorebookRecursionDepth` from 1 to 10 and the matcher appends the text of each entry that fired to the scanned string, then runs again. An entry about a sect can then pull in the entry about its founder. Two per-entry flags control this: `preventRecursion` keeps an entry's text out of the next pass, and `excludeRecursion` means only the chat itself can trigger the entry.

## How many tokens can entries take?

Always-sent entries have no budget. A comment in the budget code says why: they are the world's spine, and trimming them breaks the character.

Triggered entries share one budget, computed per turn:

```
budget = lorebookBudgetPercent x window        (default 100 percent)
budget = min(budget, lorebookBudgetCap)        (default 0, meaning no cap)
budget = min(budget, window - reply reserve - story memory)
                                               (only if the host turns this on)
```

With the defaults, the budget is the whole window. On a 64,000-token window that is 64,000 tokens of triggered entries before anything is dropped.

The third line is a switch for whoever runs the server, off by default. Its comment records the problem it solves: the world is assembled first, so with a 64,000 window and a 55,000-token world, a player's 16,000 tokens of conversation became 9,000. With the switch on, a player who chose 16,000 tokens of story memory leaves 48,000 for triggered entries, and the entries give way before the conversation does. A creator's own cap still applies when it is the smaller number.

## Which entries are dropped first when too many match?

The matcher sorts matched entries by `position`, lowest first. Ties go to the entry with more primary keywords found, plus one point if it has secondary keywords. Then it fills from the top and counts tokens.

The fill stops at the first entry that does not fit. It does not skip that entry and try smaller ones below it, so everything after the stopping point is dropped as well. The one exception is the first entry in the list, which goes out even if it alone is larger than the budget.

So the entries with the highest position numbers are the ones a crowded turn loses. If an entry must survive, give it a low number.

## One turn, with numbers

Take a world the size of that largest one: 276 entries, 280,000 tokens. Say 14 entries are always sent and total 9,000 tokens. The entries below are invented to show the arithmetic.

The player writes that they are climbing to Azure Peak to ask Elder Wen about their rank. The matcher scans that message and the reply before it. Six of the 262 other entries match:

| Position | Entry | Tokens | Running total |
|---|---|---|---|
| 10 | Azure Peak sect | 1,900 | 1,900 |
| 20 | Elder Wen | 2,400 | 4,300 |
| 30 | Cultivation ranks | 3,100 | 7,400 |
| 40 | History of the sect | 1,500 | 8,900 |
| 50 | Passage token | 300 | 9,200 |
| 60 | Mountain weather | 1,200 | 10,400 |

With default settings the budget is 64,000, so all six go out. The model receives 9,000 + 10,400 = 19,400 tokens of lore, about 7 percent of the world, and 44,600 tokens of the window are left for everything else.

Now suppose the creator set `lorebookBudgetCap` to 8,000. The first three fit at 7,400. The fourth would make 8,900, so the fill stops there. The 300-token passage token is dropped too, although it would have fit. Lore sent: 9,000 + 7,400 = 16,400 tokens.

## What can a creator set?

Per entry: always sent or not, keywords, whole-word matching, secondary keywords and their rule, variable conditions joined by all or any, `position`, the two recursion flags, and the module it belongs to. A creator can also draw links between entries. "Depends" holds an entry back unless its target was sent. "Loads with" sends an entry whenever its target is sent, and those entries count against the budget. An entry carries at most 32 links.

Per world: scan depth (default 2), recursion depth (default 0), budget percent (default 100) and budget cap (default none). The two depths have steppers in the editor. The two budget settings live in the world file and can be set through the Studio assistant.

None of these numbers depends on a plan. The window is the only input that changes with how you play. On Yumina's free models the window starts at 64,000 tokens, with no message limit. With your own key, memory is unlimited, up to what the model can hold, and the same matcher runs with a larger number.

## FAQ

### Does Yumina use embeddings or vector search to pick lorebook entries?

No. Yumina's entry selection is deterministic string matching and variable checks in `LorebookMatcher`, with no model or embedding call. The same messages and the same game state always select the same entries.

### How many messages does Yumina scan for lorebook keywords?

Yumina scans the last 2 messages by default. The world's `lorebookScanDepth` setting changes it, from 1 to 50 in the editor.

### Can a lorebook entry trigger another entry on Yumina?

Yes, when the world sets `lorebookRecursionDepth` above its default of 0, up to 10 passes. Each pass adds the text of newly fired entries to the scanned text, unless an entry is marked `preventRecursion`.

## Sources

Read on 4 December 2026 in the open-source repository:

- [packages/engine/src/lorebook/lorebook-matcher.ts](https://github.com/lovetimo0421/yumina-oss/blob/main/packages/engine/src/lorebook/lorebook-matcher.ts): sorting, scanning, recursion, order and the budget fill
- [packages/engine/src/lorebook/keyword-matcher.ts](https://github.com/lovetimo0421/yumina-oss/blob/main/packages/engine/src/lorebook/keyword-matcher.ts): substring, whole-word and regex matching
- [packages/engine/src/lorebook/entry-triggers.ts](https://github.com/lovetimo0421/yumina-oss/blob/main/packages/engine/src/lorebook/entry-triggers.ts): conditional, standby and slot-bound entries
- [packages/engine/src/lorebook/worldbook.ts](https://github.com/lovetimo0421/yumina-oss/blob/main/packages/engine/src/lorebook/worldbook.ts) and [entry-links.ts](https://github.com/lovetimo0421/yumina-oss/blob/main/packages/engine/src/lorebook/entry-links.ts): modules and links
- [packages/shared/src/lorebook-budget.ts](https://github.com/lovetimo0421/yumina-oss/blob/main/packages/shared/src/lorebook-budget.ts): the budget formula
- [packages/engine/src/world/schema.ts](https://github.com/lovetimo0421/yumina-oss/blob/main/packages/engine/src/world/schema.ts): fields and defaults
- [packages/server/src/routes/messages.ts](https://github.com/lovetimo0421/yumina-oss/blob/main/packages/server/src/routes/messages.ts): where the server calls the matcher each turn

The 280,000-token figure is from [AI Entertainment Apps That Stay Closed Will Die](/blog/why-ai-roleplay-must-be-open-source).
