AI Guardian

Quickstart

Integrate AI Guardian in under 5 minutes. All you need to change is the base_url in your existing OpenAI client.

Prerequisites

  • An AI Guardian account (free tier available — no credit card required)
  • An AI Guardian API key (starts with aig_)
  • The OpenAI SDK for Python or Node.js

1. Get Your API Key

After signing up, go to Settings → API Keys in the AI Guardian dashboard and create a new key. Copy the key — it starts with aig_ and is only shown once.

2. Install Dependencies

You can use the standard OpenAI SDK — no additional packages needed:

Python
pip install openai
Node.js
npm install openai

3. Change the Base URL

Replace the OpenAI base_url with your AI Guardian endpoint. Everything else stays the same.

4. Make a Request

Python
from openai import OpenAI

client = OpenAI(
    api_key="aig_YOUR_API_KEY",          # Your AI Guardian key (aig_ prefix)
    base_url="http://localhost:8000/api/v1/proxy",  # AI Guardian endpoint
)

try:
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": "What is the capital of France?"}],
    )
    print(response.choices[0].message.content)

except Exception as e:
    # AI Guardian returns standard HTTP errors
    print(f"Error: {e}")
Node.js
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: "aig_YOUR_API_KEY",
  baseURL: "http://localhost:8000/api/v1/proxy",
});

try {
  const response = await client.chat.completions.create({
    model: "gpt-4o",
    messages: [{ role: "user", content: "What is the capital of France?" }],
  });
  console.log(response.choices[0].message.content);
} catch (error) {
  // AI Guardian returns standard OpenAI error shapes
  console.error("Error:", error);
}

5. Handle AI Guardian Responses

AI Guardian can return three different outcomes. Your code should handle all three:

  • 200 OKrequest was safe and forwarded. Response is the normal OpenAI completion.
  • 202 Acceptedrequest is queued for human review. Body contains review_item_id.
  • 403 Forbiddenrequest was blocked. Body contains risk_score and matched rules.
import httpx
from openai import OpenAI, APIStatusError

client = OpenAI(
    api_key="aig_YOUR_API_KEY",
    base_url="http://localhost:8000/api/v1/proxy",
)

try:
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": user_message}],
    )
    result = response.choices[0].message.content

except APIStatusError as e:
    body = e.response.json()
    code = body.get("error", {}).get("code")

    if code == "request_blocked":
        risk_score = body["error"]["risk_score"]
        result = f"Request blocked (risk score: {risk_score})"

    elif code == "queued_for_review":
        review_id = body["error"]["review_item_id"]
        result = f"Under review (ID: {review_id}). Check back later."

    else:
        raise

Next Steps