Titans Architecture: Neural Long-Term Memory for LLMs

The Problem: The Fundamental Memory Dilemma of Modern Sequence Models
In modern enterprise AI systems, autonomous agents, and long-range extraction platforms like AiFetchly, foundation models are increasingly deployed on massive context lengths: ingesting multi-year financial statements, comprehensive legal contracts, deeply nested code repositories, and continuous multi-agent conversation histories.
However, existing sequence modeling paradigms force an unsustainable compromise between representational precision and computational scalability.
1. The Quadratic Wall of Full Attention Transformers
Standard multi-head attention excels at precise associative retrieval. Because it explicitly compares every query token against every historical key token via the softmax attention matrix:
$$\text{Attention}(Q, K, V) = \text{softmax}\left(\frac{Q K^T}{\sqrt{d_k}}\right) V$$
Full attention exhibits exceptional associative recall (scoring 100% on standard Needle-In-A-Haystack benchmarks within its window). However, this precision comes at immense hardware cost:
- Quadratic Time Complexity ($O(N^2)$): Processing sequences beyond 100,000 tokens during prefill requires astronomical FLOP budgets.
- Linear KV Cache Growth ($O(N)$): For a 70B parameter model at 128k context, storing the uncompressed Key-Value cache consumes over 40 GB of VRAM per user request, exhausting GPU High-Bandwidth Memory (HBM) and collapsing server throughput.
2. The Information Bottleneck of Linear Recurrent Models
To achieve linear computational complexity ($O(N)$) and constant inference memory ($O(1)$), researchers developed State Space Models (SSMs) and modern linear RNNs such as Mamba-2, RWKV-v6, and Griffin.
These architectures compress all historical context into a fixed-size hidden state vector $h_t \in \mathbb{R}^d$:
$$h_t = A_t h_{t-1} + B_t x_t, \quad y_t = C_t h_t$$
While linear recurrent models process millions of tokens efficiently, their fixed state size acts as an information bottleneck. When tested on complex multi-hop retrieval or dense associative recall tasks (such as BABILong or variable-tracking code reasoning), linear models suffer from catastrophic forgetting:
$$\text{Capacity}(h_t) \ll \text{Information Content of Sequence } X_{1:t}$$
Linear models cannot expand their state to accommodate surprising, rare, or complex factual associations encountered deep in long documents.
The Architecture: Titans and Test-Time Neural Memorization
To break this false dichotomy between quadratic transformers and lossy linear recurrent models, researchers at Google Research (Behrouz, Zhong, and Mirrokni) introduced Titans: Learning to Memorize at Test Time (NeurIPS 2025 / arXiv 2501.00663).
Titans draws direct inspiration from cognitive neuroscience, decoupling human-like working memory from long-term memory through a three-tier modular architecture.

1. The Three-Tier Memory System
The core innovation of Titans is replacing static recurrent vectors with a dynamic, test-time learnable neural network for long-term memory:
- Persistent Memory (Static Constants): Task-independent learnable parameters that remain fixed after pre-training. These represent the model’s fundamental priors, linguistic grammar, and common knowledge.
- Short-Term Memory (Sliding-Window Attention): A localized multi-head attention module operating over a bounded context window (e.g., $w = 2,048$ tokens). It handles short-range syntax, local entity relations, and fine-grained token interactions with full attention fidelity.
- Long-Term Neural Memory ($M_t$): A deep non-linear multi-layer perceptron (MLP) whose weights $W_M$ are updated dynamically during inference (test time) as the model ingests tokens.
2. The Surprise-Based Memorization Objective
How does the neural memory module decide what information to store, overwrite, or forget as it reads a sequence?
Titans models human memorization through a surprise metric. When you read a document, you do not consciously memorize predictable words like “the” or “and”; you memorize surprising, unexpected facts.
At each step $t$, the long-term memory receives a key vector $k_t$ and attempts to predict its associated value vector $v_t$ using its current internal weights $M_{t-1}$:
$$\hat{v}t = M{t-1}(k_t)$$
The model evaluates an associative reconstruction loss $\ell_t$:
$$\ell_t = \frac{1}{2} | M_{t-1}(k_t) - v_t |_2^2$$
The gradient of this reconstruction loss with respect to the memory weights measures the instantaneous surprise ($S_t$):
$$S_t = \nabla_{M} \ell_t = (M_{t-1}(k_t) - v_t) \cdot \nabla_M M_{t-1}(k_t)$$
- Low Surprise ($S_t \approx 0$): The current memory already predicts the association. Minimal or zero weight updates occur.
- High Surprise ($S_t \gg 0$): The token sequence contains unexpected or critical information. The memory module triggers an aggressive gradient update.
3. Adaptive Forgetting and Momentum Update Rule
To prevent catastrophic interference and ensure stability, Titans updates its memory weights using a momentum buffer and an adaptive forgetting gate:
$$M_t = (1 - \eta_t) M_{t-1} + \alpha_t S_t$$
Where:
- $\alpha_t \in (0, 1)$ is the adaptive learning rate (data-dependent step size).
- $\eta_t \in (0, 1)$ is the adaptive forgetting gate (governing weight decay to discard obsolete historical context).
- $S_t$ incorporates past momentum to smooth out high-frequency gradient noise.
Because the memory module is a multi-layer neural network with non-linear activations (rather than a simple 2D outer-product matrix like in Fast Weight Programmers or linear TTT), it has orders of magnitude greater storage capacity and can resolve non-linear associative conflicts without overwriting prior facts.
4. The Three Architectural Integration Topologies
Titans introduces three distinct design patterns to combine short-term sliding-window attention with long-term neural memory:
- Memory as Context (MAC): The neural long-term memory acts as a dynamic retrieval engine. For the current query segment, memory outputs are decoded into historical context tokens and prepended directly to the key-value sequence of the local attention window. Attention attends over both retrieved past context and current tokens.
- Memory as a Gate (MAG): Short-term attention and long-term neural memory execute in parallel branches. A non-linear learned gating mechanism balances the contributions: $$y_t = (1 - g_t) \odot y_t^{\text{Attention}} + g_t \odot y_t^{\text{Memory}}$$
- Memory as a Layer (MAL): Attention and neural memory are stacked sequentially. Hidden representations pass first through local attention to extract short-term context, followed by the neural memory layer to enrich representations with global historical context.
In empirical benchmarks, MAC and MAG consistently achieve the strongest performance, maintaining near-perfect retrieval across sequences exceeding 2 Million tokens.
Architectural Comparison: Long-Context Architectures
| Dimension / Metric | Full Transformer | Mamba-2 (SSM) | Test-Time Training (TTT) | Titans (Google Research) |
|---|---|---|---|---|
| Compute Complexity | $O(N^2)$ Quadratic | $O(N)$ Linear | $O(N)$ Linear | $O(N)$ Linear |
| KV Cache Footprint | $O(N)$ Linear unbounded | $O(1)$ Fixed vector | $O(1)$ Linear layer weight | $O(1)$ Bounded sliding window + MLP |
| Long-Term Memory Type | Ephemeral activation cache | Fixed 1D hidden state | 1-Layer Linear matrix | Deep Non-Linear Neural Network |
| Memorization Trigger | Passive storage | Fixed linear decay | Linear gradient descent | Surprise Metric ($\nabla \ell$) + Gated Decay |
| Context Limit Tested | ~128K tokens (VRAM wall) | Hundreds of thousands | 1M tokens | 2M+ Tokens with zero recall drop |
| Multi-Hop Recall (BABILong) | High (Short) / OOM (Long) | Fails on complex needles | Moderate | State-of-the-Art across 2M tokens |
Implementation: Building a Surprise-Driven Neural Memory Engine
The following standalone Python implementation demonstrates the exact mathematical mechanics of Titans’ neural long-term memory module, associative surprise calculation, gradient-based test-time learning, and retrieval validation.
Step 1: Neural Long-Term Memory Module Implementation
import numpy as np
class TitansNeuralMemory:
"""
Reference implementation of the Titans Neural Long-Term Memory Module.
Uses a 2-layer MLP updated at test time via surprise-driven gradient steps
with momentum and adaptive forgetting.
"""
def __init__(self, d_model: int = 64, d_hidden: int = 128, base_lr: float = 0.05, momentum: float = 0.9):
self.d_model = d_model
self.d_hidden = d_hidden
self.base_lr = base_lr
self.momentum = momentum
# Neural memory parameters: 2-layer MLP (W1, b1, W2, b2)
# Weights act as long-term storage medium
self.W1 = np.random.randn(d_hidden, d_model) * np.sqrt(2.0 / d_model)
self.b1 = np.zeros((d_hidden, 1))
self.W2 = np.random.randn(d_model, d_hidden) * np.sqrt(2.0 / d_hidden)
self.b2 = np.zeros((d_model, 1))
# Momentum state buffers
self.v_W1 = np.zeros_like(self.W1)
self.v_W2 = np.zeros_like(self.W2)
def retrieve(self, query_key: np.ndarray) -> np.ndarray:
"""
Retrieves associated value from long-term neural memory given a key.
query_key shape: (d_model, 1)
"""
h = np.maximum(0, self.W1 @ query_key + self.b1) # Non-linear ReLU activation
val_pred = self.W2 @ h + self.b2
return val_pred
def memorize_step(self, key: np.ndarray, value: np.ndarray, alpha_t: float = 1.0, eta_t: float = 0.005) -> float:
"""
Executes a test-time training step on the current token representation.
Computes surprise metric (reconstruction loss gradient) and updates weights.
"""
# 1. Forward pass through neural memory
h_pre = self.W1 @ key + self.b1
h = np.maximum(0, h_pre)
val_pred = self.W2 @ h + self.b2
# 2. Compute surprise metric: L_associative = 0.5 * || val_pred - value ||^2
error = val_pred - value
surprise_loss = float(0.5 * np.sum(error ** 2))
# 3. Analytical gradients of surprise with respect to memory weights
grad_W2 = error @ h.T
grad_b2 = error
dh = self.W2.T @ error
dh[h_pre <= 0] = 0 # Backprop through ReLU
grad_W1 = dh @ key.T
grad_b1 = dh
# 4. Momentum update rule with adaptive learning rate alpha_t
eff_lr = self.base_lr * alpha_t
self.v_W2 = self.momentum * self.v_W2 + eff_lr * grad_W2
self.v_W1 = self.momentum * self.v_W1 + eff_lr * grad_W1
# 5. Parameter update with adaptive forgetting (1 - eta_t weight decay)
self.W2 = (1.0 - eta_t) * self.W2 - self.v_W2
self.W1 = (1.0 - eta_t) * self.W1 - self.v_W1
self.b2 -= eff_lr * grad_b2
self.b1 -= eff_lr * grad_b1
return surprise_loss Step 2: Verification, Needle Retrieval, and Distractor Resistance
def verify_titans_memory_dynamics():
np.random.seed(42)
d_model = 32
d_hidden = 64
memory = TitansNeuralMemory(d_model=d_model, d_hidden=d_hidden, base_lr=0.08, momentum=0.85)
# Generate synthetic "Needle in a Haystack" key-value pair
needle_key = np.random.randn(d_model, 1)
needle_key /= np.linalg.norm(needle_key)
needle_val = np.random.randn(d_model, 1)
print("--- 1. Baseline Memory Evaluation ---")
val_before = memory.retrieve(needle_key)
init_error = np.linalg.norm(val_before - needle_val)
print(f"Pre-memorization Euclidean Distance: {init_error:.4f}")
print("\n--- 2. Test-Time Memorization (Surprise-Driven Gradient) ---")
# Simulate encountering the needle token in the context stream
for step in range(12):
surprise = memory.memorize_step(needle_key, needle_val, alpha_t=1.5, eta_t=0.001)
if step % 3 == 0:
print(f"Step {step:02d} | Instantaneous Surprise Loss: {surprise:.4f}")
val_learned = memory.retrieve(needle_key)
learned_error = np.linalg.norm(val_learned - needle_val)
print(f"Post-memorization Euclidean Distance: {learned_error:.4f}")
print(f"Associative Error Reduction: {((init_error - learned_error) / init_error) * 100:.1f}%")
print("\n--- 3. Testing Distractor Stream (Continuous Context Ingestion) ---")
# Ingest 100 random tokens (representing intervening text in a 1M token document)
for _ in range(100):
dist_key = np.random.randn(d_model, 1)
dist_key /= np.linalg.norm(dist_key)
dist_val = np.random.randn(d_model, 1)
memory.memorize_step(dist_key, dist_val, alpha_t=0.4, eta_t=0.002)
val_retained = memory.retrieve(needle_key)
retained_error = np.linalg.norm(val_retained - needle_val)
print(f"Euclidean Distance after 100 Distractors: {retained_error:.4f}")
print("Memory retained non-linear representation without catastrophic collapse!")
if __name__ == "__main__":
verify_titans_memory_dynamics() Production Advantages for Autonomous Agents and Enterprise Scraping
In real-world data extraction infrastructure and AI browser swarms like AiFetchly, deploying sequence models with neural long-term memory offers profound structural benefits:
- Constant-VRAM Long-Document Scraping: Ingesting 200-page regulatory filings or continuous DOM mutations previously required chunking documents into disconnected fragments, leading to cross-chunk reference loss. Titans maintains a constant memory footprint, processing millions of tokens in a single continuous forward stream.
- Persistent Multi-Agent State Tracking: In complex agent loops where a primary coordinator dispatches subagents across web forms and APIs, Titans’ neural memory acts as an internal episodic notebook, storing historical tool outputs and user preferences directly in weight activations.
- Hardware-Efficient Linear Throughput: Because the sliding-window attention uses a bounded context (e.g. 2,048 tokens), FlashAttention kernels execute with peak SRAM cache locality, avoiding memory bus saturation while the MLP memory updates via dense Tensor Core GEMMs.
Conclusion
Google’s Titans architecture introduces a paradigm shift in sequence modeling. By pairing the fleeting, high-resolution precision of sliding-window attention with an adaptive neural long-term memory updated at test time via surprise-driven gradients, Titans overcomes both the quadratic memory wall of transformers and the representational collapse of linear RNNs.
For engineers building production-grade AI systems, Titans provides a blueprint for scalable, infinite-context language models capable of learning and retaining knowledge throughout their entire operational lifetime.