1 // The Core Problem: Stateless Models vs Stateful Conversations
At a fundamental hardware and math level, Large Language Models (LLMs) are completely stateless functions. Given an input sequence of tokens, the transformer architecture computes attention scores across that sequence and predicts the probability distribution for the next token. When you ask a follow-up question in a chat session, the model itself has zero biological or server-side memory of your previous turn.
To create the seamless experience of ongoing dialogue in applications like ChatGPT, systems must resend previous conversation history back to the model on every single turn.
The Quadratic Cost Bottleneck
Standard self-attention mechanisms scale quadratically O(N²) in compute and memory with respect to prompt sequence length N. As chat sessions extend from 5 turns to 50 turns, reprocessing entire uncompressed transcripts causes exponential spikes in latency (Time to First Token) and VRAM consumption.
2 // The 300-Page PDF Mental Model (RAG vs Pure Memory)
To understand how production systems manage massive histories without blowing up GPU memory, consider a helpful engineering mental model: Asking a question about a 300-page technical manual.
There are two ways an AI application can answer a query about page 240:
Approach A: Naive Full Ingestion
Stuffing all 300 pages (~150,000 tokens) into a 1M token context window on every prompt. High cost, 10x slower inference, massive compute waste.
Approach B: RAG & Chunk Retrieval
Chunking the PDF into small 500-token vectors. Retrieving only the 3 most relevant pages (~1,500 tokens) and injecting them into the prompt. Fast, cheap, exact.
3 // The Multi-Tiered Context Management Pipeline
Production systems like ChatGPT use a hybrid, multi-tier strategy that combines the immediacy of short-term memory with the efficiency of vector retrieval and semantic summarization.
Sliding Window Buffer (Immediate Attention)
Keeps the last N turns (e.g., last 10 messages) raw and uncompressed in the prompt. This ensures 100% exact fidelity for immediate back-and-forth context, pronouns, and quick follow-ups.
Hierarchical Summarization (Background Rollup)
As older turns fall out of the sliding window, background worker processes condense them into structured bullet points or executive summaries. A 5,000-token exchange is compressed into a 200-token summary block.
Persistent Memory & Vector Profile Retrieval
Explicit user preferences (e.g., "Always write code in TypeScript", "I live in San Francisco") are extracted asynchronously by a secondary LLM worker, embedded, and saved in a vector store. When relevant, these facts are fetched and dynamically injected into the system prompt.
4 // System Optimization: KV Caching & vLLM PagedAttention
Summarizing and retrieving context reduces token counts, but at the GPU infrastructure level, processing long prompts still incurs heavy memory overhead due to key-value (KV) activations.
When a model generates tokens sequentially, calculating key and value vectors for past tokens on every step is redundant. Modern serving frameworks (vLLM, TensorRT-LLM) use KV Caching and PagedAttention:
How PagedAttention Prevents VRAM Waste
- Non-Contiguous Allocation: Similar to OS virtual memory paging, PagedAttention stores KV caches in non-contiguous physical memory blocks, reducing VRAM fragmentation from 60% down to under 4%.
- Prefix Caching: Common system prompts and initial conversation turns are shared across parallel user sessions in GPU memory, avoiding duplicated matrix calculations.
5 // Full End-to-End System Flow
Bringing all components together results in a robust, low-latency production architecture for long-running conversational AI:
6 // Key Engineering Takeaways
Context window management is an active systems engineering challenge, not just a model capacity limit.
Combining stochastic retrieval with deterministic cache policies is essential for high-fidelity agent sessions.
KV-caching requires rigorous garbage collection to prevent GPU VRAM fragmentation during parallel user sessions.

