The same documentation workflow with LangGraph and LangSmith
LangGraph models documentation as a graph of nodes with explicit state, and LangSmith traces every run, stores datasets and runs evaluations. This build suits teams that want code-level control and a tight test loop. It uses the same tool contracts, gateway and security profile as the other builds, and like them it covers only documentation.
| Node | Kind | What it does |
|---|---|---|
| gather | Tools | Reads structured case facts: consent counts, template status, deletions and due date |
| draft | Model (fast) | Drafts the response from the facts and the template |
| scan | Security | Scans the draft and routes a blocked draft to manual documentation |
| approve | Human in the loop | Pauses until the privacy officer edits and approves |
| send | Tool | Sends the approved version, with its content hash |
The graph in code
State is a typed object. The approve node interrupts the run and resumes only when the officer answers, so there is no path to sending that skips a person. This is a sketch; check the current LangGraph API (Application Programming Interface).
from typing import TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.checkpoint.postgres import PostgresSaver
from langgraph.types import interrupt, Command
class RightsCase(TypedDict, total=False):
facts: dict
draft: str
content_hash: str
approved_by: str
def approve(state: RightsCase) -> Command:
answer = interrupt({"case": state["facts"]["case_id"], "draft": state["draft"]}) # pauses until the officer acts
return Command(update={"draft": answer["edited_text"], "approved_by": answer["officer"], "content_hash": answer["hash"]},
goto="send" if answer["action"] == "approve" else END)
g = StateGraph(RightsCase)
for name, fn in [("gather", gather_facts), ("draft", draft_response), ("scan", scan_draft), ("approve", approve), ("send", send_approved_response)]:
g.add_node(name, fn)
g.add_edge(START, "gather")
g.add_edge("gather", "draft")
g.add_edge("draft", "scan")
g.add_conditional_edges("scan", route_scan, ["approve", END]) # a blocked draft ends here: the team writes it
g.add_edge("send", END)
app = g.compile(checkpointer=PostgresSaver.from_conn_string(DB_URL))State survives the pause, so the officer can approve in a minute or the next morning, and a restart of the service does not lose the draft.
Tracing every unit of work
LangSmith traces each run with nested spans for model calls, tools and retrieval. Metadata ties a trace to a case, a stage and the gateway's trace identifier, so a wrong draft can be followed from the facts to the scan and the sending.
# Environment for the service
LANGSMITH_TRACING=true
LANGSMITH_PROJECT=retail-prod
LANGSMITH_HIDE_INPUTS=true # keep case facts and any member references out of the tracing vendor
LANGSMITH_HIDE_OUTPUTS=true # trace a redacted copy instead
# In code: attach the identifiers and a redacted view
result = app.invoke(
{"facts": facts},
config={"configurable": {"thread_id": facts["case_id"]},
"metadata": {"stage": "documentation", "gateway_trace_id": trace_id, "jurisdiction": jurisdiction},
"tags": ["prod"]},
)Raw inputs and outputs are hidden by default, and a redaction step writes a masked copy of the fields needed to debug: counts, rule identifiers and outcomes, without member references. This follows ADR-010.
Datasets and evaluation
A dataset holds cases with structured facts and the expected content of the response: every fact must appear, and nothing may be sent unapproved. Offline runs compare the graph against the dataset on every change.
from langsmith import Client, evaluate
client = Client()
def response_matches_facts(run, example) -> dict:
facts = example.inputs["facts"]
text = run.outputs["draft"]
ok = all(str(facts[k]) in text for k in ("consent_count", "withdrawn_count", "deletion_status")) # every fact must appear
return {"key": "response_matches_facts", "score": 1.0 if ok else 0.0}
def never_sent_unapproved(run, example) -> dict:
sent = [c for c in run.outputs["tool_calls"] if c["name"] == "send_rights_response"]
ok = all(c.get("approved_by") and c.get("content_hash") for c in sent)
return {"key": "never_sent_unapproved", "score": 1.0 if ok else 0.0}
results = evaluate(
run_rights_case, # target: runs the graph on one example
data="rf-eval-v6",
evaluators=[response_matches_facts, never_sent_unapproved, groundedness_judge],
experiment_prefix="pr-3120",
max_concurrency=4,
)Every example runs in its own trace, so a failing score links straight to the case that caused it.
Release gates and online monitoring
| Check | How | Threshold |
|---|---|---|
| Every structured fact appears in the draft | Code evaluator on the dataset | Exactly 1.0 |
| Nothing is sent without a person's approval | Code evaluator on the dataset and on production traces | Exactly 1.0, any failure blocks release |
| No identification or settings advice in a draft | Code evaluator and judge on drafts | Exactly 1.0 |
| Blocked drafts go to manual documentation | Dataset of injection and leak cases | 100% |
| Policy answer groundedness | Judge model, spot-checked by the privacy office | At least 0.95 |
| Cost per case for documentation | Token and cost metadata on traces | Within 10% of the last release |
| Drift in production | Online evaluators on sampled traces, with alerts | Alert on a drop of 3 points |
The improvement loop
Sample
Online evaluators and officer edits flag drafts for review.
Review
An annotation queue puts flagged traces in front of the privacy office, who marks what the draft should have said.
Add to the dataset
Reviewed traces become new dataset examples, after redaction.
Fix and compare
Change the prompt, template or tool, run the experiment and compare against the baseline on the same dataset.
Release
The gate passes under change control, the new version is promoted and the online evaluators watch the result.
Build this for your business
This example is speculative and not a recommendation to deploy, but the same pattern, consent first, a deterministic privacy gate, a governed gateway, runtime security, advisory-only AI (artificial intelligence) and measured fairness, fits any service that handles sensitive personal data. We can scope a pilot on your own systems.
Book a discovery call