Technical Guide

    Writing a Custom Policy Enforcement Plugin for Kong AI Gateway: Effort and Tradeoffs

    A custom policy enforcement plugin for Kong AI Gateway is a reasonable choice when policy must be enforced at one gateway and your team can own the code long term. The plugin itself is the smaller part of the work. Policy logic, streaming behavior, latency, testing across Kong releases, ongoing maintenance and audit-ready evidence of each decision make up most of the effort. Use the criteria below to decide whether to build, extend or enforce policy in the execution path with a dedicated runtime layer.

    Reference architecture: where decisions and policy live

    1. 1

      Policy definition

      Versioned, declarative rules for prompts, responses and tool calls, owned by a named team.

    2. 2

      Decision point

      In-plugin logic or an external authorization service, chosen against latency and release cadence.

    3. 3

      Policy distribution

      How rules reach every gateway node, and how a bad rule is rolled back.

    4. 4

      Enforcement phases

      Which stage of the request and response lifecycle each check runs in.

    5. 5

      Evidence emission

      A record of input class, policy version, decision and time, written to durable storage.

    Decision criteria

    • Does all traffic to models, agents, tools and MCP servers pass through this gateway, with no bypass route?
    • Do built-in AI plugins cover most of the policy, leaving a gap small enough to extend rather than build?
    • Can the plugin evaluate streamed and large responses within the latency budget, and is the failure mode defined?
    • Is there a named owner for policy, plugin code, Kong upgrades and changes to models, providers and tools?
    • Is there a defined evidence record per decision, with retention, integrity and verification, and who builds it?
    • Must the same policy apply outside Kong, across agents, tools and other environments?

    What the plugin is actually responsible for

    A policy enforcement plugin turns the gateway into a policy enforcement point. NIST guidance on zero trust describes proxies in this role and says enforcement infrastructure should behave like a security kernel: always invoked, verifiable, and independent of application code. That is a useful test for a Kong plugin before any code is written.

    In practice the plugin has to inspect prompts before they reach a model, inspect responses on the way back, and act on tool calls that an agent issues. For each item it returns one of three outcomes: allow, redact or block. Each outcome needs defined behavior. A redaction needs rules for what is replaced and how the downstream consumer is told. A block needs a response the calling application can handle.

    The first scoping question is coverage. A plugin only enforces what passes through the gateway. If an agent or application can reach a model, tool or MCP server by another route, the plugin is not always invoked, and the policy has a gap that no amount of plugin quality closes.

    Engineering effort by workstream

    Plan the build as six workstreams rather than one plugin task. Before starting, check what the built-in AI plugins already cover. Extending existing behavior is usually cheaper than reimplementing it, and the remaining gap defines the real scope.

    Streaming is typically the hardest design question. To block or redact streamed output, the plugin must either buffer enough content to judge it or evaluate partial content as it arrives. Buffering adds delay and changes the user experience. Partial evaluation risks missing a sensitive value split across chunks. Large payloads add memory pressure in the request path.

    Latency needs a budget. Every check runs on the request path, so measure overhead under realistic load and decide whether a failed or slow check fails open or fails closed. NIST names rate limiting, timeouts and circuit breaking as controls against resource exhaustion, and they apply to the plugin's own behavior too. NIST also warns that one team's faulty configuration on a shared gateway can cause a shared-fate outage. A policy error in a plugin on a shared gateway has the same blast radius.

    • Plugin development: Implement against the Kong plugin framework and its language and PDK constraints, including configuration schema and error handling.
    • Policy logic: Detection rules, redaction behavior, block responses and exception handling for prompts, responses and tool calls.
    • Streaming and large payloads: Buffering strategy, chunk-boundary handling and memory limits.
    • Latency and resilience: Overhead benchmarks, timeouts, circuit breaking and a fail-open or fail-closed decision.
    • Testing and versioning: Unit, integration and system tests with known inputs, plus a compatibility check on each Kong upgrade.
    • Ongoing maintenance: Updates as models, providers and agent tool patterns change, with a named owner.

    Where the build approach strains

    Three areas tend to expose the limits of a single-gateway plugin.

    The first is evidence. A plugin enforces a decision, but it does not produce durable, audit-ready evidence of that decision by default. For a regulated enterprise, that means designing a record for every allow, redact or block outcome: what was evaluated, which policy version applied, what the decision was, and when. It also means integrity protection, retention, access control and a way to verify the record later. NIST describes gateways that log and monitor sensitive data flows, and verifiability is a stated property of enforcement infrastructure. Treat evidence as its own build item with its own owner.

    The second is coverage beyond one gateway. NIST describes a write once, enforce everywhere property, where policy spans environments instead of being written separately for each. A Kong plugin enforces policy for traffic through Kong. Agents, tools and MCP servers reached by other paths need separate enforcement, and separate copies of policy tend to drift.

    The third is consistency across models, agents and tools. Policy that differs by model or provider, or between prompt checks and tool-call checks, is hard to review and harder to defend. A central definition with uniform deployment is what NIST describes as a management-plane function, and a plugin does not supply it by itself.

    Build, extend or enforce in the execution path

    There are three realistic options. Building a new plugin gives full control over logic and behavior, and full ownership of every item above. Extending built-in AI plugins or an existing plugin narrows the code you own, but you still own the evidence design, the test matrix and the upgrade work. Enforcing policy in the execution path with a dedicated runtime layer moves enforcement, policy distribution and evidence out of the gateway, and adds a component to evaluate and operate.

    Trussed is one option in that third category. It enforces policy in the execution path across models, agents, tools and MCP servers, with allow, redact, block, rate limit and human-approval outcomes, and it generates audit-ready evidence from enforcement. It can run self-managed in the customer cloud with configurable retention and data residency. A plugin and a runtime layer are not mutually exclusive, and a Kong deployment can stay in place either way.

    None of these options is wrong in general. The right one depends on how much of the scope sits outside a single gateway and how much evidence the business must be able to produce.

    Scope of a custom policy enforcement plugin

    Inspect

    Prompts, responses, streamed output and tool calls in the request path.

    Decide

    Allow, redact or block against versioned policy.

    Operate

    Test, release and maintain across Kong, model and provider changes.

    Evidence

    Record each decision in a durable, verifiable form.

    Scope the decision with your own constraints

    If your policy has to cover more than one gateway or produce audit-ready evidence, review how enforcement in the execution path compares with a plugin build.

    Talk to an Expert
    Next step

    How does your AI governance program compare?

    See where your program has gaps in less than 2 minutes.