---
title: "How Yumina builds a prompt: what the AI reads each turn, and what gets cut first"
subtitle: The order of every part of an AI roleplay prompt on Yumina, how the token budget comes from the model and your settings, and what is dropped first when the prompt is full, read from the code.
description: What Yumina sends the AI model each turn, in order, how the token budget is computed, and what gets cut first when the prompt is full. With an example.
author: Yumina
date: 2026-12-04
category: engineering
tags: prompt, context window, AI memory, AI roleplay, lorebook, SillyTavern, open source
lang: en
url: https://yumina.io/blog/how-yumina-builds-a-prompt
canonical: https://yumina.io/blog/how-yumina-builds-a-prompt
---

# How Yumina builds a prompt: what the AI reads each turn, and what gets cut first

*The order of every part of an AI roleplay prompt on Yumina, how the token budget comes from the model and your settings, and what is dropped first when the prompt is full, read from the code.*

When a prompt is full, Yumina drops the oldest chat a third of your story memory at a time, never one message per turn. A start that moves every turn leaves nothing for the model provider's prompt cache to reuse. The world's always-send entries and the message you just typed are never cut.

All of it happens in one message route and one class, `PromptBuilder`. Three numbers decide a turn: the overall limit, story memory and the lorebook budget.

## What does the AI read each turn, and in what order?

| # | Part of the prompt | When the prompt is full |
|---|---|---|
| 1 | The world's always-send entries, your own prompts, active rule directives | Never cut |
| 2 | Your persona | Never cut |
| 3 | Format reference: how the model writes a variable change | Never cut |
| 4 | Example dialogue | Never cut |
| 5 | Keyword-triggered entries that sit in the system block | Lorebook budget |
| 6 | Recap of older scenes, then your pinned notes, then auto notes | Never cut; the recap has its own size |
| 7 | Chat history, oldest first, ending with the message you just sent. Lore with a depth and the author's pinned note are inserted here, counted back from the newest message | Story memory, oldest first |
| 8 | Context a behavior queued for this turn | Counted with the chat |
| 9 | Variable values, last turn's changes, what the card's screen is showing | Never cut |
| 10 | Post-history instructions | Never cut |

Parts 1 to 4 are the same on every turn, so the route marks the end of part 4 as a cache breakpoint. Keyword lore goes after it, so a different keyword firing does not throw away the cached prefix. A second breakpoint sits a few messages back from the end of the chat.

## How is the budget worked out?

**Overall limit** is the size of the whole request. The default is 200,000 tokens and the setting accepts 4,096 to 2,000,000. Your number replaces the card's, unless the author locked the card to its own. On Yumina's keys your plan can lower it. On your own key that step does not run.

Then the model clamps it: input may be at most (window − reply reserve) × 0.85. The reply reserve is your reply length plus thinking room, 12,000 + 4,096 = 16,096 tokens at the defaults. The 0.85 covers what the estimator misses. A comment records one Chinese-heavy prompt the estimator put near 200,000 that the provider counted as 219,958. A model whose window is unknown, such as a custom endpoint, is not clamped at all.

This is what "memory is unlimited, up to what the model can hold" means in code. A model with a 1,000,000-token window gets (1,000,000 − 16,096) × 0.85 = 836,318 tokens of input. Yumina is free and open source, and it runs on free models with no message limit or on your own key.

**Story memory** is how much chat goes out word for word. The dial runs from 2,048 to 200,000 and the suggested value is 16,000. An account that has never set it keeps the whole overall limit, unless the host has switched on a default for new accounts. On yumina.io, accounts made since 22 September 2026 start at the suggested 16,000. Lore does not count against it. The trimmer measures the lore injected into the chat this turn and adds that to the allowance. The room for what you and the AI said stays at the number you chose.

**Lorebook budget** is what matched keyword entries may spend: 100% of the overall limit by default, or a percent or fixed cap the creator sets. A host switch, off by default in the code and off on yumina.io, also sets story memory aside first. The comment explains why: with a 64,000 limit and a 55,000-token world, 16,000 of story memory "silently became 9,000".

## What gets cut first?

1. **Keyword entries over the lorebook budget.** Matched entries are sorted by position, then by match score, and filled from the top. The rest wait for another turn. [How a world larger than the context window plays](/blog/how-a-world-larger-than-the-context-window-plays) covers this step.
2. **The oldest chat.** The trimmer fills from the newest message back until it reaches story memory, or the overall limit minus the never-cut parts, whichever is smaller. The start then lands on a step boundary. A step is a third of story memory and at least 4,000 tokens, so the window holds between two thirds and all of your story memory.
3. **Nothing else.** Always-send entries skip the budget; the code calls them "the world's spine". If they fill the limit, the trimmer still keeps the newest message. An earlier version returned an empty history in that case and dropped the message the player had typed.

## When is the recap written, and what does it replace?

The recap is on by default at Standard. After a reply is saved, the server checks whether the chat is within 2,000 tokens of its budget. If it is, a background job folds everything older than the newest half of story memory into chapters: each passage of about 3,000 tokens becomes a chapter of up to 500. Folded messages stay in your saved chat and leave the prompt. The chapters go out every turn as part 6.

A send never waits for this. One automatic run makes at most 4 model calls, and if a turn overflows before the fold lands, that turn is trimmed and the recap catches up on the next.

The recap may grow to 15% (Lean), 25% (Standard) or 40% (Detailed) of story memory, with a floor of 1,500 tokens. Past that, the oldest chapters are merged first. The old single rolling summary was retired after a test on 14 long sessions. Asked 168 questions about the first 300 messages, the model got 8% right from the rolling summary and 51% from the chapters.

## One turn, with numbers

Overall limit 64,000, story memory 16,000, recap Standard, default reply length, a model with a 200,000-token window. The world has 22,000 tokens of always-send entries, and 9,000 tokens of keyword lore match this turn and are inserted into the chat.

| Step | Sum | Result |
|---|---|---|
| Model clamp | (200,000 − 16,096) × 0.85 = 156,318 | 64,000 stands |
| Recap ceiling | 25% of 16,000 | 4,000 |
| Never-cut parts | 22,000 world + 2,000 persona, format, examples + 4,000 recap + 1,000 variables and post-history | 29,000 |
| Lorebook budget | 100% of 64,000 | 64,000, so all 9,000 go out |
| Chat window | smaller of 64,000 − 29,000 and 16,000 + 9,000 | 25,000: the lore and 16,000 of chat |
| Request | 29,000 + 25,000 | 54,000 of 64,000 |

Once the chat passes about 14,000 tokens, the fold leaves the newest 8,000 word for word and turns the rest into chapters. With the recap off, the start of the chat jumps 5,333 tokens at a time instead.

Give the same settings a world with 45,000 tokens of always-send entries and the never-cut parts come to 52,000. That leaves 12,000 for the lore and the chat, and the chat gets about 3,000. Always-send entries stay whole, so the way out is a higher overall limit. The default of 200,000 already has the room.

## What can you see and change?

Memory, in the chat's + menu, shows which messages the AI reads word for word, which are in the recap, and which it no longer sees. Pinned notes hold up to 6,000 characters you wrote yourself. They go out every turn, and the prompt tells the model they win over the recap and the recent chat.

In AI settings you set the overall limit, story memory, the recap size or off, and which model writes the recap. A creator can cap the lorebook budget, limit the AI to the latest N messages (up to 500), and pin a note at a chosen depth. The [guide to AI settings](https://docs.yumina.io/guide/choosing-an-ai-model#story-memory) has the player's view of the same dials.

## FAQ

### Does a big lorebook use up chat memory on Yumina?

No. On Yumina, lore that a keyword brings into the chat is added on top of story memory, so it takes nothing from the chat. Always-send entries count toward the overall limit, and a world with a lot of them needs a higher one.

### Does Yumina delete old messages when the context is full?

No. Yumina keeps every message in the saved chat. Trimming and the recap only change which messages are sent to the model on a turn.

### Can a creator control how much chat history the AI reads on Yumina?

Yes. A Yumina world can set a history limit of the latest 1 to 500 messages, and a card can lock its own overall limit so the player's setting does not replace it. The model's window still applies after that.

## Sources

Read on 4 December 2026 in the open-source repository. The behaviour of the two host switches depends on how a server is configured.

- [packages/server/src/routes/messages.ts](https://github.com/lovetimo0421/yumina-oss/blob/main/packages/server/src/routes/messages.ts): the order of the prompt, the limits, the cache breakpoints
- [packages/engine/src/prompts/prompt-builder.ts](https://github.com/lovetimo0421/yumina-oss/blob/main/packages/engine/src/prompts/prompt-builder.ts): history trimming and the step
- [packages/server/src/lib/llm/context-window.ts](https://github.com/lovetimo0421/yumina-oss/blob/main/packages/server/src/lib/llm/context-window.ts): the model clamp and the 0.85
- [packages/shared/src/story-memory.ts](https://github.com/lovetimo0421/yumina-oss/blob/main/packages/shared/src/story-memory.ts), [lorebook-budget.ts](https://github.com/lovetimo0421/yumina-oss/blob/main/packages/shared/src/lorebook-budget.ts), [recap.ts](https://github.com/lovetimo0421/yumina-oss/blob/main/packages/shared/src/recap.ts): the three budgets and their defaults
- [packages/engine/src/lorebook/lorebook-matcher.ts](https://github.com/lovetimo0421/yumina-oss/blob/main/packages/engine/src/lorebook/lorebook-matcher.ts) and [history-window.ts](https://github.com/lovetimo0421/yumina-oss/blob/main/packages/engine/src/lorebook/history-window.ts): entry order and the creator's history limit
- [packages/server/src/lib/turn-memory.ts](https://github.com/lovetimo0421/yumina-oss/blob/main/packages/server/src/lib/turn-memory.ts), [extensions/session-memory/hooks.ts](https://github.com/lovetimo0421/yumina-oss/blob/main/packages/server/src/extensions/session-memory/hooks.ts) and [lib/summaryception.ts](https://github.com/lovetimo0421/yumina-oss/blob/main/packages/server/src/lib/summaryception.ts): pinned notes, when the recap folds, chapter sizes
