I nearly doubled my coding agent's memory recall with a 22M-parameter reranker (trained for under $5)
How I built a dataset, fine-tuned a small cross-encoder on a laptop, and shipped it into amem, with every number reproducible. amem is an open-source memory layer for coding agents. It stores short facts tied to the fil
How I built a dataset, fine-tuned a small cross-encoder on a laptop, and shipped it into amem, with every number reproducible.
amem is an open-source memory layer for coding agents. It stores short facts tied to the files they describe ("Stripe webhooks must be verified against the raw request body" โ src/webhooks/stripe.ts) and hands the right ones to Claude Code, Cursor or Codex when a prompt needs them. Retrieval is local and cheap: SQLite FTS5/BM25, a small n-gram encoder and a few ranking signals. No embeddings API, no cloud.
On our hand-written benchmark that works well: the right memory comes first 88% of the time. But that benchmark has a blind spot. Its questions mostly share words with the memory they're looking for, and real developers often don't ask that way.
The questions keyword search misses
A developer who doesn't already know the fact describes the symptom:
-
"how do I run training on multiple cards at once?", when the memory says
trainer=ddp -
"pods keep getting OOMKilled after I bumped memory limits, where do I edit that?", when the memory says the manifests live in
deploy/k8s - "cache key clear b4 or after db save? got weird old value once", when the memory says to invalidate after the transaction commits
There's no shared word to match. To measure how often this breaks amem, I needed a lot of questions like these, so I built a dataset.
Building the data: $4.64 and a lesson in "too easy"
I used Claude to write 200 synthetic projects across 40 domains (data pipelines, Kubernetes operators, iOS apps, game servers, fintech ledgers), deliberately excluding e-commerce so nothing overlaps the benchmark. For each project, Claude Sonnet 5.5 wrote 30โ40 facts anchored to files in its tree, and Claude Haiku 5.5 wrote questions a developer might ask about each fact. Through the Batch API, the whole run cost $4.64.
The first dry run taught me something. Only 2.6% of the generated questions avoided the fact's key words. The model kept writing "why does the YAML setting not load?" for a fact about YAML settings. That's easy for keyword search and useless for training. The fix was blunt: give the model each fact's distinctive words as an explicit avoid list, and require two symptom-only questions per fact. That got it to about 40%.
Then the important part: hard negatives from amem itself. Every project went into a throwaway amem store, every question ran through amem's real retrieval, and the wrong memories amem ranked highest became the negatives. A reranker trained this way learns to fix exactly the mistakes this retrieval system makes.
The result is iamem/amem-memory-queries: 6,963 facts and 27,957 questions (natural, messy and vague prompts like "ok go ahead"), split by project so the test set is 10 projects the model never sees.
The baseline was worse than I expected
On those test questions, amem put the right memory first 32.6% of the time. Its prompt hook, the automatic path, managed 34.3%. Hand-written benchmarks flatter keyword search, and this dataset doesn't.
Fine-tuning: 13 minutes on a MacBook
I started from cross-encoder/ms-marco-MiniLM-L6-v2, a 22M-parameter reranker trained on web search, and fine-tuned it with sentence-transformers on 268,612 (question, memory) pairs: one epoch, binary cross-entropy with positives up-weighted. It took about 13 minutes on Apple silicon.
Evaluation mirrors production: for each test question, amem retrieves its top 20 memories and the model reorders them. A question only counts if the right memory is in that list and lands first.
| Test split (1,497 questions) | Right memory first | In top 3 | Vague prompts given nothing |
|---|---|---|---|
| amem retrieval alone | 32.6% | 52.1% | 96.3% |
| Base MS MARCO MiniLM, not fine-tuned | 55.4% | 74.7% | 32.5% |
| Fine-tuned (iamem/amem-reranker-minilm) | 68.8% | 82.3% | 98.8% |
The ceiling is 87.6%, which is how often the right memory is anywhere in amem's top 20.
The honest part: where the gain actually comes from
Before training I ran the untrained base model as a control, and I'm glad I did. Most of the ranking gain comes from the base model. A web-search reranker already understands that "multiple cards" and "distributed training" are related, and it lifts top-1 from 33% to 55% with no work from me.
Fine-tuning adds another 13 points on top of that. But its biggest effect is somewhere I didn't expect: knowing when to stay quiet. The base model scores "ok go ahead" as relevant to something two-thirds of the time, so an agent would get memory injected into every "sounds good". The fine-tuned model rejects 98.8% of vague prompts. For a memory layer that injects context automatically, that matters as much as ranking. Every wrong injection costs tokens and attention.
On the hand-written benchmark (32 questions), the fine-tuned model reaches 93.8% first and 100% in the top 3. The base model ties it on top-1, so I don't claim a benchmark win. With 32 questions, the test split is the number to trust.
Shipping it into a Node tool
amem is a Node CLI and MCP server, and the reranker had to run inside it without making the package heavy.
-
Size. The obvious route, transformers.js, pulled in 472 MB of dependencies. Instead amem uses
onnxruntime-nodedirectly, with only the current platform's binaries kept, plus an 8-bit version of the model: 23 MB, with the same accuracy on the test split (68.5% vs 68.8%). - Tokenizer. I wrote a ~130-line BERT WordPiece tokenizer in TypeScript. It matches Hugging Face's tokenizer exactly on 495 test cases, including accents, CJK text and the edge cases of truncating long pairs.
- Speed. Scoring 20 memories takes about 0.1 s, including loading the model in a fresh hook process.
-
Safety. It's opt-in (
amem rerank enable). The model is pinned to a revision and checked against SHA-256. Scoring is local. If anything fails or is slow, amem falls back to its normal ranking.
Measured end to end inside amem on the test split, the prompt hook goes from 34.3% to 63.1% right-memory-first and from 96.2% to 98.8% on vague prompts given nothing.
What I'd tell someone doing the same
- Check your benchmark for word overlap. If its questions share words with the answers, it's measuring keyword search, not understanding.
- Run the untrained base model as a control. Otherwise you'll credit your data with gains a pretrained model gives you for free.
- Mine hard negatives from your own system. Random negatives teach the model what's obviously wrong; your retriever's mistakes teach it what's subtly wrong.
- Measure "say nothing". For anything that injects context automatically, rejecting irrelevant prompts is a first-class metric.
- Small models go a long way. A 22M-parameter cross-encoder, 13 minutes of training and under $5 of data closed about two-thirds of the gap between amem's ranking and the ceiling.
Try it
npx @iamem/amem setup # or /plugin marketplace add sslugic/amem in Claude Code
amem rerank enable
- Model: iamem/amem-reranker-minilm
- Training data: iamem/amem-memory-queries
- Benchmark: iamem/amem-retrieval-bench
- Code for data, training and evaluation:
tools/reranker-data - amem: github.com/sslugic/amem ยท getamem.com
Questions, criticism and better baselines are welcome.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes โ full credit and traffic to the original publisher.