Blog

    Agent Gateway Pattern: An API Layer Between the EHR and AI Agents

    By GauravAugust 21, 20269 mins read
    On this page

    The agent gateway pattern puts one API layer between AI agents and the EHR. The gateway checks which agent is calling, writes an audit row for every call, forwards only the tools on an allow-list, and holds the EHR credentials itself, so the agent never does.

    Written for digital health founders and hospital CTOs connecting agents to clinical systems, it covers identity, EHR authentication, allow-list entries, on-behalf-of calls, refusals, tests and ownership.

    What is the agent gateway pattern for EHR AI agents?

    The agent gateway pattern is a single API layer in front of the EHR that every AI agent must call. It checks the agent's identity, writes an audit row, and forwards only what the allow-list names. Agnotic Technologies issues the EHR credential to the gateway, never to the agent, and stops any call for an unlisted tool.

    Putting agents on an EHR without handing them credentials?

    We put identity, audit, and the allow-list in one gateway. The agent never holds the EHR secret.

    45 CFR 164.312(b) requires mechanisms that record and examine activity in systems that contain or use electronic protected health information.

    • Identity: which agent, which version, which human owner. Unique user identification still applies.
    • Audit: the tool, the resource type, the time, the result. Not the chart in the log line.
    • Allow-list: named tools only. A new tool is a change to the list, by the owner.
    • Credentials: on the gateway. Never in the agent's environment, prompt, or memory.

    A general HTTP router or a FHIR facade solves different problems. The agent gateway exists for one job: make every agent call to the EHR identifiable, auditable and limited to an approved list.

    What must the agent never hold?

    The agent must never hold EHR client secrets, refresh tokens, or a direct base URL that bypasses the gateway. Agnotic rotates those on the gateway and does not put them in the agent runtime.

    • Never in the agent: EHR secret, private key, unscoped token.
    • Never: a second path around the gateway 'just for debugging'.
    • Always: deny by default when the tool name is unknown.

    How does the gateway know which agent is calling?

    The gateway knows the caller from a credential issued to one agent deployment, not to a team or a shared service account. Agnotic gives each agent its own client identity at the gateway, tied to a build version and a named human owner.

    45 CFR 164.312(d) requires procedures to verify that a person or entity seeking access to electronic protected health information is the one claimed.

    • One credential per agent deployment. Two agents never share one, even when the same team runs both.
    • Version pinned at registration. A new build presents an updated record, so the audit row names the build that acted.
    • Owner recorded with the identity. When the owner leaves, the identity is reassigned or disabled, not left running.
    • Short-lived gateway tokens. The agent refreshes against the gateway, never against the EHR.

    If the gateway cannot tell two agents apart, the audit trail cannot either.

    How should the gateway authenticate to the EHR?

    The gateway should authenticate as a registered backend client, using the method the EHR documents for system-level access, with the key in a secrets manager only it can read. Where the FHIR API supports it, that means SMART Backend Services and a signed JWT assertion.

    The SMART Backend Services specification has the client authenticate with a signed, one-time-use JWT and says access tokens issued under it are short-lived, with expires_in that should not exceed 300 seconds.

    • Private key: in the secrets manager, readable by the gateway process alone, rotated on a schedule the security owner sets.
    • Access token: requested by the gateway, cached in gateway memory, never returned to the agent.
    • Scopes: the narrowest set that covers the allow-list, so the EHR's own check backs up the gateway's.
    • Separate registrations per environment, so a sandbox agent cannot reach live data.

    What does one entry on the allow-list contain?

    One entry names a tool, the single EHR interaction behind it, and the limits on that interaction. Agnotic writes entries narrow enough that a reviewer can read one line and know what data can leave the EHR.

    • Tool name: the string the agent sends, such as get_active_medications.
    • Interaction: the HTTP method and FHIR path, for example a search on MedicationRequest.
    • Allowed parameters: the search parameters the gateway will forward. Anything else is stripped or refused.
    • Patient binding: whether the call must carry a patient context, and where that context comes from.
    • Write flag: read-only by default. A write tool is its own entry with its own owner sign-off.
    • List version: stamped on every audit row, so a past call can be judged against the list in force at the time.

    Parameters matter more than teams expect. A read on one resource can pull linked resources through _include or _revinclude if the gateway forwards the query string untouched.

    How should the gateway handle a call made on behalf of a clinician?

    The gateway should carry the clinician's identity alongside the agent's and allow the call only when both are permitted. An agent acting for a nurse must not see more than that nurse could see in the EHR.

    • Two identities on the request: the agent's client identity and the signed-in user's unique id from the identity provider.
    • Two checks: the tool is on this agent's list, and the user's role allows the underlying read.
    • Two fields in the audit row, so a reviewer can tell an agent acting alone from one acting for a person.

    When no user is present, such as an overnight batch job, the list entry says so explicitly. A missing user id on a tool that requires one is a refusal, not a quiet fallback to agent-only access.

    What should the gateway return when it refuses a call?

    It should return a short, machine-readable refusal with a reason code and nothing from the EHR. Agnotic keeps refusals plain on purpose: the agent learns the call failed, not how to get around the rule.

    • unknown_tool: the name is not on the list for this agent.
    • parameter_not_allowed: the tool exists, but the request asked for more than the entry permits.
    • user_not_permitted: the on-behalf-of user lacks rights to the underlying data.
    • upstream_unavailable: the EHR did not answer. A read may be retried later, but the agent must not loop.

    Every refusal still writes an audit row with its outcome. A rise in unknown_tool refusals usually means a prompt or model change is asking for tools nobody approved.

    A worked example: one tool call from a discharge summary agent

    A worked example: a hospital wants an agent that drafts discharge summaries for nurses to review. The agent needs active medications for the patient on screen, and nothing more for its first release.

    Agent tool call through the gateway: the agent sends a tool name with its gateway token, the gateway checks identity and the allow-list and writes an audit row, gets a short-lived EHR token with a signed JWT, and runs a scoped FHIR search whose result returns without the token
    • Step 1, the agent sends get_active_medications with the patient context and its gateway token.
    • Step 2, the gateway validates the agent identity and the nurse's user id carried with the request.
    • Step 3, the gateway matches the tool to its list entry and drops a query parameter the entry does not allow.
    • Step 4, the gateway uses its own short-lived EHR token to run the MedicationRequest search.
    • Step 5, the gateway writes the audit row: agent, version, user, tool, resource type, outcome and list version.
    • Step 6, the result returns to the agent. The EHR token never does.

    When the team later wants allergy data in the summary, that is a new list entry with an owner, not an edit to the prompt.

    What are the common mistakes with an agent gateway?

    The first mistakes are almost always shortcuts from a pilot that nobody removed. Each one quietly reopens a path the gateway was built to close.

    • Shared credential: several agents use one gateway identity, so the audit row cannot say which one acted.
    • Debug bypass: a direct EHR base URL left in an agent's config from early testing.
    • Prefix matching: the list accepts anything that starts with a tool name, so a renamed tool slips through.
    • Pass-through query strings: include parameters pull linked resources the entry never named.
    • Body logging: the gateway writes full EHR responses into its own logs, creating a second copy of the chart.
    • Retry storms: an agent retries a timed-out write, and the EHR receives the same order twice.

    How do you test the gateway before an agent goes live?

    You test the refusals as hard as the successes, because a gateway that allows everything also passes every happy-path test. Agnotic runs these checks in the EHR vendor's sandbox first, then in the customer's non-production environment.

    • Unknown tool, extra parameter and missing user id each return the right reason code.
    • A scan of the agent's container image, environment and config finds no EHR secret and no EHR URL.
    • Every call in the test run has exactly one audit row, including refused calls.
    • Token expiry at the gateway is handled without the agent noticing or ever receiving the new token.
    • A user without rights to a record is refused even though the tool is on the list.

    How can you tell the gateway is working in production?

    You can tell it is working when every API call the EHR records for the gateway's client traces back to a gateway audit row, and no agent traffic reaches the EHR any other way. That reconciliation is the strongest signal available.

    • Reconcile: where the EHR exposes access records for API clients, they match the gateway's audit rows.
    • Watch refusals by reason code per agent, not only overall error rates.
    • Alert when an agent identity calls a tool it has never called before.
    • Review each list change against the audit trail at release time.

    Who owns the gateway, the list and the credentials?

    Separate parties own separate parts, and the design holds only when nobody owns all of them. Agnotic agrees this split with the client before the first tool goes live.

    • Platform team: the gateway code, uptime, refusal behaviour and the audit store.
    • Agent owner: the allow-list entries for their agent and the reason each one exists.
    • Security owner: the EHR client registration, the private key and its rotation.
    • Health system EHR analysts: enabling the backend client and approving its scopes on their side.

    How does Agnotic place the gateway?

    Agnotic builds the gateway for the client: one identity per agent deployment, backend authentication to the EHR, the allow-list with named owners, the refusal codes and the audit store, tested in the vendor sandbox first.

    One gateway, credentials stay, list is explicit

    The agent calls us. The EHR sees the gateway. The list is the policy in force.

    Related reading on Agnotic

    What the agent may do, the signature before any tool is connected, FHIR facades and EHR integration.

    Sources

    Frequently Asked Questions

    No. Tokens and secrets stay on the gateway. The agent holds a credential to the gateway only.

    Talk to an engineer about your project

    Name the tools the agent may call. We will put identity, audit, and the allow-list in one gateway and keep EHR credentials off the agent.

    Go deeper

    Services and solutions on this topic