litellm/tests/unit/proxy/guardrails/test_usage_tracking.py
joshua-berri 906fa7ed28
feat(accounting): add opt-in SpendLogs ledger (#49856)
* feat(accounting): add opt-in SpendLogs ledger delivery

* fix(accounting): preserve missing organization daily spend parity

* fix(accounting): preserve ledger receipts when replaying migration

* fix(accounting): preserve callback windows and tune ledger batches

* fix(accounting): preserve window charges and bound ledger statements

* test(accounting): verify budget callback registration order

* test(accounting): verify captured charges after repeated startup

* feat(accounting): store durable payloads independently of spend logs

* fix(accounting): preserve first delivery and trace ledger writes

* fix(accounting): keep SDK imports independent of proxy tracing

* fix(accounting): honor log retention during batch takeover

* test(accounting): cover retained batch alerts and invalid settings

* test(accounting): verify downgrade command against pending work

* fix(accounting): drain retries without starving fresh deliveries

* test(accounting): cover delivery projection, metrics and window key reads

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(accounting): read delivery metrics without the shared registry

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(accounting): charge a leftover $0 batch row when its claim fails

A failed batch claim buffers the retrieve's row and reports it charged. At flush the
row conflicted with the pre-upgrade $0 SpendLogs row, so its delivery was rolled back
and the charge never reached the spend tables. The flush now takes that row over in the
same transaction and keeps the delivery.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(accounting): trace the batch takeover at flush as a SpendLogs update

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(accounting): keep local spend logs when SPEND_LOGS_URL has no export client

The export client is never created, so main always writes SpendLogs to the proxy's own
database even with SPEND_LOGS_URL set. The ledger skipped local rows whenever the URL was
set, which dropped those request logs. It now treats a flush as an export only when the
client exists, and batch claims write local rows as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(accounting): keep every request's charge when its log row is shared, evicted or rejected

- Key non-batch deliveries on a per-request delivery_id so a reused client call id is still
  charged once per request, while buffer replays stay deduplicated
- Persist the redacted api_key instead of an opaque bearer token
- Keep the accounting payload when the spend-log queue evicts a row or Postgres rejects it
- Strip NUL from payloads and skip non-text tags so projection never parks a paid request

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(accounting): isolate a raw-SQL spend log row Postgres rejects instead of requeueing the batch

A rejected row on the ledger insert path raises RawQueryError, which the exact DataError check missed, so the whole batch was requeued every flush. Data and integrity SQLSTATE classes now bisect like before, while missing tables and deadlocks keep retrying

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(accounting): keep charging a request whose tag is too long for the daily tag index

A tag past Postgres's btree entry limit failed the DailyTagSpend upsert, and on the ledger path that rolled back the request's key, team and user charges with it. Such tags are now left out of the daily tag rollup only

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(accounting): store only guardrail billing fields in the accounting payload

The ledger payload copied whole guardrail records, including responses and match details, into LiteLLM_SpendLogDelivery even when spend logs were disabled. It now keeps the id, name, usage and cost fields the projection reads. NUL stripping reuses safe_json_structure instead of a recursive walk

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(proxy): move spend payload stamping out of update_database

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(proxy): restore the modules the import-layering guard evicts

The guard popped litellm.proxy.utils from sys.modules for good, so later tests on the same xdist worker patched a fresh module while calling functions bound to the old one, and spend queue tests failed depending on scheduling

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(proxy): keep update_database's payload stamping on its own local

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(accounting): type guardrail entries and use a public key redaction helper

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(accounting): keep charging requests whose end-user id is too long for an index

An oversized end-user id failed the SpendLogs insert with SQLSTATE 54000, which was requeued forever, and failed the end-user upsert in the delivery, which parked the key and team charge with it. Class 54 rows are now isolated like other rejected rows, and such ids are left out of the end-user rollups only

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(proxy): type the spend log queue snapshot

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(accounting): leave out any projected row Postgres cannot index or store

Caller-supplied ids and counters, such as a long tag or end-user id or an out-of-range compression count, failed the delivery transaction and rolled back the request's key, team and user charges. One check in project() now drops only the row that cannot be stored, replacing the per-field cutoffs

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(accounting): keep the guardrail fields compression savings are counted from

Slimming the guardrail records dropped the provider and the tokens_saved figure that Headroom compression savings are read from, and dropped a bare-dict record entirely. The payload now keeps both while still leaving out guardrail content

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(accounting): cap projected counters so daily totals cannot overflow

Counters such as a caller-supplied compression count fit one by one but overflowed the bigint daily total once summed across requests, rolling back the key, team and user charges in the same transaction. Each counter is now capped at 2^40, far below any total that could overflow

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(accounting): let a buffered batch charge take over its zero-cost receipt

A batch charge queued after a failed lookup hit the $0 receipt an earlier poll left behind, and ON CONFLICT dropped it. The buffered insert now runs the same receipt takeover as a live retrieve

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(accounting): read a rescued batch row's spend from its accounting payload

Rows rescued from an overflowing queue keep only the accounting payload, so the buffered takeover never saw their positive spend and left the $0 receipt uncharged

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(accounting): read budget windows through the delivery transaction

Window seed and existing-row reads used a separate connection while the delivery transaction held one, so with a one-connection pool they waited on their own transaction until it timed out. Inside an enclosing transaction they now share its connection

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(accounting): retry a transient failure while keeping a dropped row's charge

The rescue swallowed every error, so a database blip right after a row was rejected lost the unpaid charge. Only a rejected row is still logged and skipped, anything else propagates so the flush retries

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(accounting): give delivery projection payloads their charged entities

project() now reads entities only from accounting_entities, so the hand-built
delivery payloads carry them like real stored payloads do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(accounting): own ledger rows by their payload and key batches on the batch id

Drop the accounting_path stamp and its SpendLogs column. A buffered row that
carries an accounting payload is ledger owned, one without it is legacy, and
spend counters get a plain windows_from_ledger flag. Window seeding tells
applied ledger rows apart by their delivery row.

A ledger batch is plain transaction A keyed by the batch id, so a repeated
completed retrieve conflicts and charges nothing. A $0 retrieve records no
delivery, a batch a legacy retrieve already charged is not charged again, and
the legacy batch path is unchanged from main apart from refusing a batch the
ledger already owns. Batch takeover and its delta bookkeeping are removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(accounting): drop a projection test duplicated while rebasing

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(accounting): drop previous_payload now that deliveries are never replaced

Batch takeover was the only writer of previous_payload, so the worker applies
each delivery's payload as stored and the column leaves the migration.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(accounting): drop the ledger downgrade checklist script

The use_spend_logs_ledger flag is the rollback path, so a separate downgrade
gate is no longer needed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(accounting): name the window seed reads so their Prisma span resolves

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(accounting): remove the unused accounting event types and protocols

types.py, protocols.py and measurements.py were left from the old event
scaffolding. Nothing outside the package imports them, so they go with their
tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(accounting): park only deliveries whose own data fails

A delivery that keeps failing on a database outage, lock timeout or deadlock
stays pending and retries with capped backoff, so a recovered database applies
it. Only a data error or an invalid payload parks the isolated row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(accounting): isolate a spend log row the insert statement rejects

The raw SpendLogs insert validates columns and JSON before Postgres sees the
row, so a row that fails there is now bisected out and dropped alone while its
charge is kept, instead of failing and replaying the whole flush.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(accounting): carry a job's model window on every completed poll in ledger mode

The ledger keeps the poll whose delivery claims the job, which need not be the
poll that won the limiter's in-memory charge. Every completed poll now carries
the job's window, so the delivery that lands always has it, while the live
model budget is still charged once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(accounting): leave model-budget windows on the existing window writer

Model-budget windows are written by the model budget limiter rather than
derived from a request's spend, so they stay on main's window queue like live
budget counters do. This drops window capture from the accounting payload,
the deferred limiter registration and the job-window capture, and the ledger
keeps owning key and team budget windows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(accounting): keep a delivery pending when the database is unreachable

Prisma reports a can't reach database server outage as a DataError, so the
worker now checks for an unavailable database before treating a data error
as the delivery's own fault.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(accounting): isolate only rows the raw insert cannot encode

Catching every ValueError also swallowed unexpected insert errors that main
re-raises. The raw insert now raises RejectedSpendLogRow for an unknown column
or non-JSON value, and only that is isolated as a rejected row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(accounting): store zero savings when caller data makes them uncomputable

A huge caller-supplied tokens_saved overflowed the savings math inside
build_accounting_payload, which dropped the request's charge in ledger mode.
Savings that cannot be computed are now recorded as zero and the charge lands.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Joshua Valluru <326636767+joshua-berri@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 17:49:15 -08:00

668 lines
26 KiB
Python

import json
from datetime import datetime, timezone
from typing import Any
from unittest.mock import AsyncMock, MagicMock
import httpx
import pytest
from litellm.proxy.guardrails import usage_tracking
from litellm.proxy.guardrails.usage_tracking import (
_MAX_PENDING_ROWS,
PendingRollups,
_capped,
process_spend_logs_guardrail_usage,
)
def _prisma() -> MagicMock:
client = MagicMock()
db = client.db
db.litellm_dailyguardrailmetrics.upsert = AsyncMock()
db.litellm_dailyguardrailusageunits.upsert = AsyncMock()
db.litellm_spendlogguardrailindex.create_many = AsyncMock()
return client
def _payload(
request_id: str,
*,
team_id: str | None = "team-a",
api_key: str = "hashed-key-1",
usage: dict[str, Any] | None = None,
guardrail_status: str = "success",
cost_by_unit: dict[str, Any] | None = None,
cost_in_spend: bool | None = None,
) -> dict[str, Any]:
entry: dict[str, Any] = {
"guardrail_id": "bedrock-guard",
"guardrail_status": guardrail_status,
}
if usage is not None:
entry["guardrail_usage"] = usage
if cost_by_unit is not None:
entry["guardrail_cost_by_unit"] = cost_by_unit
if cost_in_spend is not None:
entry["guardrail_cost_in_spend"] = cost_in_spend
return {
"request_id": request_id,
"startTime": datetime(2026, 8, 17, 12, 0, tzinfo=timezone.utc),
"team_id": team_id,
"api_key": api_key,
"metadata": json.dumps({"guardrail_information": [entry]}),
}
def _units_upserts(prisma: MagicMock) -> dict[tuple, int]:
calls = prisma.db.litellm_dailyguardrailusageunits.upsert.call_args_list
out: dict[tuple, int] = {}
for c in calls:
where = c.kwargs["where"]["guardrail_id_date_team_id_api_key_usage_unit"]
create = c.kwargs["data"]["create"]
assert create["units"] == c.kwargs["data"]["update"]["units"]["increment"]
assert {k: create[k] for k in where} == where
out[tuple(where[k] for k in ("guardrail_id", "date", "team_id", "api_key", "usage_unit"))] = create["units"]
return out
def _cost_upserts(prisma: MagicMock) -> dict[str, tuple[float, int]]:
"""usage_unit -> (cost, untracked_units) written on create; the update path must increment by the same."""
calls = prisma.db.litellm_dailyguardrailusageunits.upsert.call_args_list
out: dict[str, tuple[float, int]] = {}
for c in calls:
create = c.kwargs["data"]["create"]
update = c.kwargs["data"]["update"]
assert update["cost"] == {"increment": create["cost"]}
assert update["untracked_units"] == {"increment": create["untracked_units"]}
out[create["usage_unit"]] = (create["cost"], create["untracked_units"])
return out
@pytest.mark.asyncio
async def test_usage_units_rolled_up_by_guardrail_team_key_and_date():
"""
LIT-5650: billable units must aggregate per (guardrail, date, team, key,
counter): same-key payloads sum into one upsert, a team-less payload gets
its own empty-string-team row, and blocked invocations (which Bedrock
still bills for) count exactly like passed ones.
"""
prisma = _prisma()
logs = [
_payload("r1", usage={"topicPolicyUnits": 1, "contentPolicyUnits": 1}),
_payload(
"r2",
usage={"topicPolicyUnits": 1, "contentPolicyUnits": 2},
guardrail_status="guardrail_intervened",
),
_payload("r3", team_id=None, api_key="hashed-key-2", usage={"topicPolicyUnits": 1}),
]
await process_spend_logs_guardrail_usage(prisma, logs)
assert _units_upserts(prisma) == {
("bedrock-guard", "2026-08-17", "team-a", "hashed-key-1", "topicPolicyUnits"): 2,
("bedrock-guard", "2026-08-17", "team-a", "hashed-key-1", "contentPolicyUnits"): 3,
("bedrock-guard", "2026-08-17", "", "hashed-key-2", "topicPolicyUnits"): 1,
}
@pytest.mark.asyncio
async def test_flagged_status_counts_as_flagged_not_passed_or_blocked():
"""LIT-6894: a custom code flag() verdict lands in flagged_count on the Monitor rollup."""
prisma = _prisma()
logs = [
_payload("r1", guardrail_status="success"),
_payload("r2", guardrail_status="guardrail_flagged"),
_payload("r3", guardrail_status="guardrail_intervened"),
]
await process_spend_logs_guardrail_usage(prisma, logs)
create = prisma.db.litellm_dailyguardrailmetrics.upsert.call_args.kwargs["data"]["create"]
assert (create["requests_evaluated"], create["passed_count"], create["flagged_count"], create["blocked_count"]) == (
3,
1,
1,
1,
)
def _fake_sleep() -> tuple[AsyncMock, list[float]]:
delays: list[float] = []
sleep = AsyncMock(side_effect=lambda delay: delays.append(delay))
return sleep, delays
@pytest.mark.asyncio
async def test_one_failing_upsert_does_not_drop_remaining_writes():
"""
A DB error on one daily-metrics or usage-unit upsert must not cancel the
remaining upserts in the flushed batch, or the usage endpoints would
permanently under-report billable counters.
"""
prisma = _prisma()
prisma.db.litellm_dailyguardrailmetrics.upsert.side_effect = httpx.ConnectError("db down")
prisma.db.litellm_dailyguardrailusageunits.upsert.side_effect = [httpx.ConnectError("db down"), None, None]
sleep, _ = _fake_sleep()
logs = [
_payload("r1", usage={"topicPolicyUnits": 1}),
_payload("r2", team_id=None, api_key="hashed-key-2", usage={"topicPolicyUnits": 1}),
]
await process_spend_logs_guardrail_usage(prisma, logs, sleep=sleep, pending=PendingRollups())
assert _units_upserts(prisma) == {
("bedrock-guard", "2026-08-17", "team-a", "hashed-key-1", "topicPolicyUnits"): 1,
("bedrock-guard", "2026-08-17", "", "hashed-key-2", "topicPolicyUnits"): 1,
}
@pytest.mark.asyncio
async def test_transient_upsert_failure_is_retried_with_backoff_for_failed_rows_only():
"""
A connection error (the write provably never reached the database) must
not permanently drop billed units from the aggregates: only the rows that
failed are re-sent, after exponential backoff, and the batch ends once
every row has landed.
"""
prisma = _prisma()
prisma.db.litellm_dailyguardrailusageunits.upsert.side_effect = [httpx.ConnectError("blip"), None, None]
sleep, delays = _fake_sleep()
logs = [
_payload("r1", usage={"topicPolicyUnits": 1}),
_payload("r2", team_id=None, api_key="hashed-key-2", usage={"topicPolicyUnits": 1}),
]
await process_spend_logs_guardrail_usage(prisma, logs, sleep=sleep)
calls = prisma.db.litellm_dailyguardrailusageunits.upsert.call_args_list
assert len(calls) == 3
assert calls[2].kwargs["where"] == calls[0].kwargs["where"]
assert delays == [1]
@pytest.mark.asyncio
async def test_persistent_upsert_failure_stops_after_three_retries():
prisma = _prisma()
prisma.db.litellm_dailyguardrailmetrics.upsert.side_effect = httpx.ConnectError("db down")
sleep, delays = _fake_sleep()
pending = PendingRollups()
await process_spend_logs_guardrail_usage(
prisma, [_payload("r1", usage={"topicPolicyUnits": 1})], sleep=sleep, pending=pending
)
assert prisma.db.litellm_dailyguardrailmetrics.upsert.call_count == 4
assert delays == [1, 2, 4]
assert prisma.db.litellm_dailyguardrailusageunits.upsert.call_count == 1
assert dict(pending.metrics) == {
("bedrock-guard", "2026-08-17"): {
"requests_evaluated": 1,
"passed_count": 1,
"blocked_count": 0,
"flagged_count": 0,
}
}
@pytest.mark.asyncio
async def test_retry_exhausted_rows_are_requeued_and_land_on_the_next_flush():
"""
LIT-5761: rollup rows whose connection-error retries exhaust must not be
silently lost. They are requeued and merged into the next flushed batch,
so the aggregates catch up once the database is reachable again.
"""
pending = PendingRollups()
down = _prisma()
down.db.litellm_dailyguardrailmetrics.upsert.side_effect = httpx.ConnectError("db down")
down.db.litellm_dailyguardrailusageunits.upsert.side_effect = httpx.ConnectError("db down")
sleep, _ = _fake_sleep()
await process_spend_logs_guardrail_usage(
down, [_payload("r1", usage={"topicPolicyUnits": 2})], sleep=sleep, pending=pending
)
assert dict(pending.units) == {
("bedrock-guard", "2026-08-17", "team-a", "hashed-key-1", "topicPolicyUnits"): (2, 0.0, 2)
}
recovered = _prisma()
await process_spend_logs_guardrail_usage(
recovered, [_payload("r2", usage={"topicPolicyUnits": 3})], sleep=sleep, pending=pending
)
assert _units_upserts(recovered) == {
("bedrock-guard", "2026-08-17", "team-a", "hashed-key-1", "topicPolicyUnits"): 5,
}
metrics_create = recovered.db.litellm_dailyguardrailmetrics.upsert.call_args.kwargs["data"]["create"]
assert metrics_create["requests_evaluated"] == 2
assert not pending.units
assert not pending.metrics
@pytest.mark.asyncio
async def test_ambiguous_failures_are_never_requeued():
"""
A post-send failure (the increment may have committed) must stay dropped:
requeueing it would re-send a possibly applied increment and double-count.
"""
pending = PendingRollups()
prisma = _prisma()
prisma.db.litellm_dailyguardrailusageunits.upsert.side_effect = httpx.ReadTimeout("maybe committed")
sleep, delays = _fake_sleep()
await process_spend_logs_guardrail_usage(
prisma, [_payload("r1", usage={"topicPolicyUnits": 1})], sleep=sleep, pending=pending
)
assert delays == []
assert not pending.units
assert not pending.metrics
def test_pending_requeue_is_capped_dropping_oldest_rows():
rows = {index: index for index in range(_MAX_PENDING_ROWS + 5)}
capped = _capped(rows, "usage unit")
assert len(capped) == _MAX_PENDING_ROWS
assert 4 not in capped
assert _MAX_PENDING_ROWS + 4 in capped
def _units_upsert_wheres(prisma: MagicMock) -> list[tuple]:
return [
tuple(
c.kwargs["where"]["guardrail_id_date_team_id_api_key_usage_unit"][k]
for k in ("guardrail_id", "date", "team_id", "api_key", "usage_unit")
)
for c in prisma.db.litellm_dailyguardrailusageunits.upsert.call_args_list
]
@pytest.mark.asyncio
async def test_post_send_failure_is_never_retried_so_increments_cannot_double_count():
"""
Follow-up to #37225: the units upsert is a non-idempotent increment, so an
ambiguous post-send failure (read timeout after the statement may have
committed) must be attempted exactly once. Re-sending it stacks a second
increment and inflates billable unit totals. Only a connection error proves
the write never reached the database and may be retried; the other rows in
the batch still land either way.
"""
prisma = _prisma()
prisma.db.litellm_dailyguardrailusageunits.upsert.side_effect = [
httpx.ReadTimeout("read timed out"),
httpx.ConnectError("refused"),
None,
]
sleep, delays = _fake_sleep()
logs = [
_payload("r1", usage={"topicPolicyUnits": 1}),
_payload("r2", team_id=None, api_key="hashed-key-2", usage={"topicPolicyUnits": 1}),
]
await process_spend_logs_guardrail_usage(prisma, logs, sleep=sleep)
timed_out_row = ("bedrock-guard", "2026-08-17", "", "hashed-key-2", "topicPolicyUnits")
refused_row = ("bedrock-guard", "2026-08-17", "team-a", "hashed-key-1", "topicPolicyUnits")
assert _units_upsert_wheres(prisma) == [timed_out_row, refused_row, refused_row]
assert delays == [1]
@pytest.mark.asyncio
async def test_generic_upsert_exception_is_terminal_for_that_row_only():
prisma = _prisma()
prisma.db.litellm_dailyguardrailmetrics.upsert.side_effect = RuntimeError("constraint violation")
sleep, delays = _fake_sleep()
await process_spend_logs_guardrail_usage(prisma, [_payload("r1", usage={"topicPolicyUnits": 1})], sleep=sleep)
assert prisma.db.litellm_dailyguardrailmetrics.upsert.call_count == 1
assert delays == []
assert _units_upserts(prisma) == {
("bedrock-guard", "2026-08-17", "team-a", "hashed-key-1", "topicPolicyUnits"): 1,
}
@pytest.mark.asyncio
async def test_zero_and_non_int_usage_counters_are_skipped():
prisma = _prisma()
logs = [
_payload(
"r1",
usage={
"topicPolicyUnits": 1,
"wordPolicyUnits": 0,
"contentPolicyImageUnits": 0,
"oddball": "not-an-int",
"boolish": True,
},
),
_payload("r2", usage=None),
]
await process_spend_logs_guardrail_usage(prisma, logs)
assert _units_upserts(prisma) == {
("bedrock-guard", "2026-08-17", "team-a", "hashed-key-1", "topicPolicyUnits"): 1,
}
@pytest.mark.asyncio
async def test_not_run_entries_are_indexed_but_not_counted_as_evaluations():
"""
LIT-6314 records a not_run entry when message scoping leaves a guardrail
nothing to scan. The guardrail never evaluated the request, so counting it
as a passed evaluation would inflate daily pass rates; it still gets an
index row so per-request drill-down finds the spend log.
"""
prisma = _prisma()
logs = [_payload("r1", guardrail_status="not_run"), _payload("r2")]
await process_spend_logs_guardrail_usage(prisma, logs)
metrics_create = prisma.db.litellm_dailyguardrailmetrics.upsert.call_args.kwargs["data"]["create"]
assert metrics_create["requests_evaluated"] == 1
assert metrics_create["passed_count"] == 1
index_rows = prisma.db.litellm_spendlogguardrailindex.create_many.call_args.kwargs["data"]
assert sorted(row["request_id"] for row in index_rows) == ["r1", "r2"]
@pytest.mark.asyncio
async def test_not_run_entry_shares_index_key_with_evaluated_sibling_of_same_name():
"""
The not_run entry from the shared base guardrail carries only guardrail_name,
while the evaluated entry from the same guardrail (e.g. content filter on the
output of a logging_only run) carries its guardrail_id. Keying them differently
lists one request twice in the monitor, once as not_run and once as passed.
"""
prisma = _prisma()
payload = _payload("r1")
payload["metadata"] = json.dumps(
{
"guardrail_information": [
{"guardrail_name": "cf", "guardrail_status": "not_run"},
{
"guardrail_name": "cf",
"guardrail_id": "cf-uuid",
"policy_id": "pol-1",
"guardrail_status": "success",
},
{"guardrail_name": "other", "guardrail_status": "not_run"},
]
}
)
await process_spend_logs_guardrail_usage(prisma, [payload])
index_rows = prisma.db.litellm_spendlogguardrailindex.create_many.call_args.kwargs["data"]
assert sorted((row["guardrail_id"], row["policy_id"]) for row in index_rows) == [
("cf-uuid", "pol-1"),
("other", None),
]
metrics_create = prisma.db.litellm_dailyguardrailmetrics.upsert.call_args.kwargs["data"]["create"]
assert (metrics_create["guardrail_id"], metrics_create["requests_evaluated"]) == ("cf-uuid", 1)
@pytest.mark.asyncio
async def test_malformed_not_run_entry_does_not_drop_the_batch():
prisma = _prisma()
payload = _payload("r1")
payload["metadata"] = json.dumps(
{
"guardrail_information": [
{"guardrail_name": ["not", "a", "string"], "guardrail_status": "success"},
{"guardrail_name": "", "guardrail_id": "cf-uuid", "guardrail_status": "success"},
{"guardrail_status": "success"},
]
}
)
await process_spend_logs_guardrail_usage(prisma, [payload])
index_rows = prisma.db.litellm_spendlogguardrailindex.create_many.call_args.kwargs["data"]
assert [row["guardrail_id"] for row in index_rows] == ["cf-uuid"]
metrics_create = prisma.db.litellm_dailyguardrailmetrics.upsert.call_args.kwargs["data"]["create"]
assert (metrics_create["guardrail_id"], metrics_create["requests_evaluated"]) == ("cf-uuid", 1)
@pytest.mark.asyncio
async def test_batch_of_only_not_run_entries_writes_no_metrics_row():
prisma = _prisma()
await process_spend_logs_guardrail_usage(prisma, [_payload("r1", guardrail_status="not_run")])
assert prisma.db.litellm_dailyguardrailmetrics.upsert.call_count == 0
index_rows = prisma.db.litellm_spendlogguardrailindex.create_many.call_args.kwargs["data"]
assert [row["request_id"] for row in index_rows] == ["r1"]
@pytest.mark.asyncio
async def test_payload_without_request_id_is_skipped_like_the_metrics_path():
prisma = _prisma()
logs = [
{**_payload("ignored", usage={"topicPolicyUnits": 5}), "request_id": None},
_payload("r2", usage={"topicPolicyUnits": 1}),
]
await process_spend_logs_guardrail_usage(prisma, logs)
assert _units_upserts(prisma) == {
("bedrock-guard", "2026-08-17", "team-a", "hashed-key-1", "topicPolicyUnits"): 1,
}
assert prisma.db.litellm_dailyguardrailmetrics.upsert.call_args.kwargs["data"]["create"]["requests_evaluated"] == 1
@pytest.mark.asyncio
async def test_cost_rolled_up_per_counter_alongside_units():
"""LIT-5652: the hook's per-counter cost lands on the same daily row as the
units it priced, summed across payloads exactly like the units are, and the
update path increments it so a second flush on the same day keeps adding."""
prisma = _prisma()
logs = [
_payload(
"r1",
usage={"contentPolicyUnits": 1000, "wordPolicyUnits": 50},
cost_by_unit={"contentPolicyUnits": 0.15, "wordPolicyUnits": 0.0},
),
_payload(
"r2",
usage={"contentPolicyUnits": 2000, "wordPolicyUnits": 10},
cost_by_unit={"contentPolicyUnits": 0.3, "wordPolicyUnits": 0.0},
),
]
await process_spend_logs_guardrail_usage(prisma, logs)
assert _units_upserts(prisma) == {
("bedrock-guard", "2026-08-17", "team-a", "hashed-key-1", "contentPolicyUnits"): 3000,
("bedrock-guard", "2026-08-17", "team-a", "hashed-key-1", "wordPolicyUnits"): 60,
}
costs = _cost_upserts(prisma)
assert costs["contentPolicyUnits"] == (pytest.approx(0.45), 0)
assert costs["wordPolicyUnits"] == (0.0, 0)
@pytest.mark.asyncio
async def test_counter_the_hook_could_not_price_is_stored_as_untracked_units_not_free():
"""A counter the cost map does not list arrives stamped as None. Its units
must land in untracked_units with no cost, so the row never reads as free,
while the priced counter on the same request keeps its cost."""
prisma = _prisma()
logs = [
_payload(
"r1",
usage={"contentPolicyUnits": 1000, "someFutureCounter": 3},
cost_by_unit={"contentPolicyUnits": 0.15, "someFutureCounter": None},
)
]
await process_spend_logs_guardrail_usage(prisma, logs)
assert _units_upserts(prisma) == {
("bedrock-guard", "2026-08-17", "team-a", "hashed-key-1", "contentPolicyUnits"): 1000,
("bedrock-guard", "2026-08-17", "team-a", "hashed-key-1", "someFutureCounter"): 3,
}
costs = _cost_upserts(prisma)
assert costs["contentPolicyUnits"] == (pytest.approx(0.15), 0)
assert costs["someFutureCounter"] == (0.0, 3)
@pytest.mark.asyncio
async def test_mixed_priced_and_unpriced_increments_keep_the_subtotal_and_count_the_rest_untracked():
"""Priced and unpriced increments on the same row (a hook without pricing,
a pre-upgrade proxy in a mixed fleet) must keep the priced subtotal and
count exactly the unpriced units as untracked. Nulling the cost would throw
away a known number; keeping it alone would look exact while understating."""
prisma = _prisma()
logs = [
_payload("r1", usage={"contentPolicyUnits": 1000}, cost_by_unit={"contentPolicyUnits": 0.15}),
_payload("r2", usage={"contentPolicyUnits": 700}),
_payload("r3", usage={"contentPolicyUnits": 300}, cost_by_unit={"contentPolicyUnits": None}),
]
await process_spend_logs_guardrail_usage(prisma, logs)
assert _units_upserts(prisma) == {
("bedrock-guard", "2026-08-17", "team-a", "hashed-key-1", "contentPolicyUnits"): 2000,
}
assert _cost_upserts(prisma) == {"contentPolicyUnits": (pytest.approx(0.15), 1000)}
@pytest.mark.asyncio
async def test_report_only_and_forged_costs_are_not_rolled_up_but_units_are():
"""guardrail_cost_in_spend=False (Azure Prompt Shield) keeps its cost out of
spend, so the rollup must not record it either or the dashboard would show
a number the budget never charged. A negative or non-finite per-counter cost
is treated the same way rather than subtracting from the day."""
prisma = _prisma()
logs = [
_payload("r1", usage={"text_records": 3}, cost_by_unit={"text_records": 0.5}, cost_in_spend=False),
_payload("r2", usage={"contentPolicyUnits": 10}, cost_by_unit={"contentPolicyUnits": -0.5}),
_payload("r3", usage={"topicPolicyUnits": 10}, cost_by_unit={"topicPolicyUnits": float("inf")}),
]
await process_spend_logs_guardrail_usage(prisma, logs)
assert _units_upserts(prisma) == {
("bedrock-guard", "2026-08-17", "team-a", "hashed-key-1", "text_records"): 3,
("bedrock-guard", "2026-08-17", "team-a", "hashed-key-1", "contentPolicyUnits"): 10,
("bedrock-guard", "2026-08-17", "team-a", "hashed-key-1", "topicPolicyUnits"): 10,
}
assert _cost_upserts(prisma) == {
"text_records": (0.0, 3),
"contentPolicyUnits": (0.0, 10),
"topicPolicyUnits": (0.0, 10),
}
@pytest.mark.asyncio
async def test_requeued_cost_is_added_to_the_next_flush():
"""Cost and untracked units must survive the connection-error requeue the
same way units do, or a DB blip would silently drop dollars (or the record
that some units had no price) while keeping the units themselves."""
pending = PendingRollups()
down = _prisma()
down.db.litellm_dailyguardrailmetrics.upsert.side_effect = httpx.ConnectError("db down")
down.db.litellm_dailyguardrailusageunits.upsert.side_effect = httpx.ConnectError("db down")
sleep, _ = _fake_sleep()
await process_spend_logs_guardrail_usage(
down,
[
_payload(
"r1",
usage={"contentPolicyUnits": 1000, "someFutureCounter": 3},
cost_by_unit={"contentPolicyUnits": 0.15, "someFutureCounter": None},
)
],
sleep=sleep,
pending=pending,
)
recovered = _prisma()
await process_spend_logs_guardrail_usage(
recovered,
[
_payload(
"r2",
usage={"contentPolicyUnits": 2000, "someFutureCounter": 4},
cost_by_unit={"contentPolicyUnits": 0.3, "someFutureCounter": None},
)
],
sleep=sleep,
pending=pending,
)
assert _units_upserts(recovered) == {
("bedrock-guard", "2026-08-17", "team-a", "hashed-key-1", "contentPolicyUnits"): 3000,
("bedrock-guard", "2026-08-17", "team-a", "hashed-key-1", "someFutureCounter"): 7,
}
costs = _cost_upserts(recovered)
assert costs["contentPolicyUnits"] == (pytest.approx(0.45), 0)
assert costs["someFutureCounter"] == (0.0, 7)
def _fan_out_payload(request_id: str, guardrail_ids: tuple[str, ...]) -> dict[str, Any]:
return {
"request_id": request_id,
"startTime": datetime(2026, 8, 17, 12, 0, tzinfo=timezone.utc),
"team_id": "team-a",
"api_key": "hashed-key-1",
"metadata": json.dumps(
{"guardrail_information": [{"guardrail_id": gid, "guardrail_status": "success"} for gid in guardrail_ids]}
),
}
def _index_rows_written(prisma: MagicMock) -> list[tuple[str, str]]:
return [
(row["request_id"], row["guardrail_id"])
for call in prisma.db.litellm_spendlogguardrailindex.create_many.call_args_list
for row in call.kwargs["data"]
]
@pytest.mark.asyncio
async def test_index_rows_are_written_in_row_bounded_statements(monkeypatch):
"""
LIT-5931: the drain caps logs, not logs x guardrails, so a fan-out must be
split into statements the query engine can afford instead of one create_many.
"""
monkeypatch.setattr(usage_tracking, "SPEND_LOG_WRITE_BATCH_MAX_ROWS", 100)
prisma = _prisma()
guardrail_ids = tuple(f"guard-{i}" for i in range(50))
logs = [_fan_out_payload(f"r{i}", guardrail_ids) for i in range(5)]
await process_spend_logs_guardrail_usage(prisma, logs, pending=PendingRollups())
statements = prisma.db.litellm_spendlogguardrailindex.create_many.call_args_list
assert [len(call.kwargs["data"]) for call in statements] == [100, 100, 50]
assert all(call.kwargs["skip_duplicates"] is True for call in statements)
assert _index_rows_written(prisma) == [(f"r{i}", gid) for i in range(5) for gid in guardrail_ids]
@pytest.mark.asyncio
async def test_one_failing_index_statement_does_not_drop_the_others_or_the_rollup(monkeypatch):
monkeypatch.setattr(usage_tracking, "SPEND_LOG_WRITE_BATCH_MAX_ROWS", 100)
prisma = _prisma()
prisma.db.litellm_spendlogguardrailindex.create_many.side_effect = [None, httpx.ReadTimeout("ambiguous"), None]
guardrail_ids = tuple(f"guard-{i}" for i in range(50))
logs = [_fan_out_payload(f"r{i}", guardrail_ids) for i in range(5)]
await process_spend_logs_guardrail_usage(prisma, logs, pending=PendingRollups())
assert prisma.db.litellm_spendlogguardrailindex.create_many.await_count == 3
assert prisma.db.litellm_dailyguardrailmetrics.upsert.await_count == len(guardrail_ids)
@pytest.mark.asyncio
async def test_disabled_log_deliveries_do_not_enable_guardrail_analytics():
prisma = _prisma()
row = _payload("lean", usage={"topicPolicyUnits": 2})
row["accounting_payload"] = {"request_id": "lean"}
row["accounting_log_disabled"] = True
await process_spend_logs_guardrail_usage(prisma, [row], pending=PendingRollups())
prisma.db.litellm_dailyguardrailmetrics.upsert.assert_not_awaited()
prisma.db.litellm_spendlogguardrailindex.create_many.assert_not_awaited()
prisma.db.litellm_dailyguardrailusageunits.upsert.assert_not_awaited()