How it works

What goes into the prompt

The block is text and nothing else: prepend it, and the accounting is already done. What is in it, why, and what it cost — line by line.

On this page

The shape of the block

POST /v1/context returns the block to prepend to a system prompt, plus the entries that produced it and what each cost. The block is text and nothing else — prepend it, and the accounting is already done for you.

ts
const { text, entries, totalTokens, truncated } =  await memory.context.build({ userMessage, sessionId })// entries: one per line, with the reason it was included and its token cost// truncated: true when the budget bit, rather than a silently shorter prompt

Section headings inside the block are part of the wire format and are asserted in tests: ⚠️ Correction Detected In This Message, Weak Domains — Under 75% Reliability, Memory index — established earlier in this project, and Unresolved Contradictions. If you grep stored block logs for them, they are the strings to grep for.

Four reasons a line is there

Every line carries the rule that selected it. This is the whole reason the store is auditable: four reasons is a small enough set that a token bill can be attributed, and analytics is then an argument rather than a decoration.

ReasonIncludedWhy
indexgist lineThe default. What nearly every memory costs.
triggerfull bodyA concrete identifier absent from the transcript matched this memory.
tensionfull bodyAn unresolved contradiction, with the question to ask.
guardrailfull bodyA domain this agent has been unreliable in.

Index lines are cheap and full bodies are not, so a spend that is almost entirely index lines means the store has nothing worth promoting — not that the ceiling is too low. The two have opposite fixes, and the reason mix is what tells them apart.

The trigger

The trigger is the interesting one, and it is deterministic. Pull identifiers out of the message — URLs, dotted hosts, paths, SCREAMING_SNAKE, camelCase, long kebab-case, hex-ish codes — and keep only those absent from the visible transcript.

If the caller named something concrete the model cannot already see, and a memory mentions it, that memory’s body is included. No model is asked whether that matters.

The “absent from the transcript” half is what keeps this cheap. An identifier the model can already see needs no memory, so spending a body on it is pure waste — and in an agent loop the model has usually just been told it.

The fast gate

A synchronous fast gate runs first on the raw message and catches explicit corrections — “actually, we switched to Postgres”, “stop using that”, “that’s wrong”.

When it fires, a Correction Detected In This Message section goes into the block, because a user visibly changing their mind is the single most reliable signal that the agent’s assumption is stale. It is synchronous and local because it runs before anything else can be skipped, and it is pattern-based for the same reason capture is: a signal that can be missed is not a safety mechanism.

The budget

Everything above happens inside a token ceiling — index and bodies together. When the cap bites the response reports truncated: true and the least recently accessed entries lose their place. L0 is exempt: pinned content is injected in full regardless, because a budget that can silently drop a contradiction is a budget that can resolve one without anyone deciding to.

The ceiling is COGNITIVE_MEMORY_MAX_TOTAL_TOKENS, set per deployment and overridable per organisation. Self-hosting has the rest of the budget surface.

Inspecting it

The context preview builds the block for any message you paste and shows every line with its reason and cost. Activity replays the block that was actually sent, not the one the planner would produce today — re-running the planner against current memory answers “what would the agent get now”, which is a much better-looking answer than the one that caused whatever you are trying to understand.

The triggering message is not stored with the block — that is the conversation, not the memory — but the identifiers inside it are, because those are what explain why a full body was spent.