Retrieval

The retrieve node. Embed the message, scan pgvector scoped to the client, drop weak hits. No model call, and the first of the two guardrail signals.


retrieve is Level 1’s first node and makes no model call. It embeds the message, runs a cosine scan over the client’s chunks, and discards anything below the drop floor. The node lives in agent/templates/documents.py; the search is retrieve() in retrieval/search.py, the one retriever both levels use (D12 in docs/decisions.md).

The best surviving similarity is signal 1 of the guardrail.


The flow

retrieve(state)
  │
  └─ retrieve()               retrieval/search.py · one query, no keywords
       ├─ embed_queries()     Voyage, input_type="query"
       ├─ _dense()            pgvector cosine · WHERE client_id = ? · LIMIT k*2
       ├─ floor filter        drop below retrieval_drop_floor, then [:top_k]
       └─ rrf_merge()         one list, so the identity

The node writes two things to state: the surviving hits, and top_similarity, which is 0.0 when nothing survived. Hits are stored as plain dicts (title, source URI, text, similarity) because the checkpointer saves state as JSON.

Level 2 calls the same retrieve() with more queries and keywords. See Corrective Retrieval.


A worked example

Two turns from one conversation, with drop floor 0.3 and escalate below 0.45. A real run.

Turn 1: “My Cloud Run container fails to start — what does the PORT error mean?”

  • Voyage embeds the message, and pgvector returns the 10 nearest chunks for this client.
  • Five survive the floor. The best, from Cloud Run: deployment and runtime errors, sits at 0.62.
  • 0.62 clears escalate_below, so signal 1 passes and the turn goes to grading. The node took 405 ms.

Turn 2: “so is that the same as the health check timing out?”

  • The message is embedded as written. Nothing in it names Cloud Run or the PORT error.
  • Two chunks survive the floor, the best at 0.36, from the right document.
  • 0.36 is under escalate_below. Level 1 falls short, and the turn goes to Level 2, where a rewrite with the conversation in view finds the answer.

Design decisions

Retrieval is a node, not a tool

Agentic retrieval is for decisions with more than one right answer. This one has one: the model always needs context, so letting it choose adds failure modes without adding capability.

Node (this graph)Tool call (the retired loop)
Deciding to searchNothing to decide; it always happensThe model may skip it or mistime it
Answering from memoryImpossible, grounding precedes the modelModels sometimes reply from parameters
LatencyOne embeddingReason, emit, wait, resume, twice if it reformulates
ReplaySame message, same embedding, same top-kA generated query differs per run
Injection surfaceUser text is embedded, never interpretedAn attacker influences what gets searched
Multi-hopNot possibleThe reason to choose it
What the grader ratesExactly the passages the answerer will seeA moving target

Switch to a tool when multi-hop pays off. Reformulating a bad query is not a reason to: Level 2 does that with a structured call, and keeps the search itself deterministic.

Follow-ups are Level 2’s job

Level 1 embeds the bare message. The retired pipeline prepended the previous user turn instead. The graph dropped that heuristic because Level 2 does the job properly.

Prepend the previous user turn (retired)Bare message, rewrite at Level 2 (built)
A follow-up at Level 1Often clears, for freeUsually falls short
A three-turn reference chainLoses the subjectThe rewrite sees the last six turns
A change of subjectThe old subject pollutes the queryNothing pollutes it
Cost on a follow-upNothingA rewrite, a regrade, one embedding call: $0.039 against $0.026 in the live run
Failure directionEscalates an answerable questionThe same, one tier later

The search itself

  • Embedded as a question, not a document. input_type="query" differs from the "document" type used at ingest, and Voyage exploits the asymmetry for recall. See Ingestion Pipeline.
  • Over-fetch, then filter. pgvector can’t apply the floor inside the scan, so filtering in SQL would return fewer than k rows whenever the floor bites.
  • The floor makes “nothing matched” a case. Zero survivors still go to the grader, which has to recognise a greeting. Python then overwrites its confidence with 0.
  • client_id is in the WHERE clause. Tenant isolation is a query predicate, not a convention. See Multi-Tenancy.

Reproducibility is scoped to one embedding model version. “Same query, same top-k” holds while the model is fixed. voyage-3 is hardcoded in embed.py and no version is recorded on the chunk or the step, so bumping it silently changes what old runs would retrieve today.


Configuration

KeyDefaultWhat it does
agent.retrieval_drop_floor0.3Similarity below which a chunk is discarded. Also applied to the dense half of Level 2
retrieval.top_k5Chunks kept per list and after fusion
retrieval.rrf_k60Fusion damping constant; only matters at Level 2
retrieval.keyword.enabledtrueLevel 2 keyword search; off, Level 2 is multi-query dense only

The retrieval: block is optional and query-side: edits apply on the next request, with no re-ingest. Full key list: Config Reference.

The floor and escalate_below are not the same knob. The floor discards; escalate_below fails Level 1. They read the same number at different points, and their ordering is validated at startup. See The Guardrail.

Measured

evals/retrieval_eval.py scores retrieve() against the golden set (no model calls). Full file: evals/results/retrieval-2026-10-08.md, SHA f4f50db.

Varianttop_krecall@kMRR
Level 1, dense only50.8890.889
Level 1, dense only100.8890.889
Level 2, dense + keyword51.0000.917
Level 2, dense + keyword101.0000.917
  • n = 9 questions: one question moves a metric 11 points. Direction, not a benchmark.
  • The one Level 1 miss is a floor drop (cosine 0.27 < 0.3), which keyword fusion recovers.
  • Reranking was not built: there is no ordering error left to fix.

Next: Grading, the second signal and the triage that rides with it.