Skip to main content
Source retrieval is the mechanism that connects LLM agents to your evidence layer. Rather than passing full documents into every prompt, you retrieve only the spans and nodes most relevant to the current question — keeping context windows focused, citations accurate, and costs predictable. SourceRetriever is the interface every agent and pipeline in CL SDK uses to fetch evidence at query time.

SourceRetriever Interface

searchSourceNodes is optional — implement it when your store has a structured node graph. Agents fall back to searchSourceSpans when searchSourceNodes is not available.

Retrieval Modes

Uses structured relationships and exact metadata matches. No vector search. Best for queries where you know the exact section, form number, or metadata filter that contains the answer.

Memory Store Usage

MemorySourceStore is an in-process store backed by a flat array. It uses lexical search (substring and keyword matching) rather than vector embeddings. Use it for development, testing, and single-request extraction workflows.
MemorySourceStore is not persistent. All spans and chunks are lost when the process exits. For production use, implement SourceStore against your own database — see the Storage Overview for guidance.

Deterministic Ordering

When you combine results from multiple retrieval passes (vector search + structured filters, or multiple document IDs), use orderSourceEvidence to produce a stable, deterministic ranking before passing evidence to an LLM.
Deterministic ordering ensures that identical queries always produce the same evidence sequence, making LLM outputs reproducible and diff-friendly in testing.

Implementing a Custom SourceRetriever

You can plug any data source into CL SDK agents by implementing SourceRetriever. The minimum viable implementation requires only searchSourceSpans.
Pass your implementation to any agent or pipeline via the sourceRetriever config option:
If your store supports searchSourceNodes, implement that method too. Agents that support node-level retrieval will use it to build richer hierarchical context before falling back to span-level search.

Filters Reference

string
Match spans whose sectionId equals this value. Useful for scoping queries to declarations, definitions, or named endorsements.
string
Match spans from a specific ISO or carrier form (e.g. "CG 00 01", "IM 7053").
string
Filter by source kind: "policy_pdf", "application_pdf", "email", "attachment", or "manual_note".
string
Filter by span unit type: "page", "section", "table", "table_row", "table_cell", or "text".
Any key in a span’s metadata record is also available as a filter key. Filter matching is exact string equality.