Part of the Determesh series.

What

A company in Determesh may have hundreds of resources. The orchestrator never reads all their summaries. It searches them, reads the few that match, and searches again if those fall short. Search is keyword plus meaning, run in Postgres, limited to what the user may reach.

Why

Reading every summary does not scale

The obvious first version puts every resource summary in the orchestrator’s prompt. With five resources that works. With two hundred, the prompt is huge, mostly irrelevant, and the model’s choice gets worse as the list grows.

The better model is a coding agent: it does not read every file in a repository, it searches, then opens what matters. The orchestrator gets the same kind of tools: search_resources(query) and read_summary(resource_id).

Browsing a tree breaks need to know

There was a third tool at first, browse_tree, to walk the company’s folders. It was removed.

The trouble was a folder the user may not see, holding a file a wider rule allows. Search found the file. Browsing could not reach it, because the folder above it was hidden. Making browse show hidden folders when something inside is visible would leak the folder’s existence. The cleaner answer was to stop browsing. The tree is for the company to organise its resources. The orchestrator searches.

How

Two searches, merged

Each query runs two ways at once:

  • Keyword: Postgres full text search over names and summaries. Exact terms, product names, codes.
  • Meaning: vector search with pgvector over embeddings of the same text. “Leave balance” finds “holiday allowance”.

The two result lists are merged by reciprocal rank fusion: each resource scores by its rank in each list, so a resource near the top of both wins, and a resource that only one method found can still make it in. RRF needs no tuning of how keyword scores compare to vector distances, which never compare cleanly.

A vector per method

At first each resource had one vector, from its name and summary. A resource with many methods summarised in general terms lost to a smaller one whose summary happened to use the user’s words.

Now every method gets its own vector too, from its resource’s name, its own name, its summary and what it returns. A resource ranks as close as its best hit. “When is my next payslip” matches the payroll resource through its payslip method, even if the resource’s summary only says “payroll and benefits”.

Distance cut-offs

Vector search always returns something. Asked about the weather, it returns the least unrelated resource. So vector hits must pass two limits: an absolute distance, and closeness to the best hit in the same query.

The second limit exists because related and unrelated distances overlap on short summaries. Measured on test data, a genuinely relevant resource and an irrelevant one could sit at nearly the same distance from a query, so a fixed cut-off alone either let junk in or kept good hits out. Relative to the best hit, the gap is clearer.

Off-topic requests are not search’s problem anyway. The orchestrator checks the request against the company’s summary first, and clearly unrelated questions are refused without searching at all.

Our own embedding model

Embeddings use bge-m3, multilingual, run in Ollama on our own server. No company text goes to a third party to be embedded, it costs nothing per call, and it runs on CPU. Each region runs its own.

Embedding never blocks a save. A background sweep picks up resources whose name, summary or method summaries changed and embeds them. If the model is down, embeddings are delayed and search falls back to keyword only. Changing the model means re-embedding everything, and the sweep does that by itself when the model name changes.

Access in the SQL

Every search query is limited in the SQL itself: to the thread’s company, to the resources this user may reach, and to what the channel is restricted to. There is no step where all matches are fetched and then filtered in code, so a bug in the filtering cannot leak a result.

How the orchestrator uses it

A typical request: search with the user’s words, read the summary of the top match or two, ask the best one. If the answer falls short, search again with different words, or try the next match. The prompt does not prescribe a fixed strategy. The tools are cheap and only ever return what the user may reach, so the model can search as much as it needs.

There is one deliberate exception. A request that is outside the company’s main job but might be its internal work, like a staff HR question to a payments company, gets at most a couple of searches. Search only shows what this user may reach, so a match is meant for them. No clear match means a polite refusal.


Previous: Prompt Caching by Design

Next: Signing an API With HMAC, Properly