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) | |
|---|---|---|
| Output | Ranked list of chunks + scores | One context block for the prompt |
| How much to return | top_k — a fixed count, whatever their size | A token budget (target_tokens, max_tokens) |
| Sources | Chunk IDs you map back yourself | Every passage inside its source document, with page references where the source has pages |
| Order | By score — related passages scattered | Grouped per document, related passages kept together |
| Gaps between passages | Invisible | Marked, so the model knows text was skipped |
| Your work before the LLM call | Deduplicate, order, trim, cite | None |
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
- 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.
- Hybrid search. Dense (semantic) and BM25 (keyword) scores are fused into one ranking; on the
advancedretrieval tier a reranker re-scores the candidates. - Token-budgeted context construction. Passages are admitted best-first up to the
target_tokensbudget; for tight clusters spanning several documents the context may grow towardmax_tokens, never beyond it. Within each document, passages are ordered so related text stays together (sandwich ordering). Notop_kto guess. - 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 reportstokens_estimatedandresults_dropped.
The result is the RetrievalContext: a block your model can read, quote and cite.
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
- Your own application — call the REST API or the Python SDK and put the context into your prompt.
- Agents — give the agent the MCP server as a tool; it searches your documents and gets prompt-ready context back.
- AI assistants — connect Claude or another assistant: Use index4.ai in your AI assistant.
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
- Managed RAG API — what index4.ai runs for you, end to end
- RetrievalContext format — the exact grammar of the context block
- Retrieval —
min_relevance,target_tokensandmax_tokensin detail