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, aboveescalate_below: 0.45. Groundedness0.95, aboveconfidence_threshold: 0.6. decide_level1returnsanswerwith “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_level1returnsconversewith “not a support request — answered conversationally, no ticket”.
“Can you raise my Cloud Run instance quota for europe-west1?”
- Similarity
0.50passes signal 1: the deployment docs matched. Groundedness0.05fails signal 2. decide_level1returnsrewritewith “groundedness 0.05 < confidence_threshold 0.6”.- After corrective retrieval,
decide_level2returnsescalatewith “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_level1returnsrewritewith “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 only | Groundedness only | Both (Level 1) | |
|---|---|---|---|
| Nothing in the corpus is close | Caught | Caught | Caught |
| Right document, wrong content | Missed, answers from a near-miss | Caught | Caught |
| Deterministic? | Yes | No, a model call | Half, and the half that can’t fail open |
| Cost | Free | One cheap call | One cheap call |
| Failure mode | Confident answers to unanswerable questions | The guardrail rests entirely on a model judging a model | None 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 2 | Groundedness only at Level 2 (built) | |
|---|---|---|
| A keyword-only match | Rejected: it carries no cosine similarity | Graded like any other passage |
| A follow-up with a vague embedding | Rejected again, whatever the rewrite found | Answered if the passages support it |
| What stops a wrong answer | Both signals | The grader, and citations on the answer |
| Deterministic half | Kept | Given 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-linestate["route"]lookup. LangGraph never evaluates a condition. - The answer check runs first. Strong evidence answers even when the grader called the message conversation.
conversehas to be asked for. A missing or malformedkindis a question, so a broken grade can never become small talk.- No model is asked whether to escalate. The
escalatenode 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
| Key | Default | Job |
|---|---|---|
retrieval_drop_floor | 0.3 | Discards weak chunks before the model sees them |
escalate_below | 0.45 | Fails Level 1 when the best surviving chunk is still weak |
confidence_threshold | 0.6 | Fails either level when the grader says the passages do not support an answer |
corrective.enabled | true | Whether 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.