Skip to content

Context API: prompt-ready context for LLMs and agents ​

A retrieval API answers "which chunks match?". A context API answers "what should the model read?" — and returns exactly that: LLM-ready context, assembled from your documents, fitted to a token budget, with every passage tied to its source.

index4.ai is a context engine as a service. Think of it as a document context API for agents and applications: it ingests your documents once, and every query to POST /search returns prompt-ready context you can pass straight to your own LLM or agent.

Retrieval API vs. context API ​

Retrieval API (top-k chunks)Context API (index4.ai)
OutputRanked list of chunks + scoresOne context block for the prompt
How much to returntop_k — a fixed count, whatever their sizeA token budget (target_tokens, max_tokens)
SourcesChunk IDs you map back yourselfEvery passage inside its source document, with page references where the source has pages
OrderBy score — related passages scatteredGrouped per document, related passages kept together
Gaps between passagesInvisibleMarked, so the model knows text was skipped
Your work before the LLM callDeduplicate, order, trim, citeNone

Why top-k chunks aren't enough ​

Top-k retrieval gives you a number of chunks, not a context. Ten chunks can be 800 tokens or 12,000; they arrive sorted by score, so a definition and the clause that depends on it end up far apart; and the model can't tell which document a sentence came from unless you rebuild that yourself. That context assembly — building, ordering, trimming and labelling — is where most RAG pipelines quietly lose quality.

How index4.ai builds the context ​

  1. Structure-aware chunking at ingestion. Documents are split along their real structure — headings, sections, lists, tables — so a passage keeps the hierarchy it sits in (PrimeCut). This is what makes the retrieval document-aware rather than window-based.
  2. Hybrid search. Dense (semantic) and BM25 (keyword) scores are fused into one ranking; on the advanced retrieval tier a reranker re-scores the candidates.
  3. Token-budgeted context construction. Passages are admitted best-first up to the target_tokens budget; for tight clusters spanning several documents the context may grow toward max_tokens, never beyond it. Within each document, passages are ordered so related text stays together (sandwich ordering). No top_k to guess.
  4. Source-aware assembly. Passages are wrapped per document in <doc> blocks, with page references where the source has pages, related passages stay together, and skipped text is marked. The response reports tokens_estimated and results_dropped.

The result is the RetrievalContext: a block your model can read, quote and cite.

json
POST https://api.index4.ai/v1/search
{
  "query": "What is the notice period?",
  "format": "prompt_ready",
  "target_tokens": 3000
}

Prefer raw hits for your own ranking logic? "format": "json" returns structured results instead.

Passages with sources — for answers you can check ​

Because every passage arrives inside its source document — with page references for paged formats such as PDFs — the model can cite where an answer came from, and a reader can verify it. That is the difference between context and a pile of text: the evidence travels with the answer.

Where the context API fits ​

Common questions ​

What is a context API? A context API takes a question and returns the context an LLM should read to answer it: the relevant passages from your documents, deduplicated, ordered, fitted to a token budget and labelled with their sources. A retrieval API returns a ranked list of chunks and leaves that assembly to you.

What is prompt-ready context? Context that can go into a prompt as-is: passages grouped per source document, in an order that keeps related text together, with page references, trimmed to a token budget. index4.ai returns it as an XML + Markdown block from POST /search with format prompt_ready.

What is a context engine? A context engine runs the whole path from documents to LLM-ready context: ingestion, structure-aware chunking, hybrid retrieval, optional reranking and context assembly. index4.ai is a managed context engine, offered as a service over REST, a Python SDK and MCP.

Next ​