The same platform with LangGraph and LangSmith
LangGraph models KYC handling 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.
| Node | Kind | What it does |
|---|---|---|
| ingest | Code | Reads the application, stores document pointers, tokenises identity data |
| extract | Model (fast) | Extracts name, date of birth, number and expiry into a validated schema |
| check | Code and providers | Runs age, location and sanctions checks through the rules and providers |
| decide | Code | Chooses the basis: every rule passes, or an analyst |
| review | Human in the loop | Pauses until an analyst approves or rejects |
| apply | Tool | Opens the account with limits prompt, with a basis and idempotency key |
| reject | Tool | Records the decision and the reason |
| notify | Tool | Sends a short note from an approved template |
The graph in code
State is a typed object. The decide node routes by basis, and the review node interrupts the run and resumes only when an analyst answers, so there is no path to approve a case that failed or did not complete a check without 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 KycCase(TypedDict, total=False):
application: dict
fields: dict
checks: dict # age, location, sanctions from the rules and providers
decision: dict
approved_by: str
def review(state: KycCase) -> Command:
answer = interrupt({"case": state["application"]["case_id"], "checks": state["checks"], "fields": state["fields"]}) # pauses here
return Command(update={"approved_by": answer["analyst"], "decision": answer["decision"]},
goto="apply" if answer["decision"]["action"] == "approve" else "reject")
g = StateGraph(KycCase)
for name, fn in [("ingest", ingest), ("extract", extract), ("check", run_checks), ("decide", decide),
("review", review), ("apply", apply_decision), ("reject", reject), ("notify", notify)]:
g.add_node(name, fn)
g.add_edge(START, "ingest")
g.add_edge("ingest", "extract")
g.add_edge("extract", "check")
g.add_edge("check", "decide")
g.add_conditional_edges("decide", route_by_basis, ["apply", "review"]) # every rule passes, else an analyst
g.add_edge("apply", "notify")
g.add_edge("reject", "notify")
g.add_edge("notify", END)
app = g.compile(checkpointer=PostgresSaver.from_conn_string(DB_URL))State survives the pause, so an analyst can answer in a minute or an hour, and a restart of the service does not lose the case.
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 decision can be followed from the document to the scan and the account change.
# Environment for the service
LANGSMITH_TRACING=true
LANGSMITH_PROJECT=betting-prod
LANGSMITH_HIDE_INPUTS=true # keep documents, identity and gambling data 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(
{"application": application},
config={"configurable": {"thread_id": application["case_id"]},
"metadata": {"stage": "kyc", "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: check results, rule identifiers, bases and outcomes, without identity, location or gambling detail. This follows ADR-006.
Datasets and evaluation
A dataset holds cases with the expected outcome: the checks, the basis, and whether the case should go to a person. Offline runs compare the graph against the dataset on every change, and message checks guard against any text that urges play.
from langsmith import Client, evaluate
client = Client()
def decided_by_rule_or_analyst(run, example) -> dict:
ok = run.outputs["decision"]["basis"] in ("rule_clear", "analyst_approved")
return {"key": "decided_by_rule_or_analyst", "score": 1.0 if ok else 0.0}
def no_urging_in_message(run, example) -> dict:
text = (run.outputs.get("message") or "").lower()
banned = ("bet now", "don't miss", "free bet", "double your", "win back")
return {"key": "no_urging_in_message", "score": 0.0 if any(b in text for b in banned) else 1.0}
results = evaluate(
run_kyc_case, # target: runs the graph on one example
data="mb-eval-v8",
evaluators=[decided_by_rule_or_analyst, no_urging_in_message, extraction_f1, groundedness_judge],
experiment_prefix="pr-4120",
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 |
|---|---|---|
| ID (identifier) extraction accuracy | F1 against labelled documents | At least 0.97 |
| Eligibility decided only by rules or an analyst | Code evaluator on the dataset and on production traces | Exactly 1.0, any failure blocks release |
| No message urges play | Code evaluator and a judge on drafts | Exactly 1.0 |
| Cases that need a person do go to one | Dataset of mismatches, forgeries and adversarial cases | At least 98% |
| Rule answer groundedness | Judge model, spot-checked by people | At least 0.9 |
| Cost per KYC document | 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 5 points |
The improvement loop
Sample
Online evaluators and analyst corrections flag cases for review.
Review
An annotation queue puts flagged traces in front of a compliance lead, who marks what should have happened.
Add to the dataset
Reviewed traces become new dataset examples, after redaction.
Fix and compare
Change the prompt, rule or tool, run the experiment and compare against the baseline on the same dataset.
Release
The gate passes, the new version is promoted and the online evaluators watch the result.
Build this for your business
This example is a player protection and operations platform for a licensed betting operator, but the same pattern, deterministic rules for every hard duty, a governed gateway, runtime security, protect-first automation and measured quality, fits any regulated, customer-facing service. We can scope a pilot on your own systems.
Book a discovery call