import { createGuard, isBudgetExceededError } from "llmbrake"; const guard = createGuard({ budgetUsd: (ctx) => (ctx.plan === "pro" ? 20 : 2), downgradeAt: 0.8, downgradeMap: { "gpt-5.5": "gpt-5.4-mini" }, }); const chat = openai.chat.completions; const create = guard.wrap(chat.create.bind(chat)); const res = await create( { model: "gpt-5.5", messages: [{ role: "user", content: "Hello" }] }, { customerId: user.id, plan: user.plan, feature: "chat" }, ); // at the cap → throws BudgetExceededError (isBudgetExceededError(e))
Wrap once
One line around your OpenAI-compatible create() call. Streaming usage is captured too. Same code as the README quickstart.