Engineering · AI · Feature · Product

How Cereby Builds Each Prompt: A Four-Layer Context Model

5 min read

Why a strict context stack beats a grab bag of facts: a predictable shape, safe trimming, continuity across sessions.


Every time a student asks Cereby something, the server has to answer a quieter question first: what exactly do we send to the model?

Early on, the answer was "everything relevant": preferences, recent work, the current conversation, and whatever fields had been added since the last release. The prompt grew by appending, with no fixed order and no rule for what to cut when the token budget ran out. Its shape changed between releases, so quality was hard to test, and when the model gave a strange answer, nobody could say with confidence what it had been sent. That is not a model problem. It is a contract problem.

So we replaced the grab bag with the four-layer context model, an ordered stack whose order is also the trim policy. Its memory layer is the subject of Rebuilding Cereby's Memory So It Saves 9 in 10 Facts, Not 1, and Auditing Cereby's Context Manager: A Hello Instead of an Answer audited the same context system.

Nothing decided what to cut

When a long chat outgrew the budget, something had to go, and trimming was guesswork:

Order is the policy

Four layers, because four parts of the prompt vary from turn to turn. Above them sit the system instructions, the same static rules on every turn; below them, the student's current message. Order encodes priority. The anchors near the top are the hardest to displace, and the long tail near the bottom is the safest to trim first:

When a conversation runs long, the cut is now mechanical: the oldest turns go first, then older summaries, while the system instructions, session metadata and top memory hits stay untouched. Order makes the trade-off explicit, and the rollback explainable. Here is one synthetic turn's context, top to bottom:

system instructions  the same static rules on every turn
1  session metadata  built fresh from this request, never stored as memory
                     device, timezone, local time, location when the app sends it,
                     account tier, coin balance
2  user memory       memories retrieved by embedding against this message,
                     capped by a token budget
3  recent summaries  short, student-focused lines about the last several chats
4  this chat         the turns so far
current message      "what time does the library close tomorrow?"   (synthetic)

Session metadata lets the model answer that without guessing at the date. Memory sends what is relevant to the ask, not everything we have learned. And the summaries, written when a session starts or continues rather than shipped as raw transcripts, carry continuity across chats at a fraction of the tokens of full history.

Memory off, dates still right

The memory-off and temporary-chat paths needed care. A student who turns memory off expects no durable recall, but still expects "what time is it tomorrow in Tokyo?" to work. Session metadata had to stay strictly outside memory, or turning on privacy would silently break date and locale grounding:

What this taught us

Ephemeral facts about the request belong in their own layer, not in the memory store. Mixed in, they break privacy modes in a way nobody sees until a student turns memory off.

Putting trim priority into layer order turns an editorial judgment into a mechanical rule. That matters most at 2 a.m., when something is behaving strangely and whoever is on call needs to reason about what the model actually saw.

All engineering posts