Skip to main content
Portkey is an AI gateway that routes model requests to providers and handles credentials, retries, caching, and logging. Its Bring Your Own Guardrails (BYOG) webhook can call TrustGuard before a provider request and after a model response. Portkey and TrustGuard use different webhook contracts, so the integration requires a small adapter. Portkey sends a hook event and expects a boolean verdict; the adapter sends the relevant request or response content to POST /v1/evaluate and maps TrustGuard’s status to that verdict. Only traffic routed through a Portkey configuration is evaluated. Direct calls to a model provider bypass this integration.

Integration capabilities

Before you start

Start with the policy in Observe mode. Findings appear in Activity without affecting traffic. Switch to Enforce after reviewing the results. See Policies.

1. Implement the webhook adapter

Portkey sends a JSON object containing request, response, metadata, and eventType. For beforeRequestHook, evaluate request.json as input. For afterRequestHook, evaluate response.json as output. Use the corresponding text field when a normalized JSON body is not available. The following function shows the contract conversion. Deploy the equivalent in your webhook service and return its result as JSON:
Do not point default.webhook directly at /v1/evaluate. Portkey’s hook event contains top-level fields that the Evaluate API does not accept, and the Evaluate API returns status rather than Portkey’s required verdict. The sample maps only block to verdict: false. It allows allow, report, ask, and transform. It does not return transformedData, so redaction is not supported. Use Block rules for enforcement.

2. Configure the BYOG hooks

Add default.webhook to both hook phases. Replace the example adapter URL and token with your values:
deny: true makes a failed check return HTTP 446. async: false keeps the check in the request path so Portkey can enforce its result. With only the before-request hook, Output-phase rules do not run. Prefer a saved Portkey configuration when the webhook header contains a secret. An inline configuration sent through x-portkey-config exposes that value to the calling application.

3. Send consumer and session identifiers

Pass verified identifiers through Portkey metadata so the adapter can populate consumer_id and session_id:
Portkey forwards this object to the webhook as event.metadata. _user is Portkey’s documented user identifier, and session_id is its recommended key for grouping related requests.

4. Configure failure behavior

Portkey’s webhook timeout defaults to 3000 ms. The example raises it to 5000 ms and sets failOnError: true, so a timeout or non-200 adapter response fails the check. With deny: true, Portkey returns 446 instead of calling the provider or returning the completion. Without failOnError: true, Portkey allows the request when the webhook times out or returns an error. Use that setting only when you deliberately want to fail open. The adapter should return a non-200 response when TrustGuard is unavailable or rejects its credentials. Returning { "verdict": true } in those cases makes the integration fail open regardless of the Portkey setting.

5. Verify

Attach the configuration to a request using your existing Portkey client or the x-portkey-config header. With a blocking rule in Enforce mode:
  1. Send a prompt that triggers the rule.
  2. Confirm that Portkey returns HTTP 446 and that hook_results contains the trustguard-input hook and default.webhook check.
  3. Confirm that the adapter’s data appears under the check’s responseData, including the TrustGuard trace_id.
  4. Find the same trace_id in TrustGuard Activity under the expected consumer_id and session_id.
Then send a request that passes and triggers an Output-phase rule. A blocked output also returns 446, but the provider has already processed the request.

Reference

Coverage

The adapter evaluates Portkey’s normalized request or response body. Tool definitions and model-selected tool calls are available only when Portkey includes them in that body, and a block stops the complete request or response, not an individual tool event. The adapter does not apply transformed payloads. Portkey does not support blocking output guardrails on streamed responses, so use non-streaming responses when output enforcement is required. A turn with both synchronous hooks makes two TrustGuard evaluations. Each hook also passes through the adapter, so include both network legs in the timeout and latency budget.

What is evaluated

Configuration

Attributes

  • consumer_id: map it from the _user value in verified Portkey metadata. Per-consumer policy routing and gates matching consumer.id depend on this value.
  • session_id: map it from a stable, verified session_id value in Portkey metadata. Without it, related turns may not be grouped together in Activity.
  • attributes.model.name and attributes.model.provider: the adapter can read these from request.json.model and Portkey’s provider field.
  • trace_id and request_id: return them in the webhook response’s data object. Portkey records that object under hook_results[].checks[].data.responseData.
Do not trust caller-supplied metadata for policy decisions unless your authentication layer validates or replaces it.

Troubleshooting

Experimental: a native neuraltrust plugin is proposed in Portkey-AI/gateway#1772, but it is not part of a released Portkey version.