Self-Hosted Agent Search API Built on PostgreSQL

Self-hosted agent search API architecture with PostgreSQL pgvector and Gemini models

A self-hosted agent search API gives AI agents a clean way to query a website, and dejan.ai built one on its own stack instead of using Cloudflare AI Search. The new /.well-known/search endpoint indexed 2,111 live content units into 5,718 chunks in 3 minutes 20 seconds, with zero failures.

The same index now also powers the site’s regular /search page, so human visitors and agents read from one source.

How the pipeline works

Every published post, concept, page and Codebase Explorer element is converted to Markdown. A Gemini model then splits it into chunks, a second Gemini model embeds each chunk, and PostgreSQL stores everything in a single table named search_chunks.

That table holds a generated tsvector with a GIN index for keyword matching, plus a pgvector HNSW index for semantic matching. At query time the system pulls the top 80 keyword hits and the top 80 nearest neighbors by cosine distance, then merges the rankings with Reciprocal Rank Fusion at a constant of 60.

How we built Search for Agents on dejan.ai without Cloudflare
Image: DEJAN

Chunking as a yes-or-no question

Fixed-size chunking, such as Cloudflare’s default of 1,024 tokens with 10% overlap, cuts through ideas and tables at arbitrary points. Asking a generative model to rewrite a document with chunk markers fixes boundaries but costs output-token latency on every word and risks silently dropped or altered text.

The team’s approach uses gemini-decision-1, which takes up to 262,000 input tokens and answers up to 1,000 independent yes-or-no questions over that shared input in one pass. It returns calibrated probabilities and no output tokens. Since the model never regenerates text, the chunks are verbatim by construction.

Each document is split into atomic blocks: headings, paragraphs, lists, tables, code blocks and blockquotes. The model then scores each adjacent pair of blocks.

  • A 3,150-word post with 75 blocks had all 74 transitions scored in 329 ms, using 15,657 input tokens.
  • A 759-word post with no HTML headings had its 34 transitions scored in 209 ms, using 5,635 input tokens.
End-to-end architecture diagram of self-hosted /.well-known/search on dejan.ai using gemini-decision-1, gemini-embedding-2, and PostgreSQL pgvector
Image: DEJAN

The fix for orphan headings

The first test asked whether each block started a new chunk. That backfired: both a heading and the paragraph below it look like new starts, and the model scored both above 0.50 in five sections of the long test post. The result was chunks consisting of a five-word heading and nothing else.

Reframing the question around the transition fixed it at no extra latency. The model is asked whether the previous block completes a self-contained unit so that the next block begins a new one, with a “no” for incomplete openers such as headings.

The highest post-heading cut probability fell to 0.251. Orphan headings dropped from 5 to 0, and the chunk count fell from 20 to 12, with a median of 190 words. On the heading-free post, the same prompt produced 10 clean chunks of 27 to 222 words. Two guardrails remain: chunks under 30 words that don’t start with a heading merge into the previous one, and chunks over 900 words split on block boundaries.

Comparison of block-start framing versus end-of-unit start-of-new transition framing in gemini-decision-1 on post #339
Image: DEJAN

Context for each embedding

A passage from the middle of a long post can lose its subject when embedded alone. Before calling gemini-embedding-2 at 768 Matryoshka dimensions, the system prepends the document title and section heading. The clean Markdown is stored separately for display and retrieval.

What agents and humans get

The self-hosted agent search API serves both audiences from one query. The agent endpoint accepts a GET request with a query, and also the HTTP QUERY method with a plain-text or JSON body. It returns up to 10 ranked Markdown passages with a canonical URL, title, section and normalized score.

Safeguards include a 30-minute in-memory cache holding 500 queries, rate limits of 60 requests a minute and 600 an hour per IP, and a noindex header so query URLs stay out of search engines. The human page groups passages by parent document, shows the best chunk per URL, highlights query terms and offers filter tabs for articles, concepts, pages, tools and elements.

Keeping the index fresh and discoverable

A background thread re-indexes content whenever something is published, edited, hidden, shown or deleted, through the CMS or over MCP. Each unit stores a hash of its title, summary and Markdown, so an unchanged save skips both model calls.

For discovery, the site added four agent meta tags to its page head: instructions, endpoint, method and documentation. Its Markdown responses carry the same four fields in their front matter.

What other site owners can take from it

The design shows that a self-hosted agent search API needs only one database table, two model calls and a hybrid ranking step. Teams already running PostgreSQL can copy the pattern without adding a managed search service.

The open question is how widely agents will look for a /.well-known/search endpoint. Adoption by agent builders will decide whether the meta tags become a convention or stay a niche courtesy.

Source: DEJAN

Written by

Saksham Bhargava

Saksham Bhargava runs Alteroid and has been writing about consumer technology for over five years, covering iPhone, Android, Windows, AI tools and gaming. With an MCA and six years of hands-on web development experience, he tests the features he writes about on his own devices and focuses on clear, practical guides that solve real problems. Away from the screen, he enjoys travelling to places most tourists skip.