n4nAI

Context window vs memory: what's the difference?

Understand the difference between context window and memory in LLMs — what each does, how they interact, and when to use which approach for your application.

n4n Team7 min read1,562 words

Audio narration

Coming soon — every post will get a voice note here.

The distinction between context window vs memory is one of the most common sources of confusion when designing LLM applications. The context window is the model’s short-term working memory — the fixed token budget it can attend to in a single forward pass. Memory, by contrast, is any mechanism that persists information across requests: retrieval-augmented generation, conversation summarization, external databases, or KV-cache reuse. They solve different problems, have different cost profiles, and compose in specific ways.

What the context window actually is

The context window is a hard architectural constraint. A model with a 128k context window cannot attend to token 128,001 in a single inference call. This limit is baked into the attention mechanism — specifically, the quadratic memory complexity of full attention over sequence length. When you exceed it, you get a hard error or silent truncation depending on the API.

Modern models ship with windows ranging from 4k (older GPT-3.5) to 2M tokens (Gemini 1.5 Pro). But the stated maximum is not the usable maximum. Attention quality degrades at the edges. Needle-in-haystack retrieval accuracy drops sharply past ~50-70% of nominal capacity for most models. You also pay for every input token, so stuffing the window is expensive.

# Typical context window limits (as of 2024)
CONTEXT_WINDOWS = {
    "gpt-4o": 128_000,
    "gpt-4o-mini": 128_000,
    "claude-3-5-sonnet": 200_000,
    "claude-3-opus": 200_000,
    "gemini-1.5-pro": 2_000_000,
    "llama-3.1-405b": 128_000,
    "mistral-large-2": 128_000,
}

The context window is stateless. Every request is independent. If you send the same 50k-token document in ten consecutive requests, you pay for 500k input tokens and the model re-processes the document ten times.

What memory means in practice

Memory is not a single thing. It’s a category of techniques for persisting and retrieving information across inference calls. The most common patterns:

Retrieval-augmented generation (RAG) — Embed documents, store vectors, retrieve top-k chunks at query time. The retrieved chunks are injected into the context window. This scales to arbitrary corpus size but adds latency (embedding + vector search) and introduces retrieval failure modes.

Conversation summarization — Periodically summarize prior turns into a compact representation, feed the summary instead of raw history. Cheap, but lossy. You decide what to keep.

External key-value stores — Persist user preferences, facts, or session state in Redis/PostgreSQL, inject relevant keys at runtime. Deterministic, fast, but requires explicit schema design.

KV-cache reuse / prefix caching — Some providers (including n4n.ai) cache the attention keys/values for common prefixes — system prompts, few-shot examples, large documents — so repeat requests skip recomputation. This is a provider-level optimization, not something you control directly, but it changes the cost model for repeated context.

Long-term memory agents — Autonomous systems that write to and read from a structured memory store (e.g., MemGPT, Letta). Still early, high complexity.

Memory systems trade latency and complexity for scale. They let you work with effectively infinite history, but you own the retrieval logic, the embedding pipeline, the chunking strategy, and the failure modes.

Capabilities comparison

Dimension Context window Memory systems
Max scale Hard limit (4k–2M tokens) Unbounded (limited by storage/index)
Access pattern Full attention over entire window Sparse retrieval (top-k, exact match, hybrid)
Latency Linear in tokens (prefill) Added retrieval step (10–500ms typical)
Cost model Pay per input token, every request Pay for storage + embedding + retrieval compute
Consistency Perfect — model sees everything Probabilistic — retrieval can miss
Statefulness None (stateless per request) Explicit — you manage persistence
Tooling maturity Native, zero-config Fragmented (LangChain, LlamaIndex, custom)
Failure mode Hard truncation / 400 error Silent retrieval miss, hallucination

Cost model: tokens vs infrastructure

Context window costs are predictable: input tokens × price per 1M. At $2.50/1M input (GPT-4o), a 100k context costs $0.25 per request. Ten requests = $2.50. No infrastructure to run.

Memory systems shift cost to infrastructure. A minimal RAG stack: vector DB (Pinecone/Weaviate/Qdrant), embedding model (text-embedding-3-small at $0.02/1M tokens), retrieval compute. For a 10M token corpus, embedding once costs ~$200. Monthly vector DB hosting starts around $50-200. Per-query retrieval adds ~5-50ms and fractional compute cost.

The crossover point depends on reuse. If you send the same large context repeatedly, prefix caching or KV-cache reuse (where available) can make the context window cheaper than RAG. If the corpus grows beyond the window or reuse is low, memory wins.

# Rough cost comparison for 100k tokens × 100 requests/day
# Context window only (GPT-4o @ $2.50/1M input)
daily_context_cost = 100_000 * 100 / 1_000_000 * 2.50  # $25/day

# RAG: embed once, retrieve top-4k tokens per query
# Embedding: 10M corpus @ $0.02/1M = $0.20 one-time
# Retrieval: 100 queries × 4k tokens @ $2.50/1M = $1.00/day
# Vector DB: ~$100/mo = $3.33/day
# Total daily (amortized): ~$4.50/day

Latency and throughput

Context window latency is dominated by prefill — the quadratic attention computation over input tokens. Prefill scales roughly linearly with tokens on modern kernels (FlashAttention), but 100k tokens still takes 500ms-2s on current hardware. Decode latency is unaffected by context length.

Memory systems add a retrieval step before prefill. Vector search on 10M vectors: 10-100ms on managed services, 1-10ms on local ANN indexes (HNSW). Embedding the query: 10-50ms. Total added latency: 20-200ms typical. This is usually negligible compared to prefill for large contexts, but matters for small contexts where prefill is fast.

Throughput-wise, large context windows consume more KV cache memory per request, reducing concurrent request capacity on GPU. A 128k context at FP16 needs ~2GB KV cache per request (2 layers × 128k × 4096 dims × 2 bytes). Memory systems keep per-request context small, enabling higher concurrency.

Ergonomics and developer experience

Context window is zero-config. You concatenate strings, send to API, done. No schema, no indexing, no retrieval tuning. The failure mode is obvious: 400 error or truncation.

Memory systems require real engineering decisions:

  • Chunking strategy (size, overlap, semantic vs fixed)
  • Embedding model selection (dimensions, latency, quality)
  • Retrieval topology (dense, sparse/BM25, hybrid, reranking)
  • Context assembly (how many chunks, ordering, token budgeting)
  • Evaluation (recall@k, answer quality vs ground truth)

These are not one-time choices. Chunking affects retrieval quality. Embedding model affects latency and cost. Retrieval parameters need tuning per domain. You need evals to know if changes help.

Frameworks (LangChain, LlamaIndex, Haystack) abstract some of this but introduce their own opinions and abstractions. Most production teams eventually eject to custom pipelines.

Ecosystem and tooling

Context window: universal. Every provider, every SDK, every framework supports it natively. No lock-in.

Memory: fragmented. Vector databases (Pinecone, Weaviate, Qdrant, Chroma, pgvector) have different APIs, consistency models, and scaling characteristics. Embedding providers (OpenAI, Cohere, Voyage, local models) have different dimensions and licensing. RAG frameworks impose opinions on chunking, retrieval, and prompt assembly. There is no standard — you pick a stack and own it.

KV-cache reuse / prefix caching is provider-specific. Some expose it explicitly (Anthropic prompt caching), some do it transparently. If you route through a gateway that normalizes this, you get the benefit without vendor lock-in.

Limits and failure modes

Context window limits are hard and documented. You hit the ceiling, you get an error. The model’s effective reasoning capacity also degrades near the limit — attention dilution, lost-in-the-middle effects.

Memory systems have softer but nastier failure modes:

  • Retrieval miss: Relevant context not in top-k → model hallucinates or says “I don’t know”
  • Stale data: Vector index not updated after document change → wrong answers
  • Chunking artifacts: Answer split across chunks, or chunk boundary cuts key entity
  • Embedding drift: Domain shift makes embeddings less effective over time
  • Index corruption: HNSW graph degradation, filter mismatches

These fail silently. You need observability: retrieval recall logging, answer quality evals, drift detection.

How they compose

This is the practical answer: you use both. The context window is the working memory for the current reasoning task. Memory systems populate that window with the right information.

Typical production pattern:

  1. Retrieve relevant chunks from vector store (memory)
  2. Assemble into prompt: system prompt + retrieved chunks + conversation summary + current query
  3. Send to model within context window
  4. Optionally cache the assembled prefix if the same chunks repeat
def build_prompt(query: str, user_id: str) -> list[dict]:
    # 1. Retrieve from memory (vector store)
    chunks = vector_store.query(query, top_k=5, filter={"user_id": user_id})
    
    # 2. Get conversation summary (memory)
    summary = get_conversation_summary(user_id, max_tokens=2000)
    
    # 3. Assemble into context window
    messages = [
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "system", "content": f"Relevant context:\n{format_chunks(chunks)}"},
        {"role": "system", "content": f"Conversation summary:\n{summary}"},
        {"role": "user", "content": query},
    ]
    
    # 4. Verify token budget
    total_tokens = count_tokens(messages)
    assert total_tokens < MAX_CONTEXT * 0.8, "Context overflow"
    
    return messages

The context window is the bottleneck; memory is the plumbing that feeds it.

Which to choose

Use context window alone when:

  • Total relevant context fits comfortably (<50% of window) with headroom
  • Requests are independent or low-reuse
  • You need perfect recall over the provided context
  • Zero infrastructure overhead is a priority
  • Prototyping — start here, add memory when you hit limits

Add memory (RAG) when:

  • Corpus exceeds context window
  • Same large documents reused across many queries (prefix caching helps but RAG scales better)
  • You need access control per document (filter by user/tenant at retrieval time)
  • Data updates frequently — re-embedding is cheaper than re-sending full context
  • You need citations / provenance (retrieval gives you source chunks)

Add conversation memory (summarization + KV store) when:

  • Multi-turn conversations exceed window
  • You need long-term user preferences / facts
  • Session state must survive restarts

Use KV-cache reuse / prefix caching when:

  • Same system prompt / few-shots / large document sent repeatedly
  • Provider supports it (Anthropic prompt caching, or gateway-level)
  • You want lower latency and cost without changing architecture

Avoid memory systems when:

  • Team lacks capacity to own retrieval quality (evals, monitoring, tuning)
  • Corpus is small and static — just stuff it in the window
  • Latency budget is extremely tight and you can’t tolerate retrieval variance

The bottom line

Context window vs memory is not a choice — it’s a hierarchy. The context window is the fixed-size workspace the model operates in. Memory is how you decide what gets into that workspace. Start with the window. Add memory when the window becomes the constraint. Build observability so you know which retrieval failures are costing you answer quality. And remember that every token in the window is a token you paid for — memory systems exist to make that spend efficient.

Tagscontext-windowmemoryllm-basicsglossary

Written by

n4n Team

The team building n4n — a single OpenAI-compatible API in front of 240+ models, with automatic fallback, load balancing and pay-per-token metering.

More from n4n Team →

All context window & context length posts →