HIPAA legacy integration patterns for enterprise AI agents — Reference Architecture

Reference architecture · published 2026-10-16 · regulated enterprise · HIPAA · layout diagram_first · implementation executed in CI before publish

The control that resolves this is an Idempotent System-of-Record Bridge, evaluated before the request reaches a model provider rather than audited afterwards. In regulated enterprise the binding constraint is HIPAA, and the question it actually asks is can PHI reach a place the BAA does not cover. The specific problem here is mainframe integration. The shape below holds p95 ≤ 2400ms over scheduled batch file exchange (SFTP), and ships with a standard-library implementation that runs as written.

The request path

Idempotent System-of-Record Bridge — the request path, with the decision points that must exist before the first model call:

flowchart LR
    AG[Agent] --> PR[Projection<br/>allow-listed fields]
    PR -->|unknown field| REJ[Reject - loud]
    PR --> IK[Idempotency key<br/>sha256 of business keys]
    IK --> CL{Claim table}
    CL -->|already done| RP[Replay original result]
    CL -->|in flight| DUP[Refuse duplicate]
    CL -->|new| OB[(Outbox)]
    OB --> TR[Transport - OData / IDoc / RFC]
    TR --> SOR[(System of record<br/>SAP / ERP / AS400)]
    SOR --> RC[Reconciliation]
    RC -->|drift| CMP[Compensating action]
Request path. Rendered as a diagram where scripting is available; the Mermaid source above is the authoritative definition either way.

The parts you have to build

  1. Content-derived idempotency key — Derived from business keys, so two attempts at one intent collide.
  2. Claim table — Transactional claim in the same database as the outbox; a retry observes the first attempt.
  3. Typed write contract — Allow-listed fields per document type; unknown fields are rejected loudly.
  4. Outbox — Durable intent record, so a crash mid-flight is recoverable rather than ambiguous.
  5. Reconciliation job — Compares posted documents against intents and surfaces drift.
  6. Compensating action — A defined reversal path per document type, because some writes must be undoable.

Enterprise implementation parameters

Every value below is a parameter you can hold a vendor or an internal team to. Adjectives are not acceptance criteria.

Implementation parameters for this deployment shape
Deployment vectorOn-premises gateway adjacent to the system of record
Security standardHIPAA
Applicable regimesSOC 2 Type II, ISO/IEC 27001:2022, GDPR
Latency SLAp95 ≤ 2400ms — interactive surfaces need sub-second; batch jobs do not
Integration protocolscheduled batch file exchange (SFTP) — the legacy system of record exposes nothing else
Idempotency controla content hash per file plus a processed-files ledger
Throughput envelopea window, not a rate; the constraint is the window length
Middleware requirementpolicy decision point in front of the model gateway
Data classes in scopePII, commercial-in-confidence data
Retention obligationper the data class retention schedule, enforced automatically
Data residencycontracted processing region, asserted on every response
Contractual instrumentDPA plus a disclosed subprocessor list
Systems of recordSAP, Salesforce, Workday
Primary failure modea partial file processed as complete

Where the rollout actually stalls

Agents can read the legacy estate and cannot safely write to it. A retry after a gateway timeout creates a duplicate posting, and in an ERP a duplicate journal entry is a finance incident. So the team that owns the system of record withholds write access, and the automation stalls at read-only.

What makes mainframe integration specifically hard

The mainframe is the system of record and its change window is measured in quarters. Anything that requires a change on that side is not an integration plan, it is a request to join a release queue.

What resolves it is a side-car integration that requires zero mainframe change. The failure to design against: a design whose first step is a CICS change nobody will schedule.

What HIPAA specifically requires

HIPAA holds the covered entity responsible for PHI wherever it flows. A model provider without a signed BAA is not a permissible destination, and 'the prompt only contained a little PHI' is not a defence — the minimum necessary standard is itself a control.

The artifact that satisfies it is a de-identification record per request plus the BAA chain. The way teams most often fail it: PHI reaching a subprocessor with no BAA, via a logging sidecar nobody mapped.

Why this blocks a rollout rather than slowing it: The blocker is ownership risk, not capability. Until duplicate writes are structurally impossible and every field an agent can post is enumerated in a contract, no system-of-record owner will sign off — and they are right not to. In regulated enterprise the cost of getting it wrong is concrete — a failed Type II observation window restarts the clock.

Implementation artifact — Idempotent write-back with a typed write contract

Standard-library Python, no external dependencies, runs as written. Executed by the publish gate: if it raises, this page does not ship. Wire it behind scheduled batch file exchange (SFTP); the failure mode it guards against is a partial file processed as complete. The output you hand the assessor is the minimum-necessary de-identification log.

"""Idempotent write-back adapter for a legacy system of record (SAP/ERP shape).

Why this shape: the blocker on legacy integration is almost never reading. It
is writing. An agent that retries a posting because a gateway timed out will
happily create a duplicate journal entry, and in an ERP a duplicate posting is
a finance incident, not a bug report.

Three controls make agent-driven writes acceptable to the team that owns the
system of record:
  * a deterministic idempotency key derived from business content
  * a claim table, so a retry observes the first attempt instead of repeating it
  * a bounded, typed projection, so the agent can never post a field it does
    not understand
"""
import hashlib
import json
import time
from dataclasses import dataclass


class DuplicatePosting(Exception):
    """Raised when a retry would create a second document for one intent."""


def idempotency_key(doc_type: str, business_keys: dict) -> str:
    """Derived from BUSINESS content, never from a uuid4.

    A random key makes every retry unique, which is the opposite of what
    idempotency means. Two attempts to post the same invoice must collide.
    """
    canonical = json.dumps({k: business_keys[k] for k in sorted(business_keys)},
                           sort_keys=True, separators=(",", ":"))
    return f"{doc_type}:" + hashlib.sha256(canonical.encode()).hexdigest()[:32]


ALLOWED_FIELDS = {
    "journal_entry": {"company_code", "posting_date", "currency",
                      "debit_account", "credit_account", "amount", "reference"},
}


def project(doc_type: str, payload: dict) -> dict:
    """Allow-list projection. An agent-proposed field that is not in the
    contract is dropped loudly, not posted hopefully."""
    allowed = ALLOWED_FIELDS.get(doc_type)
    if allowed is None:
        raise ValueError(f"no write contract defined for {doc_type!r}")
    unknown = set(payload) - allowed
    if unknown:
        raise ValueError(f"fields outside the write contract: {sorted(unknown)}")
    missing = allowed - set(payload)
    if missing:
        raise ValueError(f"write contract requires: {sorted(missing)}")
    return {k: payload[k] for k in sorted(allowed)}


@dataclass
class ClaimStore:
    """Stands in for a real transactional table. In production this is a row
    in the SAME database as your outbox, claimed with SELECT ... FOR UPDATE."""

    def __post_init__(self):
        self._claims: dict = {}

    def claim(self, key: str, ttl: float = 900.0) -> tuple:
        """-> (is_new, existing_result). Atomic in a real DB."""
        now = time.time()
        rec = self._claims.get(key)
        if rec and (rec["status"] == "done" or now - rec["at"] < ttl):
            return False, rec
        self._claims[key] = {"status": "in_flight", "at": now, "result": None}
        return True, None

    def complete(self, key: str, result: dict):
        self._claims[key] = {"status": "done", "at": time.time(), "result": result}

    def fail(self, key: str):
        self._claims.pop(key, None)      # let a genuine retry through


def post_document(store: ClaimStore, doc_type: str, business_keys: dict,
                  payload: dict, transport) -> dict:
    """The only sanctioned path from an agent to the system of record."""
    key = idempotency_key(doc_type, business_keys)
    body = project(doc_type, payload)

    is_new, existing = store.claim(key)
    if not is_new:
        if existing["status"] == "done":
            # The correct response to a duplicate is the ORIGINAL result.
            return {"idempotency_key": key, "replayed": True,
                    **existing["result"]}
        raise DuplicatePosting(f"{key} is already in flight")

    try:
        result = transport(doc_type, body, key)
    except Exception:
        store.fail(key)
        raise
    store.complete(key, result)
    return {"idempotency_key": key, "replayed": False, **result}


if __name__ == "__main__":
    calls = []

    def fake_sap(doc_type, body, key):
        calls.append(key)
        return {"document_number": f"50000{len(calls)}", "posted": True}

    store = ClaimStore()
    keys = {"invoice_id": "INV-8831", "company_code": "1000"}
    payload = {"company_code": "1000", "posting_date": "2026-10-16",
               "currency": "EUR", "debit_account": "400000",
               "credit_account": "113100", "amount": "14500.00",
               "reference": "INV-8831"}

    first = post_document(store, "journal_entry", keys, payload, fake_sap)
    retry = post_document(store, "journal_entry", keys, payload, fake_sap)
    print("first :", first["document_number"], "replayed", first["replayed"])
    print("retry :", retry["document_number"], "replayed", retry["replayed"])
    print("transport invocations:", len(calls), "(must be 1)")
    try:
        project("journal_entry", {**payload, "tax_code": "V1"})
    except ValueError as e:
        print("contract rejects unknown field:", e)

The HIPAA control, as code

Minimum-necessary de-identification with a BAA-pinned egress gate. This is the artifact an assessor asks for under HIPAA specifically — it is not the same control as the one above, and a system that implements one does not thereby implement the other. It executes under the same publish gate.

"""Minimum-necessary de-identification with a BAA-pinned destination gate.

HIPAA holds the covered entity responsible for PHI wherever it flows, so the
control is not "we told people not to paste PHI". Two things must be
structural: PHI is reduced to the minimum necessary BEFORE egress, and the
destination set is pinned to those under a signed BAA, failing closed on
anything else.

Safe Harbor (45 CFR 164.514(b)(2)) lists 18 identifier classes. This implements
the mechanical subset and records a per-request de-identification receipt,
because the auditor asks for proof per request, not a policy document.
"""
import re

SAFE_HARBOR_PATTERNS = {
    "name_mrn": re.compile(r"\bMRN[:\s#]*([A-Z0-9-]{4,})", re.I),
    "ssn": re.compile(r"\b\d{3}-\d{2}-\d{4}\b"),
    "phone": re.compile(r"\b(?:\+1[-.\s]?)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}\b"),
    "email": re.compile(r"\b[\w.+-]+@[\w-]+\.[\w.]+\b"),
    "date_full": re.compile(r"\b\d{1,2}/\d{1,2}/(19|20)\d{2}\b"),
    "member_id": re.compile(r"\b(?:member|policy)\s*(?:id|#)[:\s]*([A-Z0-9-]{5,})", re.I),
}


class BAARegistry:
    """Destinations are permissible only as attested data, never by convention."""

    def __init__(self):
        self._signed: dict[str, str] = {}

    def attest(self, destination: str, agreement_ref: str) -> None:
        self._signed[destination] = agreement_ref

    def covered(self, destination: str) -> bool:
        return destination in self._signed

    def reference(self, destination: str) -> str | None:
        return self._signed.get(destination)


class PHIEgressGate:
    def __init__(self, registry: BAARegistry):
        self.registry = registry
        self.receipts: list[dict] = []

    def deidentify(self, text: str) -> tuple[str, dict[str, int]]:
        counts: dict[str, int] = {}
        out = text
        for label, pattern in SAFE_HARBOR_PATTERNS.items():
            out, n = pattern.subn(f"[REDACTED:{label}]", out)
            if n:
                counts[label] = n
        return out, counts

    def send(self, text: str, destination: str) -> dict:
        """Refuses rather than redacting-and-hoping when the BAA is absent."""
        redacted, counts = self.deidentify(text)
        phi_present = bool(counts)
        if phi_present and not self.registry.covered(destination):
            receipt = {"destination": destination, "admitted": False,
                       "reason": "no BAA on file for a PHI-classified payload",
                       "identifier_classes": sorted(counts)}
            self.receipts.append(receipt)
            raise PermissionError(receipt["reason"])
        receipt = {
            "destination": destination,
            "admitted": True,
            "baa_ref": self.registry.reference(destination),
            "identifier_classes": sorted(counts),
            "redactions": sum(counts.values()),
            "payload": redacted,
        }
        self.receipts.append(receipt)
        return receipt


if __name__ == "__main__":
    reg = BAARegistry()
    reg.attest("azure-openai-us-east", "BAA-2026-0041")
    gate = PHIEgressGate(reg)

    note = ("Patient MRN: 88213A, contact 555-867-5309 or a.patient@example.com, "
            "DOB 04/11/1974, follow-up scheduled.")
    ok = gate.send(note, "azure-openai-us-east")
    assert ok["admitted"] and ok["redactions"] >= 4, ok
    assert "88213A" not in ok["payload"] and "555-867-5309" not in ok["payload"], ok
    assert "a.patient@example.com" not in ok["payload"], ok

    try:
        gate.send(note, "some-vendor-eval-endpoint")
        raise AssertionError("PHI reached a destination with no BAA")
    except PermissionError:
        pass

    # No PHI means no BAA requirement — the gate must not over-block.
    benign = gate.send("Summarise ward occupancy trends.", "some-vendor-eval-endpoint")
    assert benign["admitted"] and benign["redactions"] == 0, benign

    print("HIPAA minimum-necessary egress gate: OK")
    print(f"  receipts {len(gate.receipts)}, "
          f"refusals {sum(1 for r in gate.receipts if not r['admitted'])}")

Risk mitigation matrix

Each row carries an acceptance parameter rather than an adjective, because "low latency" cannot be signed off. Retention here is bound by per the data class retention schedule, enforced automatically.

CategoryRiskControlAcceptance parameter
HIPAAPHI egresses to a provider or subprocessor outside the BAA chainClassify and de-identify before egress; pin destinations to the BAA-covered set and fail closed on anything else0 requests to non-BAA destinations; de-identification applied pre-egress on 100% of PHI-classified payloads
mainframe integrationIntegration requires a change inside the mainframe release windowConfine all new logic to the side-car; treat the mainframe interface as immutable0 mainframe-side changes required to ship; verified in design review
OperationalRetry after timeout creates a duplicate documentBusiness-content idempotency key plus a transactional claim; retries replay the original resultInduced timeout + retry produces 1 document and 1 transport call
OperationalAgent posts a field nobody contracted forAllow-list projection per document type; reject unknown and require complete fields0 writes containing fields outside the contract; enforced in tests
OperationalCrash mid-flight leaves an ambiguous stateDurable outbox written before transport; reconciliation resolves on restartKill-during-post leaves 0 ambiguous records after reconciliation
Data privacyLegacy extracts pull more fields than the use case needsProjection on read as well as write; field-level justification recordedExtract schema matches the documented minimum field set
Latency SLASynchronous legacy calls stall the agent loopAsync submit with a claim handle; agent polls or is called backAgent p95 independent of the system-of-record response time
Cost controlRe-extracting the full estate on every runChange-data capture or a watermark; full extract only on schema changeIncremental run under 10% of full-extract cost
OperationalNo defined reversal for a wrong postingA compensating action per document type, rehearsed in a sandbox clientEvery writable document type has a tested reversal path

Control questions before sign-off

Related architectures

Enterprise infrastructure review

If you are carrying this blocker, the useful next step is not a demo. It is a review of your actual request path against the control points above:

  • Which of the 6 components exist in your estate today, and which are assumed
  • Your current numbers against the 7 acceptance parameters, starting with p95 ≤ 2400ms
  • Which HIPAA control questions you cannot yet answer, and what closes each gap
Request an architecture review