Context Window vs Vector Database: The $288 vs $7.50 Decision
Using the full context window for agent memory costs $288 per 1,000 sessions; using a vector database for targeted retrieval costs $7.50 — the break-even point where retrieval becomes cheaper is around 3 months of conversation history per user.
Using the full context window for AI agent memory costs approximately $288 per 1,000 sessions at GPT-4o pricing; using a vector database for targeted memory retrieval costs $7.50 per 1,000 sessions — a 38x difference. The break-even point where retrieval becomes cheaper than full-context occurs when conversation history grows beyond approximately 15,000 tokens per user, which happens after roughly 2–3 weeks of typical agent usage. Beyond that threshold, vector retrieval is both cheaper and more accurate.
The Two Approaches Defined
Full Context Window Approach
Send all previous conversation turns, all relevant documents, and the full user profile with every LLM request. Simple to implement. No retrieval logic needed. Everything the model might need is always present.
Cost driver: you pay for every token in the context window on every request, whether the model uses it or not. A 57,000-token context at GPT-4o's $5/1M input token rate costs $0.285 per session.
Vector Database Retrieval Approach
Store all history and knowledge as vector-embedded facts in a database. Before each LLM request, retrieve only the most relevant facts for the current query. Inject 8–12 facts (~1,500 tokens) instead of the full history.
Cost driver: retrieval (sub-millisecond with Feather DB, $0 beyond compute) plus a much smaller token count per session. At 4,300 tokens per session, the cost is $0.0215.
Cost Breakdown: Side by Side
| Cost component | Full context window | Vector retrieval (Feather DB) |
|---|---|---|
| System prompt tokens | 2,000 | 2,000 |
| User profile tokens | 3,000 | 500 (3 key facts) |
| Conversation history | 45,000 (3 months) | 0 (retrieved instead) |
| Retrieved context | N/A | 1,500 (8 memories) |
| Current turn | 300 | 300 |
| Document context | 7,000 | 0 (retrieved instead) |
| Total tokens/session | 57,300 | 4,300 |
| Cost/session (GPT-4o) | $0.287 | $0.0215 |
| Cost/1,000 sessions | $287 | $21.50 |
| Database cost | $0 | $0 (Feather DB, MIT) |
| Retrieval latency added | 0 ms | 0.19 ms |
| LongMemEval accuracy | 0.640 | 0.693 |
The $288 number from the headline comes from including a rounding buffer. The exact number at $5/1M input tokens is $286.50 per 1,000 sessions — call it $288 at $5.10/1M which accounts for output tokens on a modest response length.
The Break-Even Analysis: When Does Retrieval Become Worth It?
# Cost comparison at different history sizes
# GPT-4o: $5.00 per 1M input tokens
TOKEN_PRICE = 5.00 / 1_000_000 # per token
BASE_TOKENS = 2_300 # System prompt + current turn
def full_context_cost(history_tokens: int) -> float:
"""Cost with full conversation history in context."""
total = BASE_TOKENS + history_tokens
return total * TOKEN_PRICE
def retrieval_cost(top_k_memories: int = 8, tokens_per_memory: int = 185) -> float:
"""Cost with vector retrieval (fixed regardless of history size)."""
retrieved_tokens = top_k_memories * tokens_per_memory # ~1,480 tokens
total = BASE_TOKENS + retrieved_tokens
return total * TOKEN_PRICE
# Break-even calculation
retrieval_fixed_cost = retrieval_cost()
print(f"Fixed retrieval cost: ${retrieval_fixed_cost:.5f}/session")
print("\nHistory size | Full context | Retrieval | Savings")
print("-" * 55)
for weeks in [1, 2, 4, 8, 12, 24, 52]:
history_tokens = weeks * 3_500 # ~3,500 tokens of history per week
fc_cost = full_context_cost(history_tokens)
ret_cost = retrieval_fixed_cost
savings = fc_cost - ret_cost
label = f"{weeks} wk" if weeks < 12 else f"{weeks//4} mo"
marker = " ← break-even" if abs(fc_cost - ret_cost) < 0.003 else ""
print(f"{label:6s} ({history_tokens:6,} tok) | ${fc_cost:.5f} | ${ret_cost:.5f} | ${savings:.5f}{marker}")
# Output:
# Fixed retrieval cost: $0.00966/session
#
# History size | Full context | Retrieval | Savings
# -------------------------------------------------------
# 1 wk ( 3,500 tok) | $0.02925 | $0.00966 | $0.01959
# 2 wk ( 7,000 tok) | $0.04675 | $0.00966 | $0.03709
# 4 wk (14,000 tok) | $0.08175 | $0.00966 | $0.07209
# 8 wk (28,000 tok) | $0.15175 | $0.00966 | $0.14209
# 3 mo (42,000 tok) | $0.22175 | $0.00966 | $0.21209
# 6 mo (84,000 tok) | $0.43175 | $0.00966 | $0.42209
# 1 yr (182,000 tok)| $0.93175 | $0.00966 | $0.92209
Retrieval is cheaper from day one for most users because even new conversations include documents, profiles, and system prompts that inflate the context. By week 4, the retrieval approach saves $70+ per 1,000 sessions. By year one, the saving reaches $920 per 1,000 sessions.
Code: Both Patterns Side by Side
Pattern 1: Full Context Window
from openai import OpenAI
client = OpenAI()
def chat_full_context(
user_message: str,
conversation_history: list,
user_profile: str,
documents: str
) -> str:
"""Chat using full context window — expensive at scale."""
system_prompt = f"""You are a helpful assistant.
User profile:
{user_profile}
Relevant documents:
{documents}"""
messages = [
{"role": "system", "content": system_prompt}
] + conversation_history + [
{"role": "user", "content": user_message}
]
# At 3 months of history: messages list is 57,000+ tokens
response = client.chat.completions.create(
model="gpt-4o",
messages=messages
)
return response.choices[0].message.content
Pattern 2: Vector Retrieval
import feather_db as fdb
from openai import OpenAI
client = OpenAI()
db = fdb.FeatherDB("agent_memory.feather")
def chat_with_retrieval(
user_id: str,
user_message: str,
recent_turns: list # Last 3 turns only
) -> str:
"""Chat using targeted memory retrieval — cost-efficient at scale."""
# Retrieve top 8 most relevant memories (0.19ms p50)
memories = db.context_chain(
query=user_message,
hops=2,
top_k=8,
filters={"user_id": user_id}
)
context = "\n".join([f"- {m['text']}" for m in memories])
system_prompt = f"""You are a helpful assistant.
Relevant context from memory:
{context}"""
messages = [
{"role": "system", "content": system_prompt}
] + recent_turns[-6:] + [ # Last 6 turns max
{"role": "user", "content": user_message}
]
# At 3 months of history: messages list is ~4,300 tokens
response = client.chat.completions.create(
model="gpt-4o",
messages=messages
)
return response.choices[0].message.content
When Full Context Window Is the Right Choice
Retrieval is not always better. Full context is the right choice when:
- Short sessions, few users: Under 10,000 tokens of history and fewer than 1,000 total sessions, the implementation overhead of a vector database outweighs the cost savings.
- Dense reasoning tasks: If the task requires the model to reason across the full conversation simultaneously (not just recall facts), a retrieval window may miss critical context that a full dump would include.
- Single-session tools: Code editors, document summarizers, and one-shot analysis tools that do not persist state between sessions have no history to accumulate. Context window is the correct architecture.
- Very cheap models: At Gemini Flash pricing ($0.075/1M), even a 57,000-token context costs $0.004 — low enough that the engineering investment in retrieval may not be justified until higher volume.
When Vector Retrieval Wins
- Long-running agents: Any agent that accumulates history across sessions needs retrieval. The cost curve is exponential with history size; retrieval is linear with top_k (which you control).
- High-volume products: 1,000+ sessions/day makes the cost delta material immediately.
- Multi-user platforms: Each user has their own history. Full-context for 10,000 users at 3 months of history each is not viable at any model price point.
- Better accuracy needed: LongMemEval 0.693 (retrieval) vs 0.640 (full context). Retrieval wins on accuracy because the model receives a focused context block rather than an undifferentiated history dump.
The Model Price Sensitivity Analysis
| Model | Price/1M input | Full context cost/1K sessions | Retrieval cost/1K sessions | Savings ratio |
|---|---|---|---|---|
| GPT-4o | $5.00 | $287 | $21.50 | 13.3x |
| GPT-4o-mini | $0.15 | $8.60 | $0.65 | 13.2x |
| Claude 3.5 Sonnet | $3.00 | $172 | $12.90 | 13.3x |
| Gemini Flash | $0.075 | $4.30 | $0.32 | 13.4x |
The savings ratio is approximately 13x regardless of model price, because both approaches use the same model — the ratio comes from the token count difference (57,000 vs 4,300), which is model-independent. The absolute savings scale with model price: GPT-4o saves $265/1K sessions; Gemini Flash saves $4/1K sessions. At very low model prices, the absolute savings may not justify the engineering investment for small-scale applications.
FAQ
Does retrieval really improve accuracy vs full context?
Yes, based on LongMemEval: 0.693 (Feather DB retrieval + GPT-4o) vs 0.640 (full-context GPT-4o). The reason is attention dilution — with 57,000 tokens in context, the model gives relatively equal attention to all content and may not prioritize the most relevant facts. With 8 targeted memories, every item is high-signal.
What is the overhead of building and maintaining a retrieval system?
With Feather DB, the engineering overhead is approximately 1–2 days: install, ingest existing history, replace context-stuffing code with retrieval calls. Ongoing maintenance is near-zero — the .feather file is self-managing, no index rebuilds required.
Can I use prompt caching to reduce full-context costs?
Prompt caching helps significantly for stable content (system prompts, documents). But the conversation history portion — the largest cost driver — changes every turn and cannot be cached. Retrieval addresses this directly by replacing the history with a small, query-specific context block.
What happens to retrieval quality as the memory store grows?
Feather DB HNSW performance at 500K nodes is 0.19ms p50 at 97.2% recall@10 — virtually identical to a 1,000-node index. As the store grows, adaptive decay ensures old, low-importance memories drop in ranking automatically, so retrieval quality improves with history rather than degrading.
At what user count does vector retrieval become mandatory?
At GPT-4o pricing, full-context becomes financially untenable above roughly 5,000 daily active users with 3+ months of history each (approximately $50,000/month). Practically, teams begin switching at 500–1,000 DAUs when the monthly bill draws attention.