Skip to content
Blog

The scholars speak. We don’t.

Why no model writes answers in Bayan Library, and how we built agentic search that only searches.

Bayan6 min read

Bayan Library has one rule that shapes more of it than any other: no model writes anything a person is shown as an answer. This post is about what it rules out, why it matters in an Islamic app, and how we built agentic search without breaking it.

The rule

We write it as the scholars speak; Bayan doesn’t. A search returns the scholars’ own passages, unchanged, cited to book, volume and page. Cleaning a text tidies its setting (tags, entities, footnote and page markers) and never changes a word. The library does make altered copies of the text, with harakat stripped (and, for the keyword index, letters unified), but only for its own machinery: the search index, quote matching, and what the models that find passages read. Nobody is shown them.

A summary is a misattribution

Many retrieval systems end with a model writing an answer from what was found. In most fields a loose summary is a quality problem. In an Islamic app it is a problem of attribution. A sentence shown under the name of Ibn Kathir or al-Qurtubi is read as what that scholar said. If a model wrote it, it is a sentence the scholar never wrote, however faithful it happens to be, and the reader has no way to tell.

Take a real result. We ran the Worker’s own keyword search for الصبر عند المصيبة over the 25 sample tafsirs on our machines, filtered to Ibn Kathir at 2:156. Among the results is passage 1503000934: his commentary on 2:156–157, volume 2, page 22. It reads, in part:

أي: تسلَّوا بقولهم هذا عما أصابهم، وعلموا أنهم مُلك لله يتصرف في عبيده بما يشاء، وعلموا أنه لا يضيع لديه مثقال ذرة يوم القيامة

That is Ibn Kathir’s sentence, word for word as the library stores it. We have left it untranslated here on purpose: an English rendering would be our sentence, not his. A system that answers would offer a fluent paragraph that opens “Ibn Kathir explains that…”. The paragraph might be close. It would still not be his.

So every passage carries the means to check it. Its citation is an Arabic reference built from turath.io’s own records. Its source links to turath’s reader at that page and, when there is one, to the scan of the printed page. GET /v1/passages/:id/source returns it with the rest of the page it came from. A citation is only worth something if the words above it are the words on that page.

What the rule rules out

  • A written answer, a summary, or a paragraph that speaks for “the scholars”.
  • Machine translation of the scholars’ Arabic. Once a model is configured, a query with no Arabic letter may be turned by one model call into up to three Arabic keyword queries. Those terms are searched, and never returned, logged or shown. The person’s query becomes search terms; the scholars’ words are not translated at all.
  • Any change to the words of a text. The Qur’an’s text is the Tanzil Project’s, used verbatim, as Tanzil’s terms require.

It does not rule out models that help find passages. An embedding model (bge-m3 in the current configuration) places passages by meaning, and a reranker scores them against the query. Neither writes a word the person reads.

Agentic search inside the rule

Agentic search was the hard case. Ours is ported from Agentset (MIT, by Ahmed Riad), which has two loops. The newer one hands a model search and expand tools, lets it run up to 20 steps, and streams a written answer with citations. Bayan shows no written answer, so we did not take it. The older loop only retrieves. That is the one we ported, as guided search, in src/search/guided.ts.

In it the model does two things. It writes search queries: at most five a round, each a keyword or semantic query of 1 to 200 characters, as { queries: [{ type, query }] }. And it answers one question about the passages found, whether they can answer what was asked, as { canAnswer }, true or false. Both answers are asked for under JSON schemas that allow no other keys, and checked again by hand against the same rules. The prompt that asks for queries says:

Everything else is decided by code:

TypeScript
// src/search/guided.tsexport const MAX_EVALS = 3;       // rounds of writing queries and judgingexport const TOKEN_BUDGET = 4096; // tokens, prompts and answers togetherexport const MAX_QUERIES = 5;     // queries the model may write in one roundexport const QUERY_CHARS = 200;   // characters in one queryexport const PER_QUERY = 15;      // passages each search keeps, reranked // src/types.tsexport interface GuidedResponse {query: string;          // the person's words, trimmedqueries: GuidedQuery[]; // search terms, never an answerresults: Passage[];     // the scholars' passagesrounds: number;         // rounds that ran, 1 to 3enough: boolean;        // true only when the judge said so}

The first round always searches the person’s own words as well, so a model that fails still leaves the question searched. The passages found are merged by id and reranked against the person’s own words, not the model’s queries, and the best topK are the answer. The judge reads the best ten (or fewer) of what the person would be shown, each cut to 800 characters with harakat stripped. Its prompt tells it to be strict: “When in doubt, say false.”

An answer that breaks the schema, an error or a timeout is a failed round, and a failed round ends the loop. The error for an answer in the wrong shape names the model and says it “answered in an unexpected shape”. It never quotes what the model wrote.

The queries are search terms

The model’s queries come back beside the passages, in queries, so an app can say what was searched for. The type says what they are: search terms, never an answer. An app that shows them should label them that way. They are still text a model wrote. A query could misremember Qur’anic wording, or carry whatever a prompt injection coaxed into 200 characters. To make that harder, the person’s words reach the model inside <question> tags with < and > taken out, and both prompts say to treat the question as quoted text, never as instructions. The judge’s prompt says the same of the passages it reads.

The logs hold no queries

A guided search logs counts, timings and kinds of failure: rounds, queries (a count), searches, tokens, enough, stopped, and degraded codes such as generate:malformed. It never logs the person’s words, the queries the model wrote, or anything else a model wrote. A test checks every console line a guided search prints.

Where it stands

Guided search is built, and tested against a scripted fake model. It is off: GUIDED_MODEL is empty, and POST /v1/search/guided answers 404 until it names a model. No language model has been called yet. Before we turn it on, we still have to check the Arabic quality of the queries each candidate model writes, and of its verdicts. The API at grep.getbayan.app is not deployed yet either.

A model can help find the page. It does not get to write on it. When Bayan Library shows a passage under a scholar’s name, the words are the scholar’s own, and the citation says where to read them.