The sandbox is the same one that Kong Gateway uses, but AI Gateway generates the resources that your code runs against.
If you already use sandboxed Lua in Kong Gateway, the following differences apply to AI Policies.
The Kong Gateway sandboxing reference lists entries that AI Gateway doesn’t allow.
The following functions are nil in strict mode:
kong.client.get_identity_realm_source, kong.client.set_identity_realm_source, and kong.client.get_principal
kong.request.get_id, kong.request.get_raw_forwarded_path, and kong.request.get_raw_headers
kong.table.new
The standard table.new function is available in place of kong.table.new.
kong.router.get_route and kong.router.get_service are also nil in strict mode. In AI Gateway, they are available only in lax mode.
AI Gateway doesn’t allow the following modules in strict mode or lax mode:
- Listed under strict mode in the Kong Gateway reference:
MessagePack, binaryheap, ffi-zlib, kong.tools.glob, kong.tools.header, kong.tools.header-mapping, kong.tools.hostname, kong.tools.stack, lua_pack, luaxxhash, lxp, multipart, pl.url, resty.snappy, and xml2lua
- Listed under lax mode in the Kong Gateway reference:
kong.enterprise_edition.tools.redis.v2.config_utils, kong.tools.oauth-client, kong.tools.redis.config_utils, and kong.tools.solace
AI Gateway saves an AI Policy that calls one of these functions in strict mode without an error, and the call fails when a request runs it.
To read the Kong Identity Principal of a request, use a condition with the principal.id field instead.
AI Gateway generates Routes and Services from your AI entities.
In lax mode, PDK calls that return a Route or a Service, such as kong.router.get_route and kong.router.get_service, return those generated resources, not your AI Model, AI Agent, or AI MCP Server.
The same applies to kong.db.routes, kong.db.services, and the kong.service.set_* calls.
In strict mode, kong.router isn’t available, as described in Differences in the allowlists.
The generated names depend on the type of entity:
- AI Model:
kong.router.get_route returns a Route named for the provider endpoint, such as openai-chat, and kong.router.get_service returns a Service named ai-gateway.
AI Models that resolve to the same provider endpoint share one Route, so the Route name doesn’t identify a single AI Model.
- AI Agent and AI MCP Server:
kong.router.get_service returns a Service with the same name as the entity.
kong.router.get_route returns a Route with the entity name followed by -route, such as my-agent and my-agent-route.
The generated Routes and Services have no tags.
An AI Consumer becomes a Kong Gateway Consumer with the AI Consumer’s name as its username.
For an authenticated AI Consumer, kong.client.get_consumer returns a table with id and username.
The table includes custom_id and tags when the AI Consumer sets them.
The tags list contains the AI Consumer’s labels as key:value strings.
kong.client.get_consumer_groups returns a table of the AI Consumer Groups that the AI Consumer belongs to, and each entry has a name.
It returns nil when the AI Consumer has no groups.
An oauth AI Consumer behaves the same way.
AI Gateway matches the token claim in config.consumer_claims to the AI Consumer’s custom_id.
kong.client.get_consumer then returns that AI Consumer’s id, username, custom_id, and tags.
When you enable Kong Identity Principal mapping (config.principals) on an openid-connect AI Auth Strategy, set config.consumer_optional to true if the Principal has no linked AI Consumer.
Otherwise, AI Gateway rejects the request with a 401, even when the Principal matches.
When the Principal has no linked AI Consumer, kong.client.get_consumer returns nil.
A Pre-Function runs before authentication, so kong.client.get_consumer returns nil there.
A Post-Function runs after authentication, so use a Post-Function to read the Consumer.
A request without a valid key fails authentication, and the AI Policy doesn’t run.
The AI Policies that run during a request share the kong.ctx.shared table.
A global Pre-Function can write a value that a Post-Function scoped to an AI Model reads in the same request.
The AI Gateway Pre-Function and Post-Function AI Policies accept only a field for each phase:
certificate
rewrite
access
header_filter
body_filter
log
ws_handshake
ws_client_frame
ws_upstream_frame
ws_close
The last four fields run in the WebSocket phases.
The Pre-Function and Post-Function AI Policies don’t accept the deprecated config.functions field from the Kong Gateway Pre-Function plugin.
The Exit Transformer AI Policy takes its code in config.functions, a list of functions.
You can apply Pre-Function and Post-Function AI Policies to an AI Model, AI MCP Server, AI Agent, or globally.
You can’t apply them to an AI Consumer or an AI Consumer Group.
You can’t apply an Exit Transformer AI Policy to an AI Consumer Group.
See AI Policy scopes.
Konnect accepts a Pre-Function or Post-Function AI Policy attached to an AI Consumer or an AI Consumer Group, but the data plane rejects the configuration with in 'consumer': value must be null or in 'consumer_group': value must be null.
The data plane then applies no further configuration changes until you remove the AI Policy from the AI Consumer or AI Consumer Group.
An AI Policy scoped to an AI Model runs only when the model field of the request body names that AI Model.
A request that names a different model, such as the provider’s own model name, doesn’t run the AI Policy.
If several global Pre-Function AI Policies exist, only the one with the highest ID takes effect.
AI Gateway ignores the others without reporting an error.
An Exit Transformer AI Policy rewrites responses that other AI Policies send, including authentication 401 responses and responses that a Pre-Function or Post-Function returns with kong.response.exit.
Each function receives the status code, the body, and the headers, and returns the new values.
By default, it doesn’t rewrite responses that AI Gateway sends for unknown conditions, such as the 404 for a request that matches no Route.
Set handle_unknown and handle_unexpected to true to rewrite those too.
Unlike the Kong Gateway Pre-Function and Post-Function plugins, the AI Gateway Pre-Function and Post-Function AI Policies support the condition field.
When the condition evaluates to false, AI Gateway skips the AI Policy.
See AI Policy conditions.