How to Build a transaction monitoring Agent Using LangChain in Python for healthcare

By Cyprian AaronsUpdated 2026-04-21
transaction-monitoringlangchainpythonhealthcare

A transaction monitoring agent for healthcare watches claims, payments, eligibility events, and provider activity for patterns that look wrong: duplicate billing, upcoding, suspicious refund loops, policy abuse, or PHI exposure in downstream workflows. It matters because healthcare has a higher compliance bar than most domains; the agent needs to detect risk without leaking patient data, breaking audit requirements, or making decisions it cannot explain.

Architecture

  • Event ingestion layer

    • Pulls transactions from claims systems, EHR-adjacent services, payment processors, or message queues.
    • Normalizes records into a common schema: member_id, provider_id, claim_amount, procedure_code, timestamp, region.
  • Risk rules engine

    • Handles deterministic checks first: duplicate claim IDs, impossible service dates, out-of-network anomalies.
    • Keeps obvious cases out of the LLM path.
  • LangChain reasoning layer

    • Uses a chat model through ChatOpenAI or another provider-compatible model.
    • Converts structured transaction data into a risk assessment and explanation.
  • Retrieval layer

    • Uses FAISS or another vector store with policy documents, billing guidelines, and internal SOPs.
    • Lets the agent cite approved rules instead of inventing them.
  • Decision and escalation layer

    • Produces outcomes like allow, review, or escalate.
    • Sends high-risk cases to compliance analysts with a traceable explanation.
  • Audit and observability layer

    • Stores prompts, outputs, rule hits, timestamps, and model versions.
    • Required for healthcare audits and post-incident review.

Implementation

1) Install the core packages

Use LangChain split packages. Keep the model layer separate from retrieval and schema handling.

pip install langchain langchain-openai langchain-community langchain-core faiss-cpu pydantic

Set your API key and make sure your deployment region matches your data residency requirement.

export OPENAI_API_KEY="your-key"

2) Define the transaction schema and risk prompt

Use Pydantic for strict input validation. In healthcare workflows, bad input should fail fast before it reaches the model.

from typing import Literal
from pydantic import BaseModel, Field
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI

class Transaction(BaseModel):
    transaction_id: str
    member_id: str
    provider_id: str
    amount: float = Field(gt=0)
    procedure_code: str
    region: str
    timestamp: str

class RiskAssessment(BaseModel):
    decision: Literal["allow", "review", "escalate"]
    risk_score: int = Field(ge=0, le=100)
    reason: str

llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)

prompt = ChatPromptTemplate.from_messages([
    ("system",
     "You are a healthcare transaction monitoring analyst. "
     "Follow policy text exactly. Do not expose PHI. "
     "Return only structured risk assessments."),
    ("human",
     "Transaction:\n{transaction}\n\nPolicy context:\n{policy_context}\n\n"
     "Assess this transaction for fraud, abuse, or compliance risk.")
])

3) Add policy retrieval with FAISS

The agent should ground decisions in internal policy text. This is where LangChain’s retriever pattern pays off.

from langchain_community.vectorstores import FAISS
from langchain_community.embeddings import OpenAIEmbeddings
from langchain_core.documents import Document

docs = [
    Document(page_content="Duplicate claims within 7 days require manual review."),
    Document(page_content="Out-of-region specialist billing requires preauthorization."),
    Document(page_content="High-cost procedures above $10,000 require secondary approval.")
]

embeddings = OpenAIEmbeddings()
vectorstore = FAISS.from_documents(docs, embeddings)
retriever = vectorstore.as_retriever(search_kwargs={"k": 2})

4) Build the LangChain pipeline and run an assessment

Use LCEL composition with RunnablePassthrough and structured output parsing. This keeps the flow explicit and production-friendly.

from langchain_core.runnables import RunnablePassthrough

def format_docs(retrieved_docs):
    return "\n".join(doc.page_content for doc in retrieved_docs)

chain = (
    {
        "transaction": RunnablePassthrough(),
        "policy_context": lambda tx: format_docs(retriever.get_relevant_documents(tx.model_dump_json()))
    }
    | prompt
    | llm.with_structured_output(RiskAssessment)
)

sample_tx = Transaction(
    transaction_id="txn_1001",
    member_id="mem_42",
    provider_id="prov_9",
    amount=12500,
    procedure_code="99285",
    region="US-WEST",
    timestamp="2026-04-21T10:15:00Z"
)

result = chain.invoke(sample_tx)
print(result.model_dump())

That pattern gives you a clean path:

  • validate input with Transaction
  • retrieve policy context with FAISS
  • ask the LLM for a structured decision using with_structured_output
  • emit a typed result you can store or route downstream

Production Considerations

  • Compliance controls

    • Strip PHI unless it is strictly required for the decision.
    • Use field-level minimization so the model sees codes and amounts instead of full clinical notes.
    • Log every decision with model version, prompt version, retrieved policy docs, and reviewer outcome.
  • Data residency

    • Keep embeddings, logs, and vector stores in the same regulated region as source data.
    • If your healthcare org requires on-prem or private cloud processing, use a deployment path that supports that boundary.
    • Never send raw claims payloads to external tools without a legal review.
  • Monitoring

    • Track false positives by provider type, region, and procedure code.
    • Measure latency separately for retrieval and model inference.
    • Alert on drift when new billing patterns start getting escalated unusually often.
  • Guardrails

    • Keep deterministic rules ahead of the LLM.
    • Cap escalation authority; the agent should recommend review, not approve payments autonomously.
    • Redact identifiers in analyst-facing explanations unless access is explicitly authorized.

Common Pitfalls

  1. Sending raw PHI into prompts

    • Avoid this by preprocessing records into minimal structured fields.
    • If notes are needed for context, redact names, dates of birth, addresses, and account numbers first.
  2. Letting the LLM decide everything

    • Don’t use the model as the first line of defense.
    • Run deterministic fraud/compliance rules before LangChain so simple violations never hit inference.
  3. Skipping auditability

    • In healthcare, “the model said so” is not an answer.
    • Persist retrieved documents, final output JSON, rule hits, timestamps, and human review outcomes so compliance teams can reconstruct every decision path.

Keep learning

By Cyprian Aarons, AI Consultant at Topiax.

Want the complete 8-step roadmap?

Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.

Get the Starter Kit

Related Guides