Skip to main content
The AI Gateway plugin system lets you write guardrail functions in TypeScript, register them in conf.json, and reference them in any config just like the built-in functions. Custom plugins follow the same hook model as the default plugin.

Plugin structure

Each plugin lives in its own subdirectory under plugins/:

Step 1 — Write the manifest

The manifest.json file declares the plugin identity, any credentials it needs, and the list of functions it exposes.
plugins/my-plugin/manifest.json

Manifest fields

Step 2 — Implement the handler

Create a TypeScript file that exports a handler function matching the PluginHandler type.
plugins/my-plugin/handler.ts

PluginHandler interface

The full TypeScript signature from plugins/types.ts:
plugins/types.ts
context contains:
  • context.request — the raw request body (available in beforeRequestHook)
  • context.response — the raw response body (available in afterRequestHook)
  • context.requestType — 'chatComplete', 'complete', 'embed', etc.
  • context.provider — the target provider name
  • context.metadata — metadata passed with the request
parameters contains the values the user configured in their guardrail config, plus a credentials key with the values from conf.json. options provides access to environment variables and a simple key-value cache.
For transformer plugins, set transformed: true and populate transformedData with the modified request body instead of returning a verdict.

Step 3 — Register the plugin

Add your plugin ID to the plugins_enabled array in conf.json at the repository root. If your plugin requires credentials, add them under the credentials key:
conf.json

Step 4 — Build the plugins

Run the build command to compile all enabled plugins into the gateway bundle:
Then start the gateway:

Step 5 — Use your plugin in a config

Reference your function as <plugin-id>.<functionId> in any guardrail config:
In Python:

Step 6 — Write tests

Create a Jest test file alongside your handler. The test command for plugins is:
A minimal test:
plugins/my-plugin/handler.test.ts

Contributing a plugin to the community

If you’d like to share your plugin with the Portkey community:
  1. Ensure your plugin has test coverage.
  2. Open an issue with the title [Feature] Your Plugin Name on GitHub.
  3. Submit a pull request with the title [New Plugin] Your Plugin Name.
Join the Discord community for support and discussion.