Tawa docs
Cookbook

Raw OpenAI (no framework)

Screen a prompt before an OpenAI call, without the Vercel AI SDK.

If you're calling the OpenAI client directly — no Vercel AI SDK — screen the prompt yourself before the call. If Tawa blocks or leans toward blocking, the request is rejected without ever reaching OpenAI.

import OpenAI from "openai";
import { TawaGuard } from "@tawaguard/sdk";

const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const tawa = await TawaGuard.create(process.env.TAWA_API_KEY!);

const BLOCKED_ACTIONS = new Set(["block", "hard_block", "soft_block"]);

async function chat(userMessage: string) {
  const scan = await tawa.analyze(userMessage);

  if (BLOCKED_ACTIONS.has(scan.action) || scan.suggestion === "block") {
    throw new Error("Message blocked by content policy.");
  }

  return openai.chat.completions.create({
    model: "gpt-4o",
    messages: [{ role: "user", content: userMessage }],
  });
}

On Next.js with the Vercel AI SDK? The Vercel AI SDK integration wraps this pattern for you — no manual check needed at every call site.