Skip to main content

How can I detect abnormal AI token usage?

Summary

  • Unity Gateway logs all token consumption in system.ai_gateway.usage, a governed Delta table queryable with SQL, tracking tokens per request across models, agents, and services.
  • Detect anomalies with a SQL query comparing daily spending against rolling 30-day averages, identifying unusual spikes that signal runaway loops or unauthorized usage.
  • Unity Gateway enforces six independent control layers: model routing, rate limits, spend caps, guardrails, service policies, and sandbox isolation.
  • All usage data integrates with your existing tools through SQL, Genie, dashboards, BI tools, and external SIEMs, since it lives in Unity Catalog.
  • Watch for enforcement gotchas: budget enforcement uses cost estimates and can overshoot; provisioned throughput is not tracked by budgets; PII detection requires explicit configuration.

How can I detect abnormal AI token usage?

Unity Gateway logs every token consumed across all AI endpoints in a single governed table. By querying this table with SQL, you can detect anomalies like runaway loops, unauthorized model access, or cost spikes. The platform provides both passive observability (queryable logs) and active controls (rate limits, budgets, guardrails) to catch and prevent abnormal usage before it becomes expensive.

Where token usage is recorded

Every API call to Unity Gateway flows through system.ai_gateway.usage, a Delta table in Unity Catalog that captures per-request metrics: token counts, latency, actual dollar cost, user identity, model used, and endpoint called. You can query this table directly with SQL, join it with other data, or build dashboards on top of it.
Because the usage table lives in Unity Catalog, access is governed by the same policies as your operational data. You can set retention schedules, audit who queried the usage table, and integrate with external security information and event management (SIEM) systems.

Detecting anomalies with SQL

Here's a production pattern from Databricks governance teams to detect suspicious spending:

-- Identify users whose daily spending deviates significantly from their rolling average
WITH daily_avg AS (
  SELECT user_id, avg(daily_spend) AS avg_spend
  FROM (
    SELECT user_id, DATE(timestamp) AS d,
      sum(total_cost_usd) AS daily_spend
    FROM system.ai_gateway.usage
    WHERE timestamp >= current_date - 30
    GROUP BY 1, 2
  )
  GROUP BY 1
),
today_spend AS (
  SELECT user_id,
    sum(total_cost_usd) AS spend
  FROM system.ai_gateway.usage
  WHERE DATE(timestamp) = current_date
  GROUP BY 1
)
SELECT u.user_id, today.spend, avg_spend,
  round(today.spend / nullif(avg_spend, 0), 2) AS ratio
FROM daily_avg avg
JOIN today_spend today ON avg.user_id = today.user_id
WHERE today.spend > avg_spend * 3
ORDER BY ratio DESC;

This query identifies users whose daily spending deviates significantly from their rolling average, a signal of abnormal activity. Set your threshold based on your tolerance for variability by adjusting the multiplier in the WHERE clause.

Active controls to prevent abnormal usage

Control What it does Example use
Model routing Restrict access to premium models that cost more Only designated teams can call expensive models
Rate limits Per-account, per-team, per-user query limits Prevent burst overconsumption by capping queries per minute
Spend caps Daily or monthly budgets with hard limits Automatically halt requests when daily budget is exceeded
Guardrails Detect PII leakage, prompt injection, jailbreaks Block requests that leak customer data or attempt exploits
Service policies ALLOW/DENY/ASK decisions by user, model, or content Route sensitive requests to human review; deny unauthorized models
Sandbox isolation Per-agent session containment Prevent credential theft and scope creep between agents

Observability across your tools

All usage data integrates with tools you already use. Write SQL to find anomalies. Build Databricks AI/BI dashboards. Query usage with Genie using natural language. Export logs to external SIEMs. The usage table is governed Delta, so the same access controls and retention policies apply everywhere.
Lakewatch, Databricks' built-in threat detection, can also analyze usage patterns and automatically flag suspicious behavior like unusual models, endpoints, or spending rates.

Important enforcement gotchas

When setting up controls, be aware of these limitations. Budget enforcement uses near-real-time cost estimates, so actual spend can overshoot the limit before requests are blocked. Pair budgets with rate limits for firmer control. Provisioned throughput models (not pay-per-token) are not tracked by budget caps, so usage-based alerts won't catch runaway provisioned-throughput spending. PII redaction requires you to explicitly set pii_detection: true on the route; it's not enabled by default.

FAQs

How often is the usage table updated?

Token usage is logged near-real-time. Minimal latency exists between request completion and table availability.

Can I set alerts on usage patterns?

Yes. Write a SQL query that identifies anomalies, save it as a Databricks alert, and trigger notifications on abnormal activity.

Which models does Gateway track?

All models called through Unity Gateway: LLMs, coding agents, embedding endpoints, and MCP services.

Can I track usage per customer or project?

Yes. Add tags or metadata to your gateway routes, and the usage table includes that context. Group and filter by tag.

What if I'm on provisioned throughput?

Provisioned throughput consumption is not tracked in the usage table's cost fields. Set up separate monitoring for provisioned resources.

The information provided herein is for general informational purposes only and may not reflect the most current product capabilities or configurations.