跳到正文
DEV Community· BartholomewVance6831·· 40 分鐘前AI 評分56

多租戶 SaaS 支援中心檢索:在成本控制中追蹤引用

Multi-Tenant SaaS Help Center Retrieval: Tracing Citations Through Cost Controls

AI 導讀

多租戶 SaaS 支援中心檢索可採用一份權威文件帳本派生租戶索引,並讓每個答案的引用 ID 對應不可變文本區段,以保留可追溯性並避免在多個索引重複儲存文本。向量與關鍵詞索引應視為可重建投影,租戶條件須在排序前套用;引用記錄則需保留租戶、文件、版本和字元範圍。共享索引儲存成本較低但須強制租戶篩選,獨立索引較易隔離但成本最高,帳本加派生分片則提供折衷方案。

正文

Decision note

Short answer: keep one canonical document ledger, derive tenant-scoped indexes from it, and make every answer carry a citation ID that resolves back to an immutable span. For a SaaS help center, this gives traceable citations without paying to store the same text in several indexes. In an edtech help center, it also makes near-duplicate course-policy records visible before they inflate index cost.

Approach Citation trace Index cost Best fit
One shared index with tenant filters Good if filters are mandatory Lowest Many small tenants, identical schema
Per-tenant indexes Straightforward Highest Strict isolation or very different ranking
Canonical ledger plus derived shards Strong, with extra metadata Middle Most teams balancing audit and scale

I would start with the third option. The ledger is the authority; vectors and keyword postings are disposable projections. This separation is the decision that matters more than the choice of embedding model.

Why do multi-tenant help center retrieval systems lose citation lineage?

The common failure is an innocent transformation. A Markdown article is split into chunks, normalized, deduplicated, and embedded. By the time a generator sees a chunk, its original heading, revision, and character offsets have disappeared. The answer can sound right, yet a reviewer cannot prove which sentence supported it.

Tenant boundaries make that failure dangerous. A refund-window article for tenant A can be almost identical to tenant B's article, except for one number. Approximate nearest-neighbor search may return the wrong copy when a filter is applied after retrieval, and a citation that only says "refund-window.md" hides the mistake. Apply the tenant predicate before ranking, then carry the tenant ID through every stage.

Near duplicates are a data problem, not a prompt problem. Store a normalized fingerprint for each span, but retain the original text and its source revision. If two fingerprints collide, mark them as a candidate pair and let a reviewer decide whether the records are equivalent, superseded, or intentionally different. Automatic deletion is a cheap way to erase a legitimate tenant-specific exception.

A citation contract that survives re-indexing

Give each chunk a stable citation object, separate from its vector ID:

type Citation = {
  tenantId: string;
  documentId: string;
  revision: string;
  start: number;
  end: number;
  textHash: string;
};

type RetrievedSpan = Citation & {
  text: string;
  score: number;
};

The hash is a check, not an identity. A new revision gets a new revision value even when the text is unchanged. At answer time, persist the retrieved span IDs and the model input. A citation renderer can then fetch the exact revision and verify that text.slice(start, end) hashes to textHash.

A common implementation mistake is treating vector IDs as permanent references. When a compaction job rewrites them, old answers become impossible to audit. The fix is boring: IDs stay projection-local, while the citation object stays in the ledger. Boring wins. That change also forces a review of exports, retention, and replay: an exported answer includes the source revision, while a deleted revision stays unavailable to new retrievals but remains identifiable in an audit record. This is the kind of unglamorous contract that saves a support engineer from guessing six months later.

Audit first.

Keep the evidence window small. Returning eight overlapping chunks increases token use and makes attribution fuzzy. A better rule is to return the top distinct spans, merge only adjacent ranges from the same revision, and reject an answer when no span clears your measured relevance threshold. I’m not sure one threshold will fit every tenant; support teams should calibrate it against labeled questions and inspect false citations separately from missed answers.

Measuring index cost without hiding isolation risk

Count bytes and operations per tenant, not just a blended monthly total. Track source bytes, chunk count, vector bytes, postings, refresh work, and duplicate ratio. The duplicate ratio is especially useful for an edtech catalog where the same accessibility policy appears in dozens of course spaces.

A shared index usually wins on raw storage, but only if the tenant filter is part of the retrieval query and covered by tests. Per-tenant indexes simplify deletion and export requests, yet idle tenants still consume metadata and background work. The ledger-plus-shard design lets you rebuild a shard from source data, which is valuable when an embedding model changes.

Run a cost-and-correctness fixture in CI. Include two tenants with identical titles and one deliberately different numeric limit. Assert that a query from tenant A never returns tenant B's span, that the citation resolves to the requested revision, and that rebuilding the projection leaves the citation unchanged.

const result = await retrieve({
  tenantId: "school-a",
  query: "How long can a student request a refund?",
  topK: 5,
});

for (const span of result.spans) {
  if (span.tenantId !== "school-a") throw new Error("tenant leak");
  const source = await ledger.get(span.documentId, span.revision);
  if (hash(source.text.slice(span.start, span.end)) !== span.textHash) {
    throw new Error("stale citation");
  }
}

The test should fail loudly. A silent fallback to an unfiltered search is an incident waiting to happen.

Choose separate indexes when contractual deletion must be immediate, tenants require incompatible analyzers, or one tenant's traffic can dominate the shared graph. The extra storage buys operational independence. It does not remove the need for a ledger; without one, citations still rot during re-indexing.

Stick with a shared filtered index when tenants use the same language, chunk policy, and retention rules, and your authorization layer can enforce pre-filtering. A hybrid can route large tenants to dedicated shards while keeping small tenants together. Document that rule; otherwise cost surprises arrive as a routing bug disguised as a capacity problem.

References

來源:DEV Community · dev.to