4 min read

I Built a Search Engine for My Agents. They Used Grep.

One query against my search index cost about 750 tokens. The agents used grep anyway. The fix wasn't a better index. It was putting the index where they'd trip over it.
I Built a Search Engine for My Agents. They Used Grep.
The good tool was one shelf over.

Fixing the tool nobody reached for

💡
TL;DR
- One query against my search index cost about 750 tokens. Reading the same files the long way cost ten to fifty times that.
- The agents ignored it anyway. Grep was already in their hand. The index was a command they had to remember.
- Putting it in the tool list, next to grep, is what changed that. Better was never the problem. Reachable was.
- Hiding things from a search index by folder doesn't hide the topic. The tool now says so out loud.

I spent real time on this.

A nightly job reads the documentation across our repos: runbooks, design specs, decision records, vendor guides. It chunks them, embeds them on a VM we already had, and stores them in Postgres with a hybrid search on top, meaning keyword and meaning at the same time. Ask a question, get back cited snippets with the file each one came from.

One query came back in about 750 tokens. An agent answering the same question by opening files would burn ten to fifty times that, and fill its context with pages it read once and never needed again.


Then I watched the sessions. They grepped. They opened files. They grepped again.
Not occasionally. By default.


Why it lost

The index wasn't wrong. It was invisible.

It lived behind a command-line tool. To use it, an agent had to decide to shell out to a program it had read about at the start of the session, possibly an hour and a compaction ago. Grep and Read are built-in tools. They are sitting right there in the list the model picks from on every turn.

A model mid-task reaches for what's in its hand. I know. I do the same thing.

So the problem wasn't quality. It was where the tool sat.


Putting it where they'd trip over it

The fix was small. I wrapped the search in an MCP server, a little program that speaks the protocol agents use to discover tools. It has no dependencies and exactly one tool, a knowledge search that takes a query, how many results to return, and a couple of switches.

That did two things.

First, the search now shows up in the tool list, right next to Grep and Read. Same shelf.

Second, an MCP server gets to hand the session a short set of instructions when it connects. Those get injected into every session, with no extra hook. Mine say, roughly: search here before you grep, and trust the cited snippet instead of re-reading the file it came from.

That second part matters more than it sounds. Without it, an agent would search, get a perfectly good answer with a citation, and then open the file anyway to double-check. Which is responsible, and costs exactly what the index was supposed to save.

The server also logs every call. Query, hit count, latency, day. A small script turns that log into a summary.

"Am I actually using the thing I built?" stopped being a feeling and became a number.

Over the last 30 days, the number is 227 searches from my sessions, about seven and a half a day, on 25 of the 30 days. None failed. None came back empty. The median search takes under two seconds, which is slower than grep and still far cheaper than reading the files.


What the index won't tell you

Not everything belongs in a search index that other people can query.

Teammates outside engineering use this knowledge base too, so a few things are held back on purpose: the infrastructure reference notes, the cross-cutting audits, and anything in a secrets folder. The nightly job skips them by path. It also skips anything archived or marked superseded, because an index that serves retired docs as current truth is worse than no index.

Here's the catch I didn't see at first. Withholding by folder is not withholding by topic.

Ask the index about a host or a VM, and you don't get nothing. You get hits from the neighbors: a runbook that mentions the host in passing, an inventory doc somewhere else. Those can be stale, or just less authoritative than the note that was held back. An agent has no way to know the better answer exists and was hidden.

So the tool's description and the server's instructions now say it plainly. These folders are not indexed, by design. Hits on those topics come from neighboring docs and may be out of date. For infrastructure facts, go read the withheld folder in the checkout.

It felt strange to write documentation that says "this tool will give you a confident, wrong-ish answer on these subjects." It's the most useful paragraph in the whole server.


The order of things

Once the index was reachable, the rules file got a line about order:

  1. Knowledge search first, for "how does this work, what was the plan, what does the vendor doc say.
  2. The code graph next, for structure and call paths across repos.
  3. Grep and Read when you already know the file, or you're changing code.

    Grep didn't lose its job. It lost its monopoly.

Where I Landed

  • The search is in every session's tool list on my VM, with instructions to use it first.
  • Every call is logged, so I can check whether it's actually being used instead of guessing.
  • A networked copy serves a coworker's sessions, with their own token. It has been called 11 times, total. That's a later post, and a different problem.
  • What's held back is held back by folder, and the tool admits what that does and doesn't protect.
  • The agents still grep. That's fine. Grep is the right tool when you already know which file you want.

I built a search engine and then had to put up a sign telling the agents it was there.

I've worked in a few offices where the documentation had the same problem.

Next: my notebook feeds the machine.