stackbone.rag
Parse, chunk, embed, store and retrieve documents on top of the agent's own Postgres. The RAG schema lives in the same database as
stackbone.databaseand shares the same connection pool, so touching both surfaces still opens one pool. You reach it through the ambientstackboneclient, the same handle on every surface, from an agent tool or a durable workflow step.
import { stackbone } from '@stackbone/sdk';
const result = await stackbone.rag.retrieve({
text: 'how do I reset my password?',
topK: 5,
namespace: 'help-center', // the collection to search
});
if (result.error) throw new Error(`${result.error.code}: ${result.error.message}`);
console.log(result.data);stackbone.rag runs the same across local development, self-host and
cloud, because it sits on the agent's own Postgres: its only dependency is
the STACKBONE_POSTGRES_URL the runtime injects. There is no separate
hosted RAG service to provision.
Mental model
stackbone.rag stores its data in these canonical tables:
rag_collections, rag_documents, rag_chunks and rag_jobs, in the
managed stackbone_platform schema that Stackbone provisions in your
agent's Postgres for you. Stackbone also keeps rebuild progress in this schema.
- The tables live in the agent's own Postgres
(
STACKBONE_POSTGRES_URL), in thestackbone_platformschema, next to your own tables. There is no separate "RAG store". stackbone.ragreuses the pool thatstackbone.databasebuilds, so an agent that touches both surfaces opens exactly one connection pool.- Stackbone provisions the tables on every install. You use
stackbone.raginstead of installing its tables yourself.
The collection owns its embedding provider, model and dimension. Ingestion and retrieval read that saved configuration before they embed text. Collections with 1536 and 768 dimensions can share the same database without mixing their results.
Configure a collection
Update your box before you create configured collections in Studio. Choose an embedding provider and model in the collection dialog. If the provider catalogue does not identify embedding models, enter the model id manually. Studio asks the box to probe the selected model. A nonempty vector of finite numbers determines the saved dimension. A failed probe leaves no configured collection on an updated box. A keyless provider is valid.
The collection details show the saved provider, model, dimension and search mode. Dimensions from 1 to 2000 use HNSW when the dimension index is available. Larger vectors, up to 16000 dimensions, use exact search. The reported search mode tells you which path is available.
The dialog also has a Hybrid search toggle, off by default. Turn it on for a collection that your agent searches by product code, reference or name. See Hybrid search.
You can also create a collection with your first stackbone.rag.ingest or
ingestAsync call. Its explicit model, or the SDK's configured fallback,
initializes the collection. A bare model id initializes provider default. A
provider pin such as local::nomic-embed-text initializes provider local when
the SDK sends embeddings through the box proxy. The first chunk's embedding
probes the dimension and is reused when the SDK stores that chunk.
After creation, omit model to use the saved configuration. A matching bare
model id keeps the collection's saved provider. A provider pin must match both
provider and model. A conflicting override returns rag_embedding_conflict
before the SDK embeds or writes anything. Changing the workspace fallback does
not change an existing collection.
An explicit createClient({ modelProviderBaseUrl, modelProviderKey }) keeps its
direct provider target. That target cannot route a collection assigned to a named
box provider. Use the box proxy for that collection, or use a collection assigned
to default whose model your direct endpoint serves.
Historical collections keep provider default, model
openai/text-embedding-3-small and dimension 1536. The legacy create request with
only a name and metadata still works.
Change a collection's model
Update the box and CLI before you use this feature. In Studio, open RAG, choose the collection and click Change model. Select its target provider and embedding model, or enter a model ID. Read the warning, then start the rebuild. The box probes the target and checks the source of every stored version before it accepts the change. A failed check leaves the collection unchanged.
Every stored version takes part, including unpublished and older versions. The box uses each version's saved original. When the original is missing, it uses stored chunk text instead. If neither source exists, the error names the affected versions. A storage outage or an unreadable original remains an error; it does not silently switch to chunk text.
While rebuilding, Studio shows the current and target models and the number of completed versions. Queries, uploads, publication and content deletion pause. You can still open history and download originals. A failure keeps the collection paused and retains the completed count. Fix the provider or runtime problem, then click Retry rebuild to resume the same operation. A worker restart or a lost launch receipt does not require new document versions.
When all versions finish, the collection uses the target provider, model and detected dimension. Document keys, version numbers, original links, metadata and publication flags stay intact. Try and Publish also work on older rebuilt versions. Publishing one selects that version without embedding it again.
Update the code that runs your RAG calls
The calls remain stackbone.rag.ingest, ingestAsync and retrieve.
Ingestion names a collection; retrieval names a namespace.
| Where your code runs | What must change |
|---|---|
| A packaged deep agent with the SDK external | A new runtime image can supply the corrected SDK while preserving your bundle and declared SDK dependency. Confirm which SDK bytes the image contains. |
| A compiled workflow containing the old RAG implementation | Update its installed SDK, compile the workflow again, and ship the new artifacts. Replacing the runtime package alone does not replace inlined code. |
| Local development | Update the SDK installed in your project and compile again. Check workflows/node_modules too if your workflows install their own copy. |
| Studio or the CLI | Update the box first, then use a compatible Studio and CLI. An older CLI may reject the new exact search mode. |
A creator workflow step can also keep its SDK import external. If the emitted
step does so, it loads the installed SDK. Check your artifacts before assuming
they contain the old implementation. To compile new TypeScript calls that omit
model, update your local SDK declarations too; an image update only changes
runtime bytes. An old managed ingest workflow can keep its
own fixed model, which may now conflict with the collection. Update the CLI and
regenerate that managed workflow as part of the update. The updated CLI also
provides the managed collection-rebuild workflow. Compile and ship it with the
updated box before changing existing collections.
These collection settings require coordinated schema and SDK changes. A package version alone cannot identify an unreleased build. Use a released SDK and runtime that explicitly support collection-owned embeddings once available. Upgrade instructions do not authorize skipping the SDK major release required for a breaking schema or index change.
An older box may accept a create request while ignoring its embedding fields. Current Studio checks the returned provider and model, but that check cannot undo a legacy row the older box already created. Update the box before saving. Old inlined SDK implementations and direct SQL do not acquire collection-state checks automatically.
Where RAG calls live
stackbone.rag is the ambient handle: you import it once and call it from
wherever your code runs. Two places matter for an agent:
- From an agent tool: retrieve inside a tool call and return the hits, so the model can use them to answer. A deep agent grounds its replies in your documents this way. See LangChain's tool docs.
- From a durable workflow step: ingest is
I/O that can be slow and can fail, so put it inside a
'use step'. The Workflow SDK runs each step once, persists its result and auto-retries on failure, which is what a parse → chunk → embed → store pipeline needs.
The pure helpers parse and chunk touch nothing external, so you can
also call them from a one-off script or a test.
Ingestion
Pure helpers: parse and chunk
stackbone.rag.parse(...) and stackbone.rag.chunk(...) are pure helpers.
They never touch the database and never call the embedding provider, so you
can use them anywhere (in a tool, a step, a script or a test) to iterate
on chunking before committing to ingestion:
import { stackbone } from '@stackbone/sdk';
// `parse` accepts the document bytes (or a plain string / Blob), not a URL:
// fetch the source yourself first.
const bytes = await fetch('https://example.com/post').then((r) => r.arrayBuffer());
const text = await stackbone.rag.parse(bytes);
const chunks = stackbone.rag.chunk(text, { size: 512, overlap: 64 });parse accepts a string, a Blob, a Uint8Array or an ArrayBuffer.
Markdown and plain text come back verbatim; parse flattens a PDF to one
string. The reader is text-only (no OCR): a scanned, image-only PDF has no
text layer, so parse rejects it with a clear error rather than ingesting
an empty document.
Synchronous ingest: stackbone.rag.ingest
For one-off ingestion (a setup script, a small upload handler) call
ingest directly. The promise resolves once the SDK has embedded and
persisted every chunk:
import { stackbone } from '@stackbone/sdk';
const bytes = await fetch('https://example.com/post').then((r) => r.arrayBuffer());
const text = await stackbone.rag.parse(bytes);
const result = await stackbone.rag.ingest({
id: 'post-123',
collection: 'help-center',
chunks: stackbone.rag.chunk(text),
model: 'openai/text-embedding-3-small',
metadata: { source: 'help-center', url: 'https://example.com/post' },
});
if (result.error) {
// SdkError: see "Errors" below.
throw new Error(`${result.error.code}: ${result.error.message}`);
}
console.log(`Ingested ${result.data.chunks} chunks for ${result.data.id}.`);The SDK embeds string chunks with the saved collection configuration. Pass chunks as
{ content, embedding } objects instead when you embedded them yourself: that
shape takes no model. Re-ingesting with the same id replaces all chunks for
that document atomically, useful for periodic re-crawls.
You can pass onProgress to observe per-chunk progress without paying for
the SQL job writer:
await stackbone.rag.ingest({
id: 'post-123',
collection: 'help-center',
chunks,
onProgress: (event) => console.log(event.type, event),
});Ingest inside a durable workflow step
A real ingest is slow and can fail mid-flight. Put it in a workflow
'use step'
so it runs once, persists its result and auto-retries on transient
failures:
import { z } from 'zod';
import { stackbone } from '@stackbone/sdk';
export const inputSchema = z.object({ url: z.string().url(), id: z.string() });
export const outputSchema = z.object({ chunks: z.number() });
export async function indexDocsWorkflow(input: z.infer<typeof inputSchema>) {
'use workflow';
return ingestStep(input.url, input.id);
}
async function ingestStep(url: string, id: string) {
'use step'; // runs once, persisted, retried on failure. Keep it idempotent
const bytes = await fetch(url).then((r) => r.arrayBuffer());
const text = await stackbone.rag.parse(bytes);
const result = await stackbone.rag.ingest({
id,
collection: 'help-center',
chunks: stackbone.rag.chunk(text),
metadata: { source: 'help-center', url },
});
if (result.error) throw new Error(result.error.code);
return { chunks: result.data.chunks };
}Durable ingest in one call: ingestDocuments
The step above wires the parse → chunk → embed → store pipeline by hand. To
ingest a document durably from a workflow without customising the pipeline,
call ingestDocuments from @stackbone/sdk/workflow.
It runs the built-in ingest workflow as a durable sub-workflow and resolves once
the document is stored, returning { documentId, chunks }:
import { ingestDocuments } from '@stackbone/sdk/workflow';
export async function indexDocsWorkflow(input: { markdown: string }) {
'use workflow';
// Hand it the text: the helper stages it to storage and ingests it durably.
const { documentId, chunks } = await ingestDocuments({
collection: 'docs',
content: input.markdown,
contentType: 'text/markdown',
});
return { documentId, chunks };
}It takes one of two input shapes:
{ content }: inline text or bytes. The helper stages it to the agent's storage for you (in a durable, replay-safe step keyed by the content hash) and then ingests it.{ storageKey }: a file already staged under the reservedrag/prefix (for example one an operator uploaded from Studio). The helper ingests it directly, so a large file never travels in the workflow input.
Both must be called from a workflow body. If your workspace ships its own
version of the ingest workflow, ingestDocuments uses that one instead. For a
custom pipeline, call stackbone.rag.ingest inside your own step, as above.
Asynchronous ingest: stackbone.rag.ingestAsync
For a long-running ingest you want to observe outside a workflow (a webhook
handler that must return in seconds, a backfill that takes minutes) call
ingestAsync. It allocates a rag_jobs row synchronously, returns the job
id immediately, and exposes both a streaming channel of progress events and
the final Result:
import { stackbone } from '@stackbone/sdk';
const handle = await stackbone.rag.ingestAsync({
id: 'post-123',
collection: 'help-center',
chunks,
});
if (handle.error) throw new Error(handle.error.code);
// You now have a stable job id. Hand it back to the caller and return early.
const { jobId } = handle.data;
// Drain progress events into your own logs / stream when you want to follow it.
for await (const event of handle.data.events) {
switch (event.type) {
case 'started':
console.log('rag.ingest.started', event);
break;
case 'progress':
console.log('rag.ingest.progress', event);
break;
case 'completed':
console.log('rag.ingest.completed', event);
break;
case 'failed':
console.error('rag.ingest.failed', event);
break;
}
}The handle is the observability surface for a job your own code starts:
events streams the progress, and handle.data.result settles with the
final Result. The Jobs list in Studio and stackbone rag jobs report the
durable ingest runs an upload starts. They do not read the rag_jobs rows
ingestAsync writes, so follow your own job through the handle.
The worker re-reads its job row between chunk batches. Mark that row
cancelled and the ingest stops with rag_ingest_cancelled.
Retrieval
stackbone.rag.retrieve is the only read API. Pass a text query to let
the SDK embed it for you, and namespace to name the collection to search.
A query is scoped to one collection. Without namespace it searches the
collection called default, so a document ingested into help-center is only
found when the query names it. The most common place to call it is inside an
agent tool, returning the hits so the model can ground its answer:
import { tool } from '@langchain/core/tools';
import { stackbone, z } from '@stackbone/sdk';
const searchDocs = tool(
async ({ query }: { query: string }) => {
const result = await stackbone.rag.retrieve({
text: query,
topK: 5,
namespace: 'help-center',
});
if (result.error) throw new Error(result.error.code);
// Return the hits to the model so it can answer from them.
return result.data.map((hit) => ({
score: hit.score,
content: hit.content,
}));
},
{
name: 'search_docs',
description: 'Search the help-center knowledge base before answering a question.',
schema: z.object({ query: z.string() }),
},
);topK defaults to 5. Each hit is { id, chunkIdx, content?, metadata?, score, mixedScore?, rerankScore? }, ordered by descending cosine similarity
(score is in [0, 1], 1 = identical). mixedScore and a different order
appear on a hybrid search. rerankScore and a different order
appear when you rerank the hits. When you
split a long text into searches, the hits
come in turns, one from each search.
Filtering on metadata
Pass filter to scope the search to documents whose metadata matches a
JSON sub-object (matched server-side via jsonb @> $1):
const result = await stackbone.rag.retrieve({
text: 'pricing tiers',
namespace: 'help-center',
filter: { source: 'help-center', locale: 'en' },
topK: 10,
});
if (result.error) throw new Error(result.error.message);
console.log(result.data);filter keeps its containment meaning, including nested metadata objects.
Use ranges for number or date comparisons.
Filter by number or date
Supply values in document metadata when you ingest. You choose the names:
total and fecha below could instead be precio, stock or due_date.
Store one invoice or product per document. Every chunk inherits that document's
values, so split a file containing several entities before you ingest it.
import { stackbone } from '@stackbone/sdk';
const ingested = await stackbone.rag.ingest({
id: 'invoice-42',
collection: 'invoices',
chunks: ['Hosting invoice for October'],
metadata: { total: 1250, fecha: '2026-10-31', team: 'support' },
});
if (ingested.error) throw new Error(ingested.error.message);
const result = await stackbone.rag.retrieve({
namespace: 'invoices',
text: 'hosting expenses',
filter: { team: 'support' },
ranges: [
{ field: 'total', type: 'number', gt: 500 },
{ field: 'fecha', type: 'date', gte: '2026-10-01', lte: '2026-10-31' },
],
topK: 10,
});
if (result.error) throw new Error(result.error.message);
for (const hit of result.data) console.log(hit.id, hit.score, hit.metadata);Every range and filter must match. They restrict eligible chunks before the
topK cut and reranking. The date range above includes 31 October and excludes
1 November. The score remains cosine similarity.
| Bound | Meaning |
|---|---|
gt |
Greater than |
gte |
Greater than or equal to |
lt |
Less than |
lte |
Less than or equal to |
Only JSON numbers match type: 'number'; the string "1250" does not.
Dates must be real Gregorian YYYY-MM-DD strings in years 0001–9999.
A missing field, wrong JSON type, malformed date or impossible date such as
2026-02-31 never matches its range and cannot break the search.
Each range needs at least one bound. Do not combine gt with gte, or lt
with lte. The lower bound must not exceed the upper bound; equal exclusive
bounds produce no matches. Numeric bounds must be finite. field must be a
nonempty literal top-level key without dots. Unknown properties or types are
rejected. You can send at most ten ranges; omitted or empty ranges add no
restriction. Invalid ranges return rag_invalid_request, or HTTP 400 with that
code through the box query API.
For agent tools, import ragFilterSchema, ragRangeSchema, ragRangesSchema
and the RagRange type from @stackbone/sdk to use the same validation.
Map amount or date constraints to the field names the uploader supplied.
Retrieval returns the best matching chunks, limited by topK. Use a structured
query when you need every matching document or a total.
Precomputed embeddings
If you already have an embedding (e.g. from your own embedder or a cached vector), pass it directly:
await stackbone.rag.retrieve({
embedding: cachedQueryVector, // number[]
topK: 5,
});The pipeline skips embedding and checks the vector dimension against the saved collection. Supply vectors from that collection's model. Equal dimensions do not prove that two vectors use the same model.
Hybrid search
A search by meaning finds text that says the same thing in other words. It can
miss an exact string: a product code, a reference, a surname. Hybrid search
adds a second search for the exact terms of your query and merges the two
lists. Use it for a catalogue, a price list or a set of invoices, where your
agent asks for 1239asd/OK or Pérez.
Hybrid search is a collection setting, off by default. Turn it on when you
create the collection: the Hybrid search toggle in Studio's collection
dialog, or
stackbone rag collections create <name> --hybrid.
A collection that your first stackbone.rag.ingest call creates has it off.
With the toggle on, the dialog also asks for a number and date format, eu or
us, which decides how prices and dates match. You can
change both later.
On a hybrid collection, retrieve with a text query runs both searches. The
call does not change:
const result = await stackbone.rag.retrieve({
text: '1239asd/OK',
namespace: 'catalogue',
topK: 5,
});
if (result.error) throw new Error(`${result.error.code}: ${result.error.message}`);
for (const hit of result.data) {
console.log(hit.mixedScore, hit.score, hit.content);
}The search for exact terms follows these rules:
| Rule | Example |
|---|---|
| Case and accents do not matter. | perez finds Pérez. CAMIÓN finds camion. |
| Common Spanish and English words are ignored. | de, la and the never decide a match. |
| A query of up to 3 words matches a chunk only when the chunk holds all of them. | cable SCK finds a chunk with both words, not a chunk with only one. |
| A query of 4 words or more matches a chunk that holds at least 2 of them. The more of them a chunk holds, the higher it ranks. | necesito el cable rojo SCK para mi pedido finds a chunk with cable and SCK, not a chunk with only pedido. |
| In a query of 4 words or more, the euro does not count toward the 2. | A chunk that shares only an amount and € with the query needs one more word of it. |
| In a query of 4 words or more, greetings and sign-offs do not count toward the 2. | A chunk that shares only muchas gracias, un saludo with an email is not lifted. The query noches still finds 3 noches. |
A code with / or - matches whole, joined or by its parts. |
1239asd/OK, 1239ASD/ok, 1239asd OK, 1239-asd and 1239asd find 1239asd/OK. |
| A part made only of digits does not match alone. | 1239 does not find 1239asd/OK. 11 does not find 03/11/2026. |
| Words match whole: no prefixes, no plural forms, no typo tolerance. | camisetas does not find camiseta by its exact terms. The search by meaning can. |
The greetings and sign-offs are hola, buenas, buenos, días, tardes,
noches, gracias, saludo, saludos, atentamente, cordialmente,
estimado, estimada, hi, hello, thanks, thank, regards and
dear.
When no chunk holds enough words of the query, the search for exact terms
finds nothing, and retrieve returns the same hits as a search by meaning
alone.
scorekeeps its meaning: the cosine similarity. A hybrid list is not ordered by it, and a hit found by its exact terms can have a lowscore. Do not filter hybrid hits onscorealone.mixedScoreis the merged score the list is ordered by. Higher is better. It is absent when the search ran by meaning only.rerankreads the merged list, not the cosine one.- A precomputed
embeddinghas no words, so it always searches by meaning only.
Pass hybrid: false to search by meaning only for one call, also on a hybrid
collection. A descriptive question reads better that way:
const result = await stackbone.rag.retrieve({
text: 'something to plug several devices into',
namespace: 'catalogue',
hybrid: false,
});hybrid must be a boolean. A string such as 'false', which a model can write
in a tool call, returns rag_invalid_request.
Prices and dates
The search for exact terms reads a price or a date by the collection's number
format, so a query finds the same value written another way. eu is the
default:
| Query | Finds on an eu collection |
Finds on a us collection |
|---|---|---|
1250 euros, 1250 €, €1250 |
1.250,00 €, 1250 EUR |
1,250.00 €, 1250 EUR |
3 de noviembre de 2026, November 3, 2026 |
03/11/2026 |
11/03/2026 |
3 de noviembre, November 3 |
03/11/2026, 03/11/2025 |
11/03/2026, 11/03/2025 |
03/11/2026 |
3 de noviembre de 2026 |
11 de marzo de 2026 |
3 de noviembre |
2026-11-03, 03-11-2026 |
2026-11-03, 11-03-2026 |
The rule follows these limits:
- A numeric date needs
/or-and a four-digit year:03/11/2026,03-11-2026and the ISO form2026-11-03are dates, in a query too.03/11/26and03.11.2026match only as they are written. A date that does not exist, such as31-02-2026, is not read as a date, and neither is a phone number such as600-11-03. - A date with a Spanish or English month name reads the same in both formats.
The Spanish year may follow with or without
de:3 de noviembre 2026. - A query without a year finds that day in any year.
- An amount with cents finds only that amount:
1250 eurosdoes not find1.250,50 €. - A query that names the euro (
€,eur,euro,euros) finds only an amount that names it too. A query with only the number (1250) finds both. - A price inside a code or with a unit is read too:
1250 €finds1.250 €/mes, and1.500 €finds1.250-1.500 €. - Only the euro is read as a currency.
Chunks keep their exact terms from the moment they were indexed. A hybrid collection indexed before this release reads prices and dates only after you save its search settings again or change its model.
Write short queries
A short query is the most precise. Up to 3 words, every word must be in the
chunk. A longer query matches any chunk that holds 2 of its words, so its exact
hits are looser, and a text that asks several things blurs into one search by
meaning. Give the search a few key words, and copy a code as it is written
(1239asd/OK) instead of describing it. When an agent tool wraps retrieve,
say this in the tool description, so the model writes its queries the same
way:
import { tool } from '@langchain/core/tools';
import { stackbone, z } from '@stackbone/sdk';
const searchCatalogue = tool(
async ({ query, hybrid }: { query: string; hybrid?: boolean }) => {
const result = await stackbone.rag.retrieve({ text: query, namespace: 'catalogue', hybrid });
if (result.error) throw new Error(result.error.code);
return result.data.map((hit) => hit.content);
},
{
name: 'search_catalogue',
description:
'Search the product catalogue. Keep the query short: a few key words, not a sentence. ' +
'Copy product codes, references and names exactly as the user wrote them.',
schema: z.object({
query: z.string(),
hybrid: z.boolean().optional().describe('false to search by meaning only'),
}),
},
);When your code must search with a text nobody shortened, such as a customer
email, let retrieve
split it into searches.
Chunks from an older SDK
The exact terms of a chunk are written when the chunk is ingested into a hybrid collection. Chunks that an SDK release without hybrid search ingested carry no exact terms, so hybrid search finds them by meaning only. Ingest into a hybrid collection with an SDK release that supports it. To give the chunks already stored their exact terms, save the collection's search settings again (see the next section). An older box ignores the Hybrid search toggle, and Studio then says that the box did not confirm the selected settings. Update the box first.
Change hybrid search on an existing collection
You can turn hybrid search on or off, or change the number format, on a
collection that already holds documents. In Studio, open the collection and
choose Edit search settings. From the CLI, run
stackbone rag collections update.
The SDK has no call for it.
The box recomputes the exact terms of every chunk from the text it already
stores, with the new number format: on a us collection, 03/11/2026 is
March 11. It calls no embedding model. While it runs, the collection is paused:
retrieve and ingest on it return rag_collection_unavailable. It usually
takes seconds, and Studio shows the collection as rebuilding until the box
answers. Your agent code needs no change: once the collection is ready again,
the same retrieve call runs the new setting.
If the recompute fails, the collection stays paused. Save the same settings again to retry. A box older than this feature cannot change the settings, and Studio and the CLI both ask you to update the box.
Rerank the hits
Pass rerank to reorder the hits with a rerank model before they reach
your agent. A rerank model reads the question and each fragment together, so
it puts the fragment that answers the question first, where cosine order can
put a fragment that only looks similar. It helps most over a large collection
of similar documents. Rerank is off by default, because each reranked call
adds one model request.
const result = await stackbone.rag.retrieve({
text: query,
namespace: 'help-center',
topK: 5,
rerank: true,
});
if (result.error) throw new Error(result.error.code);
for (const hit of result.data) {
console.log(hit.rerankScore, hit.score, hit.content);
}With no model passed, the rerank model follows your model provider, not
OpenRouter. rerank: true uses voyageai/rerank-3-lite when your model
provider serves it. When the provider does not, it uses the first rerank model
the provider lists, such as cohere/rerank-v3.5. If the provider lists no rerank model, or
fails or does not answer within 5 seconds, it uses voyageai/rerank-3-lite. The
SDK keeps the result for 60 seconds, a failed one too, so each client asks at
most once a minute. A model you pass is always used as is, and the SDK does
not ask the provider.
The call goes through stackbone.ai.rerank,
with the same model provider and key as every other stackbone.ai call, so you
configure nothing new. Pass an object instead of true to change the defaults:
| Option | Default | What it does |
|---|---|---|
model |
voyageai/rerank-3-lite |
The rerank model, as a creator/model-name id. Without it, the SDK picks one as described above. |
candidates |
topK * 4 |
How many vector hits the reranker reads before the cut to topK. Capped at 50, and never fewer than topK. |
minScore |
none | Drops every hit the reranker scored below it. See Return nothing below a threshold. |
The search first fetches candidates hits in cosine order, or in merged order
on a hybrid search. Your filter
and the published-version rule apply to them as usual. The reranker scores
each one, and retrieve returns the best topK in rerank order.
scorekeeps its meaning: the cosine similarity.rerankScoreis the rerank model's score for that hit.includeContent: falsestill works. The reranker reads each fragment's text, and the hits come back withoutcontent.
Return nothing below a threshold
minScore drops the hits the reranker scored below it. A question nothing in
your documents answers then returns zero hits, so your agent can say "I don't
have that" instead of answering from weak fragments. Treat an empty list as
that answer:
// Each rerank model scores on its own scale. Pick the threshold from the
// rerankScore your own questions get with `rerank: true`.
const MIN_RERANK_SCORE = 0.5;
async function searchHelpCenter(query: string) {
const result = await stackbone.rag.retrieve({
text: query,
namespace: 'help-center',
rerank: { minScore: MIN_RERANK_SCORE },
});
if (result.error) throw new Error(result.error.code);
// Zero hits: nothing in the help center answers the question.
if (result.data.length === 0) return 'The help center does not cover this.';
return result.data.map((hit) => hit.content);
}There is no default threshold. Each rerank model scores on its own scale, so
a threshold that works for one model does not carry over to another. Set it
again when you change model.
When the rerank call fails
A failed rerank call never fails retrieve. When the provider is down, the
model is retired or the account has no credit, retrieve still returns the
first topK hits:
- in search order, as without
rerank, - without
rerankScore, so your code can tell, - without the
minScorefilter, because that threshold is on the rerank model's scale. An outage does not make your agent answer "I don't have that" to a question your documents cover.
The SDK logs one console.warn with the model and the provider's message, so
the failure shows in your agent's logs.
Mistakes that fail the call
These are programming mistakes, so retrieve returns rag_invalid_request
before it embeds the question or touches the database:
rerankwith a precomputedembedding. The reranker needs the questiontext.- A
minScorethat is not a finite number. - A
candidatesthat is not a positive integer.
Split a long text into searches
An agent that answers email often has only the whole message to search with: a
greeting, two or three questions, a signature. One search over that text blurs
the questions together, and the second question often finds nothing. Pass
rewrite and retrieve asks a chat model to split the problem into its parts
and write one short keyword search per part, then runs each one:
const result = await stackbone.rag.retrieve({
text: email,
namespace: 'catalogue',
topK: 6,
rewrite: true,
});
if (result.error) throw new Error(`${result.error.code}: ${result.error.message}`);
for (const hit of result.data) {
console.log(hit.score, hit.content);
}Rewrite is off by default, because each rewritten call adds one model request.
Without rewrite, retrieve calls no chat model and runs one search.
With rewrite, retrieve does three things:
- The model reads
text, splits the problem it describes into its parts and writes 1 to 3 keyword queries, one per part. It copies product and model names, error codes, parameters and numbers as the text writes them. It writes each query in the language of your documents, or in the language of the text when it cannot tell. retrieveruns one search per query. Each search follows the same rules as a call withoutrewrite: yourfilter,ranges,hybridand the collection's settings apply to each one.- It merges the hits in turns: the best hit of every query, then the second
of every query, and so on. A chunk that two queries found comes back once.
The list stops at
topK.
So every part keeps its best hit near the top, even when the hits of another
part score higher. Set topK to at least 3, the most queries one rewrite
writes. The merged list is not ordered by score.
Pass an object instead of true to pick the chat model for one call:
| Option | Default | What it does |
|---|---|---|
model |
The Query rewrite model, else the Default model, else openai/gpt-4o-mini |
The chat model that writes the queries, as a creator/model-name id. |
The call goes through stackbone.ai, with the same
model provider and key as every other stackbone.ai call. It shows on the
run's timeline as a model call with its tokens, like any
chat completion, but without
your text or the model's reply.
stackbone rag query and Studio's search dialog do not rewrite: they search
the text as you type it. A voice agent's knowledge search does not rewrite
either.
Pick the rewrite model
For each rewrite, the model follows this order:
- The
modelyou pass inrewrite: { model }. - The model and provider picked on the Query rewrite row of Settings › Model defaults.
- The workspace default model (
defaultModel, the Default model row of the same screen), with its provider. - The platform default,
openai/gpt-4o-mini.
The Default model comes before the platform default on purpose. Splitting a message into its parts works on a strong model and falls short on a small one, and the Default model is usually the one your agents already run on. Each rewrite reads steps 2 and 3 from the workspace settings, so a change in Studio applies to the next call.
Change the rewrite prompt in Studio
Open the Query rewrite row of
Settings › Model defaults
and click Edit in its Prompt section. Saving publishes your prompt, and the
next rewrite uses it, a call that names its own model included. Reset to
built-in goes back to the prompt Stackbone ships.
One line that describes your collection helps the most, for example "The manuals are in Spanish and name each product by its model code." After your prompt, Stackbone always adds the reply format and the limit of 3 queries, so a prompt can ask for fewer queries but never for more.
A blank published prompt, or a deleted one, runs on the built-in prompt. So
does a prompt the SDK cannot read, with one console.warn. The rewrite still
runs.
Rewrite and rerank together
With rerank too, each search reranks its own hits against
its own query before the merge. One rerank over the merged list would score
every hit against the whole email and could push one question's best hit below
topK. Each search makes its own rerank request, so a rewrite into three
queries makes one chat request and three rerank requests. A rerankScore
compares hits of the same query only, and the merged list is not ordered by it.
When the rewrite fails
A failed rewrite never fails retrieve. When the provider is down, the model
is retired, or the reply holds no usable query, retrieve searches with your
original text once, the same as without rewrite. The SDK logs one
console.warn with the model and the code rag_rewrite_failed. It never logs
your text.
When the workspace settings cannot be read, retrieve cannot tell which model
to rewrite with, so it does the same: it searches your original text once and
logs one console.warn with the settings error code, such as
settings_unavailable. A call that names its own model reads no settings,
so it still rewrites.
When the rewrite works but one of its searches fails (for example, the
embedding provider rate-limits one call), retrieve drops that search and
returns the hits of the others, with one console.warn for each search it
dropped. Only when every search fails does retrieve return the error.
rewrite with a precomputed embedding and no text returns
rag_invalid_request before any model call: there is no text to rewrite.
Deletion
Two atomic deletion APIs:
// Delete one or many documents by id (re-ingest replaces; this removes).
await stackbone.rag.delete(['post-123', 'post-124']);
// Delete every chunk whose metadata matches a sub-object.
await stackbone.rag.deleteWhere({ source: 'help-center' });Both run inside a single SQL statement and cascade through to chunks via
the foreign key. Pass { namespace } to scope either call.
RAG calls and your own transactions
RAG and your own tables share one connection pool, but a stackbone.rag
call does not join a stackbone.database.transaction(...) callback. It
checks out its own connection from the pool and commits on its own, even
when you call it inside the callback. Do not rely on a rollback of your
transaction to undo an ingest.
To keep your own rows and the RAG store consistent, run the ingest first. Write your own row only after it succeeds:
import { stackbone } from '@stackbone/sdk';
import { eq } from '@stackbone/sdk/db';
import { documents } from '../src/schema';
async function reindexStep(id: string, parsedText: string) {
'use step';
const result = await stackbone.rag.ingest({
id,
collection: 'help-center',
chunks: stackbone.rag.chunk(parsedText),
});
if (result.error) throw new Error(result.error.code);
// Only mark your own row once the ingest is stored.
await stackbone.database
.update(documents)
.set({ ragIndexedAt: new Date() })
.where(eq(documents.id, id));
}A failed step retries, and re-ingesting the same id replaces its chunks
atomically, so the retry converges instead of duplicating data.
Inspecting RAG data
Three surfaces read the same rows:
stackbone rag: list collections and documents, run a similarity query, and follow ingest jobs from your shell.- Studio's RAG explorer: read-write. A person can create a collection, upload a file and ingest it from the screen. See Managed ingestion from Studio below.
- The agent's own HTTP surface:
GET /api/rag/collections,POST /api/rag/collections/:name/query,GET /api/rag/jobsandPOST /api/rag/jobs/:jobId/cancel. Studio and the CLI both go through it.
Managed ingestion from Studio
stackbone.rag.ingest is the code path: your agent calls it at
runtime. There is also a human path: anyone who installed your agent
(and, locally, you as the creator running stackbone dev) can feed a
collection straight from the Studio screen, with no ingest code to write.
Both paths land in the same rag_* tables, so the agent retrieves
manually-uploaded chunks with stackbone.rag.retrieve() exactly as if your
code had ingested them: there is no second-class "uploaded by hand" flag.
What a person can do from the screen:
- Create an empty collection with a name, an optional description and a probed embedding provider and model, before uploading anything. Turn on hybrid search here, or later from Edit search settings, if your agent searches by code or name.
- Upload a file (
.txt,.mdor.pdf) into a collection. The upload returns immediately with a job id, and the parse, chunk, embed and store work runs in the background, so the browser never freezes on a large PDF. - Watch the job move through
queued → running → succeeded | failedin the Jobs tab. Retry a failed job without re-uploading: the retry starts from the original the upload stored. Cancelling marks the jobcancelled, and it is best effort: an ingest already mid-flight runs to the end. - Upload a new version of a document, try it, and publish it when it is ready. See Version documents from Studio.
- Delete a document with every version of it (their chunks cascade), or delete a whole collection (documents and chunks cascade).
- Download the original file. The platform keeps the uploaded source, linked to its document row.
Two contracts matter for your agent:
- Manual uploads and SDK retrieval use the collection's saved provider, model and dimension. The built-in ingest workflow follows that configuration. If you override the ingest workflow, update its SDK and compile it again so its RAG calls follow the same contract.
- The schema stays with the platform. Stackbone provisions the
rag_*tables on every install, so the screen works from the first boot. Studio never creates or migrates the schema itself; it only reads and writes through it.
The platform keeps files uploaded this way in object storage under a
reserved, read-only rag/ prefix. See
stackbone.storage.
Version documents from Studio
A document you upload from Studio can hold versions. You add one with New version on the document's row, and you decide when your agent starts to read it. Each version keeps its own chunks and its own original file.
| In Studio | What happens |
|---|---|
| First upload of a file | It becomes version 1, published at once. |
| New version on a document | Studio ingests the file you pick, whatever its name, as the next version. It stays unpublished, and the row shows v2 ready to publish once Studio has ingested it. |
| Upload a file with the name of a published document | Studio refuses it (case does not matter, as on macOS) and offers Upload as new version. |
| A new version identical to an earlier one | Studio refuses it with Same as v1 and offers to publish v1. |
| History on a document | Lists every version with its status, and Download, Try, Publish and Delete where they apply. |
| Try on a ready, unpublished version | Studio searches the whole collection as if that version were published. |
| Publish | Your agent reads that version from its next search on. Publish an earlier version to roll back. Publishing re-embeds nothing, because each version has its chunks. |
| Delete on an unpublished version | Removes that version, its chunks and its original file, once its ingest is over. The rest of the document stays. |
| Delete the document | Removes every version, with their chunks and original files. |
What your code sees
stackbone.rag.retrievesearches the published version of each document only. Your agent never gets a hit from an unpublished version.stackbone.rag.ingestnever creates a version. Re-ingest with the sameidand it still replaces that document's chunks in place. The document keeps its version number and its published state.stackbone.rag.deleteremoves the one version whose id you pass, and leaves the other versions of that document. To remove a document with every version, delete it from Studio.
Update the SDK before you add versions
Versions need your workspace on an SDK release that supports them. An older SDK does not know about versions:
- Its
retrievealso returns chunks from unpublished versions. - Its ingest of a new version publishes that version as a separate document, next to the one it was meant to replace.
Update @stackbone/sdk before anyone uploads a new version from Studio.
Update the SDK gives the
command, the second copy under workflows/, and how to restart or redeploy
your agent on the new release.
Custom pgvector columns
If you need a vector column on your own table (e.g. an embedding field on
documents you maintain by hand),
@stackbone/sdk/db re-exports vector:
import { pgTable, text, vector } from '@stackbone/sdk/db';
export const documents = pgTable('documents', {
id: text('id').primaryKey(),
content: text('content').notNull(),
embedding: vector('embedding', { dimensions: 1536 }).notNull(),
});The first migration that introduces a vector(...) column emits a
CREATE EXTENSION IF NOT EXISTS vector; the RAG installer
also ensures the extension exists, so the order in which you add custom and
platform vector columns does not matter.
Errors
Every stackbone.rag.* call returns Result<T>: check .error and
branch on error.code:
| Code | When | Hint |
|---|---|---|
rag_embedding_conflict |
The explicit provider/model or a concurrent configuration revision conflicts with the collection. | Use the saved configuration and retry after checking the collection. |
rag_collection_unavailable |
The collection state does not permit the operation. | Inspect the collection state before retrying. |
rag_schema_missing |
The platform RAG schema is not present in this database. This should not happen on a managed install: the schema is provisioned for you. | If you hit it, the install's setup did not complete; redeploy the install or contact support. |
rag_embedding_model_unsupported |
Your model provider does not serve the model for the /v1/embeddings endpoint. |
Pick an embedding model your model provider lists (e.g. openai/text-embedding-3-small on OpenRouter). |
rag_embedding_failed |
The embedding provider rejected the request (auth, rate limit, transient). | The message carries the provider's error; retry policy is up to the caller. |
rag_invalid_request |
Required field missing or malformed (e.g. empty id, no chunks, topK not a positive integer, rerank or rewrite with a precomputed embedding, hybrid not a boolean). |
The message names the offending field. |
rag_ingest_cancelled |
The job's row was marked cancelled while the worker was running. |
Reachable for a job your own code started with ingestAsync. |
rag_rerank_failed |
The rerank call of a retrieve({ rerank }) failed. retrieve never returns it: you see it only in the warning the SDK logs. |
Nothing to branch on. A hit without rerankScore tells you the fallback ran. See When the rerank call fails. |
rag_rewrite_failed |
The chat call of a retrieve({ rewrite }) failed, or its reply held no usable query. retrieve never returns it: you see it only in the warning the SDK logs. Unreadable workspace settings fall back the same way under their own code. An unreadable rewrite prompt only warns. |
Nothing to branch on. retrieve searched your original text instead. See When the rewrite fails. |
rag_error |
Anything the pipeline could not classify (a Postgres error, a dropped connection). | error.cause carries the original error. |
The contract gate adds contract_version_unsupported,
capability_unavailable, contract_unreachable and contract_malformed.
Every gated stackbone.rag method requires the rag.basic capability:
see overview.
Pure helpers (parse, chunk) never hit the database or the embedding
provider, so the gate exempts them.
Internal subpaths
@stackbone/sdk exposes one low-level subpath behind the RAG pipeline. It
exists for the SDK's own use: creators usually do not import it
directly. This page lists it so you can find the surface when you need
something low-level.
| Subpath | Purpose |
|---|---|
@stackbone/sdk/rag/schema |
Drizzle table definitions for rag_collections, rag_documents, rag_chunks and rag_jobs (in the stackbone_platform schema), plus job-status types. Keep your own joins against these tables read-only. |
This subpath is internal to the platform contract: it may change shape between SDK majors.
Where to go next
stackbone.database: the same pool RAG runs on, for your own tables and migrations.- Agents & sessions: where a retrieval tool lives, and how an agent grounds answers in your documents.
- Workflows: where a long ingest belongs, as a step that persists and auto-retries.
@stackbone/sdkoverview: the rest of the surfaces: storage, AI, approval, secrets, config.