# LangWatch AI Gateway budgets and keys notifications on your phone

> Get a push notification when a LangWatch AI Gateway budget nears or hits its limit, or a virtual key is disabled, revoked or rotated.

- Company: LangWatch (https://www.justpush.io/recipes/langwatch)
- Event: `gateway.budget.breached` (Gateway events)
- Tags: AI, Monitoring
- Install: https://studio.justpush.io/recipes/langwatch/gateway
- Web page: https://www.justpush.io/recipes/langwatch/gateway

## Setup

1. Click Install in Studio and sign in. The recipe is added to your account and you get a personal webhook URL.
2. Follow the setup notes below for AI Gateway budgets and keys.
3. Paste your webhook URL (shown in Studio after install) as the webhook URL and save.
4. Send a test fire to check it.
5. In LangWatch, go to **AI Gateway → Webhooks** and add an endpoint with the URL above. Subscribe it to the `gateway.budget.*` and `gateway.virtual_key.*` events. The recipe ignores `gateway.request.*`, which would push on every call.
6. **Availability:** gateway webhooks are an **Enterprise** feature.
7. LangWatch batches deliveries; when a batch holds several events, the push describes the most important one and says how many more there are. The **Send test** button gives a quiet "LangWatch connected" push.
8. LangWatch can sign requests with `X-LangWatch-Signature` (HMAC-SHA256), but JustPush doesn't check it yet, so keep your endpoint URL private.

## Code

Studio calls `handleRequest(request)` with the incoming webhook (`{ method, headers, body, query, raw }`) and sends the message object it returns. Returning `null` sends nothing.

```js
// LangWatch → AI Gateway webhooks (budgets and virtual keys)
// Fires for LangWatch AI Gateway events: budget thresholds and breaches, and virtual
// key changes. Request-level events are skipped because they'd push on every call.
// Docs: https://langwatch.ai/docs/features/webhooks

const EVENTS = {
    "gateway.budget.threshold_crossed": { title: "⚠️ Budget almost used", priority: 0 },
    "gateway.budget.breached": { title: "🚫 Budget exceeded", priority: 1 },
    "gateway.virtual_key.created": { title: "🔑 Virtual key created", priority: -1 },
    "gateway.virtual_key.rotated": { title: "🔄 Virtual key rotated", priority: -1 },
    "gateway.virtual_key.disabled": { title: "⏸️ Virtual key disabled", priority: 0 },
    "gateway.virtual_key.enabled": { title: "▶️ Virtual key enabled", priority: -1 },
    "gateway.virtual_key.revoked": { title: "⛔ Virtual key revoked", priority: 0 },
}

// Look a key up in one of the tables above, ignoring inherited names like "toString".
function pick(table, key, fallback) {
    return Object.prototype.hasOwnProperty.call(table, key) ? table[key] : fallback
}

function usd(value) {
    const n = Number(value)
    return value != null && Number.isFinite(n) ? `$${n.toFixed(2)}` : null
}

function handleRequest(request) {
    const body = request.body || {}
    const batch = Array.isArray(body.batch) ? body.batch : [body]

    // "Send test" delivers a test.ping; confirm the connection quietly.
    if (batch.some((envelope) => envelope?.type === "test.ping")) {
        return { title: "🔔 LangWatch connected", message: "Test event received", topic: "LangWatch", priority: -1 }
    }

    // Deliveries are batched; push about the most important event in the batch.
    const events = batch.filter((envelope) => pick(EVENTS, envelope?.type, null))

    if (!events.length) {
        console.log("No budget or virtual key events in this delivery — skipping")
        return null
    }

    events.sort((a, b) => EVENTS[b.type].priority - EVENTS[a.type].priority)
    const envelope = events[0]
    const event = EVENTS[envelope.type]
    const data = envelope.data ?? {}
    const more = events.length > 1 ? ` (+${events.length - 1} more)` : ""

    if (envelope.type.startsWith("gateway.budget.")) {
        const spent = usd(data.spent_usd)
        const limit = usd(data.limit_usd)
        const blocking = envelope.type === "gateway.budget.breached" && data.on_breach === "block"
        const scope = [data.scope_type?.replace(/_/g, " "), data.window ? `per ${data.window}` : null].filter(Boolean).join(", ")

        return {
            title: event.title + more,
            message:
                (spent && limit ? `${spent} of ${limit}` : "Budget limit reached") +
                (scope ? ` (${scope})` : "") +
                (blocking ? " — requests are being blocked" : ""),
            topic: "LangWatch",
            // A blocking budget means production traffic is failing right now.
            priority: blocking ? 2 : event.priority,
        }
    }

    const key = data.name ?? data.virtual_key_id ?? data.id

    return {
        title: event.title + more,
        message: key ? `Key ${key}` : "A virtual key changed",
        topic: "LangWatch",
        priority: event.priority,
    }
}
```

## LangWatch gateway.budget.breached webhook payload (sample)

```json
{
  "batch": [
    {
      "id": "01K1ZX3:breached",
      "data": {
        "window": "month",
        "event_id": "bgt_01H:vk_01H:breached:1790553600000",
        "budget_id": "bgt_01H",
        "limit_usd": "25",
        "on_breach": "block",
        "spent_usd": "25.1042",
        "event_type": "gateway.budget.breached",
        "scope_type": "virtual_key",
        "end_user_id": null,
        "occurred_at": "2026-09-29T14:03:11.482Z",
        "virtual_key_id": "vk_01H",
        "bucket_scope_id": "vk_01H",
        "organization_id": "org_01H",
        "anchor_project_id": null,
        "period_started_at": "2026-09-01T00:00:00.000Z"
      },
      "type": "gateway.budget.breached",
      "created": "2026-09-29T14:03:11.482Z",
      "schema_version": "1"
    }
  ]
}
```

## FAQ

### Does this work on iPhone and Android?

Yes. Install the JustPush app from the App Store or Google Play and sign in. Every phone signed in to your account gets the notification.

### Do I need to write code?

No. Install the recipe in Studio, paste your webhook URL into LangWatch and you are done. The code is there if you want to change the text, the sound or the buttons.

### Can I change what the notification says?

Yes. After install the recipe's code is yours. Edit it in Studio and test it against the sample payload before you save.

### What does it cost?

JustPush is free for 30 days. After that it's $19.99 a year, or $39.99 once. There is no extra charge for recipes.
