Skip to content

Building the Gateway with AI Policies

After writing a custom AI policy, build it into the AI Gateway image so it can run alongside the built-in guardrails and rate-limiting policies.

Install the AP CLI Tool

The ap CLI tool is used to build a custom gateway image with your own policies. This is the same ap CLI used for AI Workspace CI/CD (ap gateway apply, ap ai-workspace build, etc.) — if you already have it installed for that workflow, you can skip ahead to Configure the Build File. Otherwise, download the binary for your platform from the AP CLI releases page and follow the steps below to install it.

Step 1: Extract the binary

After downloading the zip file for your platform, extract it:

unzip ap-darwin-amd64-v0.7.0.zip   # replace with your downloaded filename

Step 2: Move the binary to a bin directory

mkdir -p ~/bin
mv ap ~/bin/

Step 3: Add to PATH

Add the following line to your ~/.zshrc or ~/.bashrc:

export PATH="$HOME/bin:$PATH"

Step 4: Reload your shell

source ~/.zshrc   # or source ~/.bashrc

Step 5: Verify the installation

ap --version

Step 1: Extract the binary

After downloading the zip file, right-click it and select Extract All, or run in PowerShell:

Expand-Archive -Path ap-windows-amd64.zip -DestinationPath ap-windows-amd64

Step 2: Move the binary to a bin directory

New-Item -ItemType Directory -Force -Path "$HOME\bin"
Move-Item ap-windows-amd64\ap.exe "$HOME\bin\ap.exe"

Step 3: Add to PATH

Run the following in PowerShell to permanently add ~/bin to your user PATH:

[Environment]::SetEnvironmentVariable("Path", $env:Path + ";$HOME\bin", "User")

Step 4: Reload your shell

Close and reopen PowerShell for the PATH change to take effect.

Step 5: Verify the installation

ap --version

Configure the Build File

The build.yaml file is included in the AI Gateway package you downloaded when setting up the AI Gateway. It declares the gateway version and the list of policies to include. Edit this file to add your custom AI policies before building the gateway image.

Sample Custom Policies

WSO2 provides a set of sample custom policies to help you get started, including AI-specific examples. You can find them in the api-platform sample policies repository.

Structure

version: v1
gateway:
  version: 1.2.0-beta
policies:
  - name: <policy-name>
    gomodule: <go-module-path>@<version>   # for policy hub managed policies
  - name: <custom-ai-policy-name>
    filePath: <relative-path-to-policy-dir> # for custom AI policies

Each policy entry uses one of two source types:

Field Description
gomodule Go module reference for policy hub managed policies (e.g., github.com/wso2/gateway-controllers/policies/pii-masking@v1)
filePath Relative path from build.yaml to a local custom AI policy directory

Adding a Custom AI Policy

Your custom AI policy can reside anywhere on the filesystem. Use a relative path from the build.yaml file to point to it.

For example, if your directory layout is:

my-ai-gateway/
├── build.yaml
└── ../my-ai-policy/   # policy lives outside the gateway directory
    ├── policy-definition.yaml
    └── myaipolicy.go

Add the policy to build.yaml using a relative filePath:

policies:
  - name: my-ai-policy
    filePath: ../my-ai-policy

Note

The path in filePath is always relative to the location of build.yaml, not the directory from which you run the ap command.

Build the Gateway Image

Once build.yaml is ready, run the following command from the directory containing build.yaml to build the custom gateway image:

ap gateway image build

This packages the gateway runtime together with all listed policies — built-in and custom — into a container image that can be used in place of the standard AI Gateway image.

Once the build completes, the output lists the two image names produced. For example:

✓ Built gateway images with 1 policies:
  • ghcr.io/wso2/api-platform/wso2apip-ai-gateway-1.2.0-beta-gateway-runtime:1.2.0-beta
  • ghcr.io/wso2/api-platform/wso2apip-ai-gateway-1.2.0-beta-gateway-controller:1.2.0-beta

A build-manifest.yaml file is also written alongside build.yaml, recording the resolved versions of all policies included in the build.

Update the Docker Compose File

After building, update the image: fields in your docker-compose.yaml to use the newly built images.

Locate the gateway-controller and gateway-runtime services and replace their image: values with the images from the build output:

services:
  gateway-controller:
    image: ghcr.io/wso2/api-platform/wso2apip-ai-gateway-1.2.0-beta-gateway-controller:1.2.0-beta  # (1)

  gateway-runtime:
    image: ghcr.io/wso2/api-platform/wso2apip-ai-gateway-1.2.0-beta-gateway-runtime:1.2.0-beta     # (2)
  1. Replace with the gateway-controller image name from your build output.
  2. Replace with the gateway-runtime image name from your build output.

Once updated, start the gateway as usual. api-platform.env continues to be loaded automatically via the Compose env_file: directive:

docker compose up

What's Next?