LangGraph 付款例外處理智能體:時限分流、制裁命中與 LLM 不可自行作出的放行決定
Agentic AI for Payment Exceptions in LangGraph: Deadline Triage, Sanctions Hits & a Release the LLM Must Never Make
LangGraph 付款例外處理智能體以模型分類例外、整理證據及提出處置建議,並將放行、暫緩或拒絕交由人工逐次決定。剩餘截止時間及緊急程度由程式按規則計算;制裁命中不能由模型清除,欄位修正建議須先通過規則驗證。方案以約 200 宗歷史例外案例作評估,對已確認的真實制裁匹配設下不得建議放行的零容忍閘門。
Payments is the domain where "the happy path is already automated" is most literally true — straight-through processing has existed for decades, and a clean payment never touches a human. All the cost lives in the exceptions: the malformed beneficiary reference, the sanctions screening hit that's probably a false positive, the duplicate suspicion, the failed downstream leg — each needing context from three systems, a judgment call, and often a sign-off, under a hard clock. This post builds the agent for that queue in LangGraph, with the cardinal rule at its hardest: release / hold / reject is proposed by the model and decided by a human, every single time. (Full version on the original — link at the end.)
TL;DR
| What | How |
|---|---|
| Happy path | Untouched — STP stays deterministic; the agent never sees clean payments |
| Triage | A small model classifies exception type + urgency from the cut-off clock |
| Evidence | Read-only tools: beneficiary history, sanctions-hit detail, duplicate scan |
| Sanctions hits | Agent drafts the false-positive narrative; it can NEVER clear a hit |
| Repairs | LLM proposes field fixes; scheme rules (IBAN, BIC, purpose codes) re-validate deterministically |
| Release |
requires_human=True unconditionally — releasing money is never autonomous |
| Audit | Signed, hash-chained entry per exception: evidence + rationale + approver + clock |
The error costs are brutally asymmetric: block a legitimate payment and you have an angry client and a breached SLA; release a bad one and you have a loss, a regulatory incident, and possibly a sanctions violation measured in millions. The agent's product is a disposition memo, not a payment instruction — a named human turns that memo into money movement.
1. Scope — the agent lives in the exception queue only
Straight-through processing is not an AI problem — validation, enrichment, routing, and settlement of clean payments are deterministic, fast, and already work. The agent subscribes to the exception queue and nothing else:
payments in -> deterministic STP pipeline -> settled (95-99%, untouched)
|
+--> EXCEPTIONS (the residual) -> the agent
- missing/malformed reference or beneficiary data
- sanctions screening hit
- duplicate suspicion
- failed downstream leg / funding / FX / cut-off
Nobody has to re-certify the payment pipeline, because the pipeline didn't change. The agent is a new consumer of an existing queue, and its write path is one deterministic, gated service.
2. Deadline-aware triage — urgency is data, not vibes
Payment exceptions have a property reconciliation breaks don't: a cut-off. The same malformed reference is routine at 9am and an incident at 15:40 when the clearing window closes at 16:00. Triage scores two independent things: what kind of exception is this (a small model) and how much clock is left (arithmetic — never the model).
async def triage_exception(exc: PaymentException) -> QueueItem:
label = await haiku.ainvoke(triage_prompt(exc), response_format=TriageResult)
# Urgency is deterministic: minutes to the relevant cut-off, from refdata.
cutoff = scheme_cutoffs.for_payment(exc.payment) # scheme + currency + date
minutes_left = (cutoff - utcnow()).total_seconds() / 60
return QueueItem(
exception=exc, label=label.label,
priority=priority_matrix(label.label, minutes_left), # code, not model
escalate_now=(minutes_left < 30 and exc.payment.amount > HIGH_VALUE))
The model never computes urgency — an LLM guessing "this seems urgent" is noise wearing a suit. And high-value + low-clock short-circuits straight to a human with whatever evidence exists so far; an agent that spends eleven minutes beautifully investigating a payment that misses its window has optimised the wrong thing.
3. Evidence gathering — read-only typed tools
Each tool returns sourced Evidence with an ID the explanation must later cite: beneficiary_history (prior payments, amounts, past repairs — the workhorse), sanctions_hit_detail (the screening engine's matched entry + fuzzy score), duplicate_scan (deterministic: same amount+beneficiary+reference in a window), and scheme_rules (IBAN structure, BIC validity, purpose codes). "47 prior payments to this exact beneficiary, three with the same malformed reference, all repaired the same way" turns a judgment call into a review.
4. The two hard cases
Sanctions hits — draft the discrimination, never the decision
Screening engines are deliberately over-sensitive: fuzzy name matching collides "Mohammed Al-Rashid, DOB 1961, Damascus" with "Mohamed Rashid, DOB 1988, Mumbai." Analysts resolve most hits as false positives by discriminating on the non-matching fields — slow, repetitive, perfect agent work. The decision is not:
class HitAssessment(BaseModel):
discriminators: list[Discriminator] # field, list_value, payment_value, match/mismatch
cited_evidence: list[str] # required — guardian rejects uncited claims
narrative: str # the write-up an analyst would have produced
assessment: Literal['likely_false_positive', 'possible_match', 'insufficient_data']
# NOTE: there is no 'clear' and no 'release' in this enum. On purpose.
# The proposal ALWAYS routes into your existing four-eyes sanctions process:
return Proposal(action='route_to_sanctions_review', requires_human=True, ...)
The model literally cannot output "clear the hit." Its best case is likely_false_positive routed into your existing four-eyes process with the discrimination table pre-built. You are never asking anyone to approve "AI clears sanctions hits," because it doesn't.
Repairs — the LLM proposes, the scheme rules decide
The model is genuinely good at inferring the intended value from context (invoice references, beneficiary history) — but an inferred IBAN is a hallucination risk wearing a fix, so every repair is re-validated deterministically before it becomes a proposal:
def validate_repair(draft: RepairDraft, rules: SchemeRules) -> RepairDraft:
if draft.field == 'beneficiary_iban':
assert iban_checksum_valid(draft.proposed_value), 'repair fails IBAN mod-97 check'
assert country_consistent(draft.proposed_value, rules), 'IBAN country vs scheme mismatch'
if draft.field == 'purpose_code':
assert draft.proposed_value in rules.allowed_purpose_codes, 'unknown purpose code'
assert draft.cited_evidence, 'repair cites no evidence'
return draft # only now may it enter a Proposal
A fabricated IBAN fails mod-97 before any human sees it. A purpose code the model "remembered" from training fails the allow-list.
5. The disposition — always gated
Everything converges into a typed disposition proposal, and note what is not here:
return Proposal(
action=d.recommendation, # 'release' | 'hold' | 'reject'
payload={'payment_id': ..., 'repairs': [...]},
rationale=d.rationale, confidence=d.confidence,
requires_human=True, # UNCONDITIONAL. Not confidence-gated. Ever.
)
There is no if confidence > 0.95: requires_human = False. That line is the single most tempting edit in this whole system; it will appear in a code review within a month, and it converts your audit-proof workflow into an autonomous payment engine the first time it's merged. In reconciliation the equivalent shortcut mis-posts a journal you can reverse; here it releases money you may not get back. Hold the line in code review.
6. The gate, the clock, and the idempotent release
from langgraph.types import interrupt
async def disposition_node(state: AgentState) -> AgentState:
decision = interrupt({ # durable pause — survives restarts
'payment_id': state.exception.payment.id,
'proposal': state.proposals[-1].model_dump(),
'cutoff_utc': state.cutoff.isoformat(), # approver sees the clock too
'needs': 'approve|reject|amend'})
if decision['action'] != 'approve':
return record_rejection(state, state.proposals[-1], decision)
# The ONLY write path. Deterministic, idempotent, approval-token-bound.
if state.proposals[-1].action == 'release':
await payments.release(
payment_id=state.exception.payment.id,
approval_token=decision['token'], # service verifies token, not caller
idempotency_key=state.exception.id) # retries cannot double-release
return append_audit(state, ..., extra={'minutes_to_cutoff_at_decision': state.minutes_left()})
Three payment-specific details: the release service verifies the approval token itself (a bug or prompt injection reaching this path with a fabricated proposal still can't move money); idempotency key = exception ID (graph retries and double-clicks collapse into one release — double-release is the payments equivalent of a double-posted journal, except the counterparty keeps it); and the clock goes into the audit log ("approved with 12 minutes to cut-off" is context a regulator will want).
7. Evals — with one zero-tolerance gate
The golden set is ~200 historical exceptions with analyst dispositions. Standard scores run in CI (disposition accuracy, groundedness, narrative quality) — plus one gate with no threshold to tune:
# ZERO-TOLERANCE: on the subset of TRUE sanctions matches (confirmed by compliance),
# the pipeline must never produce recommendation='release'. Not 99%. Zero.
for case in [c for c in golden if c.confirmed_sanctions_match]:
draft = await run_payments_pipeline(case.exception)
assert draft.recommendation != 'release', f'RELEASE on true match {case.id} — hard fail'
If a prompt or model change ever makes that assertion fail, the change doesn't ship, and no aggregate accuracy improvement buys it back. Some regressions are not trade-offs.
Representative metrics
| Metric | Manual | Agentic (propose + approve) |
|---|---|---|
| Handling time per exception | 15-25 min | 3-5 min (review a memo) |
| Exceptions with one-click disposition draft | n/a | ~60-80% |
| Missed cut-offs from queue ordering | FIFO luck | roughly halved |
| Sanctions decisions made by AI | n/a | zero — structurally |
| Audit completeness | notes + tribal memory | 100% — signed entry incl. clock state |
Representative of the workflow class, not a single audited deployment — instrument with Langfuse and publish numbers your own auditors can check.
The model to carry forward
The payments agent is a reconciliation agent with two extra disciplines bolted on: a clock that only code may read, and a sanctions wall the model cannot even express a crossing of — no "clear," no "release" in its sanctions vocabulary, dispositions gated by a named human holding a token the release service independently verifies. The LLM does what it's uniquely good at: reading a beneficiary's two-year history, discriminating a fuzzy name match, inferring the intended reference — and writing the memo. Money moves only when a person says so, and the audit log can prove it, minute by minute, against the cut-off. If anyone sells you "autonomous payment operations," they're selling the exact component this architecture exists to forbid — the human gate is the product, not the limitation.
This is a condensed version. The full playbook — with the complete evidence tools, the full LangGraph subgraph, the when-NOT-to-build list, and every code block — is on PrepStack:
來源:DEV Community · dev.to