Tuesday, 24 November 2026

Shola Jegede
← All packages

Developer tools

kinde-ai-policy

Decide which AI models, output limits and budgets each Kinde user, organization or agent gets, and hand the decision to your AI gateway. Verifies the Kinde token, picks the plan, reserves budget before the call and settles it after.

npm install kinde-ai-policy
—downloads last week
—downloads last 12 months
v0.1.0latest, Nov 24, 2026
2releases since Nov 24, 2026

Downloads

Download history

Loading download history…

Build this

Build with kinde-ai-policy

  1. 1

    Install

    bash
    npm install kinde-ai-policy

    It needs Node.js 20 or later and runs on edge runtimes that have fetch and Web Crypto. Its only dependency is jose.

  2. 2

    Quick start

    Define the policy:

    ts
    // ai-policy.ts
    import { definePolicy } from "kinde-ai-policy";
    
    export const policy = definePolicy({
      plan: { from: [{ flag: "plan" }], fallback: "free" },
      plans: {
        free: {
          models: ["openai/gpt-5-mini"],
          maxOutputTokens: 1024,
          budgets: [{ scope: "org", usd: 2, period: "month" }],
        },
        pro: {
          models: ["anthropic/claude-sonnet-4.5", "openai/gpt-5-mini"],
          fallbacks: ["openai/gpt-5-mini"],
          maxOutputTokens: 8192,
          budgets: [
            { scope: "org", usd: 100, period: "month" },
            { scope: "user", usd: 20, period: "month" },
          ],
        },
      },
      grants: [{ permission: "ai:premium-models", addModels: ["anthropic/claude-opus-4.1"] }],
    });

    Then use it in any route that calls a model. This one forwards to an OpenAI-compatible endpoint:

    ts
    import { createAiPolicy } from "kinde-ai-policy";
    import { openaiCompatible } from "kinde-ai-policy/openai-compatible";
    import { policy } from "./ai-policy";
    
    const ai = createAiPolicy({
      policy,
      issuer: process.env.KINDE_ISSUER_URL!, // https://yourbusiness.kinde.com
      audience: process.env.KINDE_AUDIENCE!, // the API audience registered in Kinde
      pricing: {
        "openai/gpt-5-mini": { input: 0.25, output: 2 }, // USD per million tokens
        "anthropic/claude-sonnet-4.5": { input: 3, output: 15 },
        "anthropic/claude-opus-4.1": { input: 15, output: 75 },
      },
    });
    
    const gateway = openaiCompatible({ baseUrl: "https://openrouter.ai/api/v1", apiKey: process.env.OPENROUTER_API_KEY });
    
    export async function POST(req: Request) {
      const token = req.headers.get("authorization")?.replace(/^Bearer /, "");
      const decision = await ai.decide({ token });
      if (!decision.allowed) return ai.toResponse(decision);
    
      const call = await ai.prepare(decision, await req.json(), gateway);
      if (!call.allowed) return ai.toResponse(call);
    
      try {
        const res = await fetch(call.url!, { method: "POST", headers: call.headers, body: JSON.stringify(call.body) });
        const json = await res.json();
        await (res.ok ? call.settle(json) : call.release());
        return Response.json(json, { status: res.status });
      } catch (error) {
        await call.release();
        throw error;
      }
    }

    If your framework has already verified the token, pass its claims instead with ai.decide({ claims }), and leave out issuer and audience.