The Guardrail

decide_level1 and decide_level2. Two signals, three thresholds, two plain functions. The routes the model is structurally unable to choose.


The guardrail is every route in the graph, and it is plain Python. decide_level1 and decide_level2 read the signals from retrieval and grading, compare them to configured thresholds, and return the route and the reason for it. The grade node writes both to state; the edge only reads the route.

No model is consulted, and none can override it. This is the component the whole graph exists to make possible.


The flow

flowchart TD
  A[decide_level1] --> B{hits, similarity at least escalate_below, and groundedness at least confidence_threshold?}
  B -->|yes| ANS[answer]
  B -->|no| C{kind is conversation?}
  C -->|yes| CON[converse]
  C -->|no| D{corrective.enabled?}
  D -->|yes| L2[Level 2: rewrite, hybrid_retrieve, regrade]
  D -->|no| ESC[escalate]
  L2 --> E[decide_level2]
  E --> F{hits, and groundedness at least confidence_threshold?}
  F -->|yes| ANS
  F -->|no| ESC

Each function returns a route and a human-readable sentence saying which check produced it. The sentence is recorded on the next step and rendered in the UI, so every answer and every escalation carries its own justification.


A worked example

Four messages from a real run, one down each path.

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

  • Similarity 0.62, above escalate_below: 0.45. Groundedness 0.95, above confidence_threshold: 0.6.
  • decide_level1 returns answer with “similarity 0.62 and groundedness 0.95 both above threshold”. A successful answer is as justified in the trail as a refusal.

“hi”

  • No passages, and the grader returned kind: conversation.
  • decide_level1 returns converse with “not a support request — answered conversationally, no ticket”.

“Can you raise my Cloud Run instance quota for europe-west1?”

  • Similarity 0.50 passes signal 1: the deployment docs matched. Groundedness 0.05 fails signal 2.
  • decide_level1 returns rewrite with “groundedness 0.05 < confidence_threshold 0.6”.
  • After corrective retrieval, decide_level2 returns escalate with “groundedness 0.05 < confidence_threshold 0.6 after corrective retrieval”.

“How do I configure a Cloud SQL read replica to fail over automatically?”

  • One passage survives the floor, at 0.33, from a Cloud Run autoscaling page.
  • decide_level1 returns rewrite with “top similarity 0.33 < escalate_below 0.45”. Level 2 finds nothing better, and the turn escalates as a documentation gap.

The quota case is the one that justifies two signals. Similarity alone would have answered it.


Design decisions

Why two signals, not one

Each signal alone fails in a way the other covers.

Similarity onlyGroundedness onlyBoth (Level 1)
Nothing in the corpus is closeCaughtCaughtCaught
Right document, wrong contentMissed, answers from a near-missCaughtCaught
Deterministic?YesNo, a model callHalf, and the half that can’t fail open
CostFreeOne cheap callOne cheap call
Failure modeConfident answers to unanswerable questionsThe guardrail rests entirely on a model judging a modelNone of those

Level 2 drops the cosine floor

Level 2 answers on groundedness alone. The passages it exists to find are the ones dense retrieval ranked low.

Keep escalate_below at Level 2Groundedness only at Level 2 (built)
A keyword-only matchRejected: it carries no cosine similarityGraded like any other passage
A follow-up with a vague embeddingRejected again, whatever the rewrite foundAnswered if the passages support it
What stops a wrong answerBoth signalsThe grader, and citations on the answer
Deterministic halfKeptGiven up, for this tier only

The trade is stated in the trail: a Level 2 answer’s reason names groundedness only.

The decision, and the record of it

  • The reason string is a product feature, not a log line. It is the answer to “why did it escalate?”, produced by the comparison that made the decision.
  • The route is a string on state. The grade node calls the decide function and writes route; the conditional edge is a one-line state["route"] lookup. LangGraph never evaluates a condition.
  • The answer check runs first. Strong evidence answers even when the grader called the message conversation.
  • converse has to be asked for. A missing or malformed kind is a question, so a broken grade can never become small talk.
  • No model is asked whether to escalate. The escalate node drafts ticket fields after the route is chosen. See Handoff & Ticket.
  • “Escalate” is not “file a ticket”. It is an offer the user confirms. Nothing reaches the queue unrequested.
  • There is no decision step in the trail. The reason is recorded on the step the route produced, which keeps the trail one entry per unit of work.

Configuration

KeyDefaultJob
retrieval_drop_floor0.3Discards weak chunks before the model sees them
escalate_below0.45Fails Level 1 when the best surviving chunk is still weak
confidence_threshold0.6Fails either level when the grader says the passages do not support an answer
corrective.enabledtrueWhether a turn that fails Level 1 gets Level 2 or goes straight to a human

The first two operate on the same number at different points, so their ordering matters. Every surviving hit is at or above the drop floor by construction, so if escalate_below <= retrieval_drop_floor, signal 1 can never fire. The config validator rejects that relationship at startup for graph clients. Keys in full: Config Reference.


Next: Corrective Retrieval, where a turn goes when Level 1 falls short.