Search documentation

Search all documentation pages

OpenAI Integration

Evaluate OpenAI GPT model requests and responses against your policies.

Overview

The OpenAI integration acts as a proxy between your application and the OpenAI API. Requests pass through Aguardic for policy evaluation before reaching OpenAI. If a request violates a policy, it can be blocked before it ever leaves your infrastructure.

The proxy supports all OpenAI API endpoints. Policy evaluation is triggered on completion endpoints (/v1/chat/completions and /v1/completions). Other endpoints (models, embeddings, etc.) pass through without evaluation.

Setup

1. Create an OpenAI Integration

Navigate to Integrations in the Aguardic dashboard, click Add Integration, and select OpenAI. Provide your OpenAI API key and give the integration a name.

Aguardic stores your OpenAI API key securely (encrypted with AES-256-GCM). When you create the integration, you receive a proxy URL and an Aguardic API key.

Store your Aguardic API key securely. It is shown only once. If you lose it, revoke it and create a new one.

2. Bind Policies

Go to Policy Bindings and bind your governance policies to the OpenAI integration. These policies will be evaluated against every completion request that passes through the proxy.

3. Replace Your Base URL

Point your application at the Aguardic proxy URL instead of the OpenAI API directly.

Proxy URL

https://api.aguardic.com/v1/integrations/openai/proxy/{integrationId}

Replace {integrationId} with the ID returned when you created the integration.

Never expose the proxy URL in client-side code. The proxy should only be called from your server to prevent API key leakage.

How the Proxy Works

  1. Your application sends a request to the Aguardic proxy URL
  2. Aguardic extracts the text content from the request (messages, system prompt)
  3. Input evaluation: Content is evaluated against all bound policies
  4. If BLOCK: Returns 403 with violation details. The request never reaches OpenAI.
  5. If APPROVAL_REQUIRED: Returns 403 with a review request ID for polling.
  6. If ALLOW or WARN: The request is forwarded to OpenAI with your stored API key
  7. The OpenAI response is streamed back to your application
  8. Output evaluation: For non-streaming responses (or buffered streaming up to 1MB), the response content is also evaluated asynchronously

Code Example

Since Aguardic stores your OpenAI API key, you authenticate to the proxy with your Aguardic API key:

import OpenAI from "openai";
 
const openai = new OpenAI({
  apiKey: process.env.AGUARDIC_API_KEY, // Your Aguardic API key
  baseURL:
    "https://api.aguardic.com/v1/integrations/openai/proxy/YOUR_INTEGRATION_ID",
});
 
// Use the OpenAI SDK as normal
const completion = await openai.chat.completions.create({
  model: "gpt-4o",
  messages: [{ role: "user", content: "Summarize our Q4 earnings report" }],
});
 
console.log(completion.choices[0].message.content);

Or with curl:

curl -X POST https://api.aguardic.com/v1/integrations/openai/proxy/YOUR_INTEGRATION_ID/v1/chat/completions \
  -H "Authorization: Bearer ag_live_abc123def456" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [
      { "role": "user", "content": "Summarize our Q4 earnings report" }
    ]
  }'

Input Evaluation

Before forwarding to OpenAI, Aguardic extracts text from your request for policy evaluation:

  • Chat completions: All message content (system, user, assistant) is concatenated and evaluated
  • Legacy completions: The prompt field is evaluated
  • Multimodal requests: Text parts are extracted from content arrays. Image and file presence is detected and available as metadata (hasImages, hasFiles, fileTypes)

The evaluation input also includes metadata: the model name, endpoint path, and whether streaming is enabled.

Output Evaluation

After OpenAI responds, Aguardic evaluates the output content asynchronously:

  • Response body is buffered during streaming (up to 1MB)
  • Once complete, the assistant's response text is extracted and evaluated against bound policies
  • Output violations are logged in the Aguardic dashboard but do not block the response (the response has already been streamed to the client)
  • Output evaluation does not count against your evaluation quota -- it is part of the same request

Streaming Support

Streaming responses (stream: true) are fully supported. Aguardic streams chunks to your application in real time while buffering for post-response output evaluation. If the response exceeds 1MB, output evaluation is skipped.

Error Responses

When a request is blocked by policy, the proxy returns a 403 response in OpenAI's error format:

{
  "error": {
    "message": "Request blocked by policy. 2 violation(s) detected.",
    "type": "policy_violation",
    "code": "request_blocked",
    "run_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
    "review_request_id": null,
    "poll_url": null
  }
}

When approval is required:

{
  "error": {
    "message": "Request held for review. 1 violation(s) detected.",
    "type": "policy_violation",
    "code": "approval_required",
    "run_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
    "review_request_id": "r1e2v3i4-e5w6-7890-abcd-ef1234567890",
    "poll_url": "/v1/reviews/r1e2v3i4-e5w6-7890-abcd-ef1234567890"
  }
}

Enforcement Modes

  • BLOCK -- Returns 403 before the request reaches OpenAI. The request is never sent.
  • APPROVAL_REQUIRED -- Returns 403 with a review request. The request is held until approved.
  • WARN -- Forwards the request to OpenAI. Violations are logged in Aguardic for review.
  • MONITOR_ONLY -- Forwards the request to OpenAI. Violations are logged silently.

Next Steps