Slopfluencer

Notes › AI › Agents › Memory and context

Neural memory and below-the-prompt context

Most agent memory works at the text layer. Files, databases and search tools select passages of prose and paste them into the prompt before the model runs. Every time that text enters a new session or gets evicted from memory, the model has to read and process all of those tokens again from scratch, and as conversations grow long the KV cache that stores attention state crowds out other work.

A separate line of research moves memory below the prompt, storing past context or learned facts directly inside the model's internal states or dedicated memory parameters. Three main approaches have emerged: saving and reusing KV caches across sessions, adding neural memory layers that update or look up internal representations, and compressing prompts into continuous vectors known as soft tokens.

Saving and reusing KV caches

When a transformer processes a prompt during prefill, it computes key and value vectors for every token in every layer and stores them in the KV cache. Standard prompt caching keeps those states in memory while a single prefix stays hot, but drops them once the prefix changes or the session ends.

Prompt Cache, from November 2023, treats reusable text segments such as system messages, templates and reference documents as named modules whose attention states are precomputed once and stored on the server. A schema tracks token positions so cached modules can be stitched into new prompts without changing the model's weights. In prototype tests on long-prompt tasks such as document question answering, modular reuse cut time-to-first-token by 8 times on GPUs and up to 60 times on CPUs without hurting accuracy.[4]

On local hardware running several agents at once, RAM fills up before the prompt cache can help. A February 2026 paper by Yakov Pyotr Shkolnikov noted that an Apple M4 Pro with a 10.2 GB cache budget fits only three agents at an 8,000-token context in 16-bit precision; a ten-agent workflow has to evict caches constantly, paying 15.7 seconds of re-prefill per agent at 4,000 tokens. Shkolnikov's system saves each agent's KV cache to disk in 4-bit quantized form and restores it directly into the attention layers. Tested on three open-weight models (Google's Gemma 3 12B, DeepSeek-Coder-V2-Lite 16B and Meta's Llama 3.1 8B), cache restoration cut time-to-first-token by 11 to 136 times at 4,000 to 32,000 tokens and fit four times as many agents in memory, with perplexity (a measure of prediction error where lower is better) shifting between minus 0.7 percent on Gemma and plus 3.0 percent on DeepSeek.[7]

Neural memory layers and fast weights

Memorizing Transformers, from Google Research in 2022, built memory into the architecture itself by adding an approximate nearest-neighbor lookup over an external store of past key-value pairs that the model queries without updating through gradient descent. Expanding that store up to 262,000 tokens steadily lowered language-modeling loss across web text, books, math papers, code and formal proofs, and let the model call functions and theorems defined earlier at test time without updating weights.[2]

Memory Layers at Scale, from Meta in December 2024, took a parametric route. A trainable key-value lookup layer adds sparse parameters that activate only for matching queries, raising capacity without increasing floating-point operations per token. Pretrained to 1 trillion tokens with up to 128 billion memory parameters, models with memory layers outperformed dense models given more than twice the compute budget and matched-compute mixture-of-experts baselines, with the largest gains on factual recall.[5]

Two 2026 papers combine sparse memory with test-time training, updating memory parameters during inference as new text arrives. Fast-weight Product Key Memory (FwPKM), by Tianyu Zhao and Llion Jones in January 2026, runs chunk-level gradient updates on sparsely activated memory slots during both training and inference. Trained only on 4,000-token sequences, FwPKM generalized to 128,000-token needle-in-a-haystack retrieval while keeping per-token compute fixed.[6] Titan-Llama, by Sarah Pan and Ali Behrouz in October 2026, showed that an existing pretrained transformer can be retrofitted without pretraining from scratch. It keeps full attention over recent tokens and compresses older history into fixed-size neural memory modules updated at test time. Retrofitted onto Llama-3.2-1B-Instruct, Meta's 1-billion-parameter instruction-tuned model, it beat a baseline that splits long documents into separate attention chunks, raising accuracy from 23.87 to 40.51 percent on QuALITY (long-document reading comprehension) and from 14.50 to 40.75 percent on LongHealth (long clinical-record question answering). It also ran over 20 percent faster in prefill and 37 percent faster in decode with 24 percent lower decode memory.[8]

Soft tokens and continuous prompts

Prefix-Tuning, by Xiang Lisa Li and Percy Liang in 2021, replaced discrete prompt text with continuous vectors that the model attends to directly. It froze the pretrained model and learned a small sequence of task-specific vectors prepended to each layer. Updating 0.1 percent of the parameter count matched full fine-tuning on table-to-text generation and summarization in full-data tests, beat fine-tuning when data was scarce, and extrapolated better to unseen topics.[1]

A 2023 theoretical analysis by Aleksandar Petrov and colleagues pinned down what continuous prompts can and cannot do. Even though continuous vectors live in a richer space than discrete tokens, prompting, soft prompting and prefix-tuning cannot alter the model's relative attention pattern over the input content; they can only bias an attention layer's output in a fixed direction. They work well for eliciting abilities the pretrained model already has, but they cannot teach tasks that require genuinely new attention patterns.[3]

Long-horizon agents have revived the idea for compressing past interaction history inside the model rather than asking a separate summarizer to rewrite text. Agentic Soft-Token Compression (ASTC), from October 2026, works on hybrid models that mix attention with recurrent layers, using the recurrent state to detect which earlier details have already been absorbed and carrying them forward as soft tokens while keeping exact values as ordinary tokens. On long-horizon agent benchmarks, the training-free method matched or beat uncompressed agents while cutting token-to-token attention computations by up to 59.84 percent and preserving reusable KV caches across turns.[9]

History and timeline

  • Jan 2021: Li and Liang introduce Prefix-Tuning, adapting frozen models with continuous virtual tokens.[1]
  • Mar 2022: Memorizing Transformers add approximate nearest-neighbor lookup over up to 262,000 cached key-value pairs.[2]
  • Oct 2023: Petrov et al. prove that prompting and prefix-tuning cannot change relative attention patterns over content.[3]
  • Nov 2023: Prompt Cache reuses precomputed attention states across modular prompt segments.[4]
  • Dec 2024: Memory Layers at Scale trains sparse key-value memory layers up to 128 billion parameters.[5]
  • Jan 2026: Fast-weight Product Key Memory updates sparse memory slots at test time and generalizes from 4,000 to 128,000 tokens.[6]
  • Feb 2026: Agent Memory Below the Prompt persists 4-bit quantized KV caches to disk for local multi-agent inference.[7]
  • Oct 2026: Titan-Llama retrofits neural memory modules onto a pretrained transformer;[8] ASTC compresses long-horizon agent context into soft tokens without extra training.[9]

My position

I have argued that text-level memory systems usually lose information because they compress history before anyone knows what a future task will need (Plain files and search beat memory systems), and that ideally an agent could keep everything in context without manual management.[10] Below-the-prompt memory attacks the cost of keeping that context directly, either by saving the KV cache instead of re-reading text or by letting the model compress history in its own internal representations.

None of these mechanisms are part of my daily setup yet. Persistent KV caches and neural memory layers require control over the inference engine or the model weights, which rules out hosted frontier APIs. For local open-weight models running several agents on one machine, quantized KV-cache persistence and retrofit memory modules look like the most practical bridge between plain-file history and native long-term memory.

Criticism and counterpoints

Every approach here trades away the main advantage of plain-text memory, which is inspection. A Markdown file or a SQL event log can be read, edited and moved from one model to another. A saved KV cache or soft-token sequence is tied to one exact model checkpoint and tokenizer, so upgrading the model invalidates every stored state.

Theoretical and practical limits also narrow each branch. Continuous prompts cannot learn new attention patterns,[3] quantized KV caches introduce small perplexity penalties that vary by architecture,[7] and test-time memory layers like FwPKM and Titan-Llama have so far been evaluated mainly on smaller models or synthetic retrieval tasks rather than frontier coding workloads.[6][8]

See also

Change log

  • 2026-12-06: First written, from papers on persistent KV caches, neural memory layers, fast weights, prefix-tuning and soft-token context compression.

Reading

References

  1. ^ Li and Liang, Prefix-Tuning: Optimizing Continuous Prompts for Generation
  2. ^ Wu et al., Memorizing Transformers
  3. ^ Petrov et al., When Do Prompting and Prefix-Tuning Work? A Theory of Capabilities and Limitations
  4. ^ Gim et al., Prompt Cache: Modular Attention Reuse for Low-Latency Inference
  5. ^ Berges et al., Memory Layers at Scale
  6. ^ Zhao and Jones, Fast-weight Product Key Memory
  7. ^ Shkolnikov, Agent Memory Below the Prompt: Persistent Q4 KV Cache for Multi-Agent LLM Inference on Edge Devices
  8. ^ Pan and Behrouz, Titan-Llama: Retrofitting Pre-trained LLMs with External Neural Memory Modules
  9. ^ He et al., In-Model Context Compression with Agentic Soft Tokens for Long-Horizon LLM Agents
  10. ^ Ideally everything goes into the agent's context (@trashpandaemoji, Jun 2026), since deleted
    At the end of the day its just context management and a way to chain operations to reduce the amount of context and agent turns. IMO the optimal case is if we're able to put everything into the agent's context if intelligence and context length wasn't an issue. Every "dumb" operation we try to do or have the agent do to manage its own context reduces the amount of information it has to work with. I think in a decade you'll be able to just dump everything into the model and context management will be a thing of the past.