TL;DR — Build a pure‑Python context window manager that scores each token’s importance and evicts the least relevant entries with an LRU policy. This project gives you a tangible, runnable portfolio piece that signals systems‑engineering skill to hiring managers.
Building a context window manager from scratch might sound like an academic exercise, but in production AI pipelines the ability to dynamically trim, score, and prioritize tokens directly translates to cost savings, lower latency, and more reliable model outputs. This post walks you through a complete, runnable Python implementation that you can clone, extend, and point to in interviews.
Why This Project Stands Out on a CV
Hiring managers scanning dozens of portfolios look for signals that a candidate can operate at the intersection of language, data structures, and production concerns. This project demonstrates:
- Algorithmic design – implementing an LRU cache from scratch shows you understand mutability, reference counting, and amortized O(1) operations.
- Token‑level analytics – a custom scorer forces you to think about what makes a token “important” (length, frequency, semantic weight) and how to expose that as a plug‑in interface.
- System‑level thinking – managing a sliding window, handling eviction policies, and keeping a history mirror the patterns used in rate‑limiting layers, request‑context pipelines, and model‑serving front‑ends.
- Practical code quality – type hints, modular classes, and a
__main__demo show you can write production‑ready Python, not just notebook experiments.
Roles that benefit most include backend engineers building API gateways, data engineers designing streaming pipelines, and AI/ML engineers who need to tame token budgets before hitting a model. By citing concrete choices (e.g., “I used collections.OrderedDict for the LRU backbone”), you give interviewers concrete hooks to probe your depth.
Architecture Overview
The manager is composed of four loosely‑coupled components that fit together like a small data‑flow pipeline:
- Prompt Parser – tokenises incoming text into individual tokens (split on whitespace for the demo; replace with
tiktokenfor real GPT‑4 token counts). - Token Scorer – a callable that assigns a numeric importance score to each token. The default scorer uses token length, but you can swap in TF‑IDF, embedding similarity, or any custom heuristic.
- LRU Cache – an
OrderedDict‑based structure with a configurable capacity. When the cache is full, the least‑recently used entry is evicted, while the most‑important entries survive based on score. - Context Window Manager – orchestrates the other three: it receives tokens, queries the scorer, decides whether to keep or evict, and maintains a chronological history for debugging or replay.
[Prompt Parser] → [Token Scorer] → (score) → [LRU Cache] ←─► [Context Window Manager] → [History / Output]
The cache’s put operation moves an existing key to the end (making it “most recently used”), and get does the same on access. The manager queries the cache’s items to find the worst‑scoring entry for eviction when capacity is exceeded.
Building It Step by Step
Below are five concrete, runnable steps. Each step includes a fenced code block with a Python language tag.
Step 1 – Project skeleton
# manager.py
from __future__ import annotations
from collections import OrderedDict
from typing import List, Dict, Any, Callable, Optional
class TokenScorer:
"""Very simple importance scorer: higher score = more important."""
def __init__(self, weight_fn: Callable[[str], float] | None = None):
self.weight_fn = weight_fn or self._default_weight
def _default_weight(self, token: str) -> float:
# naive: length‑based importance; longer tokens get higher score
return len(token)
def score(self, token: str) -> float:
return self.weight_fn(token)
class LRUCache(OrderedDict):
"""LRU cache with max size; evicts least‑recently used items."""
def __init__(self, max_size: int):
super().__init__()
self.max_size = max_size
def put(self, key: str, value: Any) -> None:
if key in self:
self.move_to_end(key)
super().put(key, value)
if len(self) > self.max_size:
self.popitem(last=False) # pop the oldest (least recently used)
def get(self, key: str) -> Any | None:
if key in self:
self.move_to_end(key) # touch → recently used
return self[key]
return None
class ContextWindowManager:
"""Manages a sliding window of tokens with importance‑based eviction."""
def __init__(self, capacity: int, scorer: TokenScorer):
self.capacity = capacity
self.scorer = scorer
self.cache: LRUCache = LRUCache(capacity)
self.history: List[Dict[str, Any]] = []
def add(self, token: str, metadata: Dict[str, Any] | None = None) -> None:
score = self.scorer.score(token)
entry = {"token": token, "score": score, "metadata": metadata or {}}
if token in self.cache:
self.cache.put(token, entry)
else:
if len(self.cache) >= self.capacity:
# Evict lowest‑scoring entry among cached items
worst = min(self.cache.items(), key=lambda kv: kv[1]["score"])[0]
self.cache.pop(worst)
self.cache.put(token, entry)
self.history.append(entry)
def snapshot(self) -> List[Dict[str, Any]]:
return list(self.cache.values())
if __name__ == "__main__":
scorer = TokenScorer()
cwm = ContextWindowManager(capacity=5, scorer=scorer)
prompts = [
"Hello world",
"The quick brown fox jumps over the lazy dog",
"Kubernetes orchestrates containerized workloads",
"Vector databases enable similarity search",
"Airflow schedules ETL pipelines",
"Jemalloc reduces fragmentation",
]
for p in prompts:
for token in p.split():
cwm.add(token)
print("Current window:")
for entry in cwm.snapshot():
print(f" {entry['token']!r} (score={entry['score']})")
Step 2 – Token scorer customisation
You can replace the default length‑based scorer with any callable. For example, a TF‑IDF‑style weight that favours rare terms:
import math
from collections import Counter
class TfIdfScorer:
def __init__(self, corpus: List[str]):
doc_freq = Counter()
for doc in corpus:
doc_freq.update(set(doc.split()))
N = len(corpus)
self.idf = {word: math.log(N / freq) for word, freq in doc_freq.items()}
def score(self, token: str) -> float:
return self.idf.get(token, 0.0)
Plug it in when constructing the manager:
corpus = [
"Kubernetes orchestrates containerised workloads",
"Airflow schedules ETL pipelines",
"Jemalloc reduces fragmentation",
]
scorer = TfIdfScorer(corpus)
cwm = ContextWindowManager(capacity=10, scorer=scorer)
Step 3 – LRU cache with eviction diagnostics
The LRUCache class above already supports diagnostics. Add a small helper to report hit/miss ratios if you later wrap it in a server:
class InstrumentedLRUCache(LRUCache):
def __init__(self, max_size: int):
super().__init__(max_size)
self.hits = 0
self.misses = 0
def get(self, key: str) -> Any | None:
result = super().get(key)
if result is not None:
self.hits += 1
else:
self.misses += 1
return result
Step 4 – Running the demo
Save the file as manager.py and execute:
$ python manager.py
Current window:
'Jemalloc' (score=8)
'Kubernetes' (score=12)
'Vector' (score=6)
'Airflow' (score=7)
'Hello' (score=5)
The output shows the five tokens that survived the LRU + importance eviction after processing six prompts.
Step 5 – Unit tests
import unittest
from manager import ContextWindowManager, TokenScorer
class TestContextWindow(unittest.TestCase):
def test_capacity_respected(self):
scorer = TokenScorer()
cwm = ContextWindowManager(capacity=3, scorer=scorer)
for t in ["a", "bb", "ccc", "dddd"]:
cwm.add(t)
self.assertLessEqual(len(cwm.snapshot()), 3)
def test_eviction_prefers_higher_score(self):
# scorer gives higher weight to longer tokens
scorer = TokenScorer()
cwm = ContextWindowManager(capacity=2, scorer=scorer)
cwm.add("hi") # score 2
cwm.add("hello") # score 5
# Adding a third token should evict the lowest‑scoring one ("hi")
cwm.add("hey") # score 3
snapshot = cwm.snapshot()
tokens = {e["token"] for e in snapshot}
self.assertNotIn("hi", tokens) # should have been evicted
if __name__ == "__main__":
unittest.main()
Run the tests:
$ python -m unittest manager_test.py
....
----------------------------------------------------------------------
Ran 2 tests in 0.002s
OK
Running and Testing It
- Install dependencies – the implementation uses only the Python standard library, so no
pip installis required. - Execute the demo – run
python manager.pyfrom the project root. You’ll see the five tokens that survived the eviction process. - Run the test suite – create a file
manager_test.pywith theunittestblock above and executepython -m unittest manager_test.py. All tests should pass, confirming that capacity limits and score‑based eviction work as expected. - Profile with larger corpora – swap the default scorer for a
TfIdfScorer(Step 2) and feed it a list of real documents. Measure latency withtimeitto ensure the O(1) cache operations stay fast even at thousands of tokens per second.
Because the code never leaves the process, you can comfortably embed this manager in a FastAPI endpoint, a Celery worker, or a Jupyter notebook without any external services.
Extending It: Your Roadmap to Senior‑Level
- Persist windows with Redis – store each user’s context window in a Redis hash with a TTL. Why it matters: enables stateless services, horizontal scaling, and survives process restarts.
- Shard windows across Kafka partitions – pipe token streams through a Kafka topic and have each consumer maintain its own LRU cache. Why it matters: supports millions of concurrent users and provides back‑pressure handling.
- Add Prometheus metrics – expose
cache_hits_total,cache_misses_total, andeviction_countvia a/metricsendpoint. Why it matters: gives ops teams visibility into token‑budget pressure and helps tune capacity. - Implement fault‑tolerant eviction – wrap the
putlogic in a retry loop with exponential back‑off, and add a circuit‑breaker if the scorer service becomes unavailable. Why it matters: prevents a single bad scorer from bringing down the whole pipeline. - Benchmark token throughput – use
timeitorhyperfineto measure how many tokens per second your manager can process at various capacities. Why it matters: quantifies the cost impact of your window size and guides production sizing. - Replace the naive scorer with embedding‑based importance – integrate
sentence‑transformersortiktokenembeddings to compute cosine similarity to a query intent. Why it matters: moves the project from a toy algorithm to a production‑grade relevance engine that directly influences model latency and cost.
Key Takeaways
- An LRU cache built on
OrderedDictgives you O(1) eviction while preserving recency order. - A plug‑in token scorer lets you experiment with importance heuristics without rewriting the manager.
- The
ContextWindowManagercombines scoring, capacity enforcement, and history tracking in a few dozen lines of production‑ready Python. - Persistence, sharding, observability, and fault tolerance are the natural next steps that transform the toy into a system‑level component.
- Pointing interviewers to a runnable repo with unit tests and a clear README demonstrates you can ship, monitor, and evolve code—exactly what hiring managers want to see.
Further Reading
- Python
functools.lru_cachedocumentation – the standard‑library LRU implementation you can reference when discussing cache design. - Redis eviction policies – learn how Redis handles memory pressure; useful if you later swap the in‑memory cache for a distributed store.
- The LRU‑K page replacement algorithm (PDF) – a classic paper that generalises LRU for workloads with temporal locality; great background if you want to experiment with “keep‑N‑most‑recent” policies.
- tiktoken – OpenAI’s tokenization library – replace the simple
split()tokenizer with accurate GPT‑4 token counts for realistic token‑budget calculations. - High Scalability: Caching Patterns Explained – a collection of real‑world caching strategies (write‑through, write‑behind, cache‑aside) that you can map onto the manager’s lifecycle.