You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tooling.apache.org by gs...@apache.org on 2026/10/12 21:06:15 UTC

(tooling-llmao) branch main updated: usage: read LiteLLM's daily aggregate instead of the raw spend log

This is an automated email from the ASF dual-hosted git repository.

gstein pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tooling-llmao.git


The following commit(s) were added to refs/heads/main by this push:
     new 5251bab  usage: read LiteLLM's daily aggregate instead of the raw spend log
5251bab is described below

commit 5251bab63a92872087495997e41dfd177693fa07
Author: Greg Stein <gs...@gmail.com>
AuthorDate: Mon Oct 12 16:05:36 2026 -0500

    usage: read LiteLLM's daily aggregate instead of the raw spend log
    
    My Keys took 21-26 seconds to load, and the Stopwatch lines put 17-21 of
    them in one call: GET spend/logs.
    
    That endpoint filters by date only when both start_date and end_date are
    sent. We sent start_date alone, so it was ignored and LiteLLM returned its
    10,000 most recent requests. Slow, and not "this week" either: the pilot
    fleet did 6,431 requests last week, so the figures reached back further.
    The per-user allowance read had the same flaw.
    
    /user/daily/activity is one row per day, model and key, carrying each key's
    owner and team. The fleet's week is about a second. Per-key usage,
    per-project draw and the allowance are all folded from that one read, so
    the allowance no longer costs a scan of its own.
    
    A day that has ended is history. LiteLLM files a request under the day it
    started, so a day is treated as closed an hour after it ends, then kept and
    never refetched. Only the days still open are asked for again, on the same
    60 second cache as before.
    
    Co-Authored-By: Claude Opus 5.5 <no...@anthropic.com>
---
 llmao/litellm_client.py  | 335 ++++++++++++++++++++++++-------------------
 tests/test_spend_rows.py | 360 +++++++++++++++++++++++++++++++----------------
 2 files changed, 427 insertions(+), 268 deletions(-)

diff --git a/llmao/litellm_client.py b/llmao/litellm_client.py
index 337bed5..8e16c1b 100644
--- a/llmao/litellm_client.py
+++ b/llmao/litellm_client.py
@@ -331,12 +331,15 @@ class LiteLLMBackend:
         self._cfg = cfg
         self.fleet = fleet
         self._team_ids: dict[str, str] = {}  # project → team_id
-        # user → (monotonic stamp, daily token buckets). Allowances are
-        # derived from the spend log on read; this keeps a page refresh from
-        # costing a scan.
-        self._allowance_cache: dict[str, tuple[float, dict[str, int]]] = {}
-        # (monotonic stamp, folded spend rows) for the current week.
-        self._spend_cache: tuple[float, list[dict]] | None = None
+        # ISO date → usage rows for that UTC day, from LiteLLM's daily
+        # aggregate. Spend and allowances are both derived from this.
+        self._daily: dict[str, list[dict]] = {}
+        # Days that can no longer change: kept, never refetched.
+        self._closed_days: set[str] = set()
+        # Monotonic stamp of the last read of the days still open.
+        self._daily_at: float | None = None
+        # The longest window anyone has asked for; older days are dropped.
+        self._daily_span = 0
         # (monotonic stamp, team/list rows).
         self._team_rows_cache: tuple[float, list[dict[str, Any]]] | None = None
         # model_name -> cost_basis, filled when routes are registered, so a
@@ -742,7 +745,7 @@ class LiteLLMBackend:
         self._raise_http(resp)
         self._team_rows_cache = None
         _LOGGER.info("tier %s updated: %s", tier, sorted(changes))
-        # The cap is re-read on every check, but cached buckets are not --
+        # The cap is re-read on every check, but today's usage is cached --
         # without this an admin lowering a cap sees a stale remaining figure
         # for up to a minute and concludes the control did not work.
         self.invalidate_allowance()
@@ -982,10 +985,31 @@ class LiteLLMBackend:
         resp = await self._request("POST", "key/delete", json={"keys": [token_id]})
         self._raise_http(resp)
 
-    # Spend reads are cached for the same reason allowance reads are: a page
-    # refresh should not cost a log scan. Short enough that a running job's
-    # draw appears while it is still running.
+    # Usage is read from LiteLLM's daily aggregate, never from the raw log.
+    #
+    # /spend/logs was the first choice and the wrong one. It filters by date
+    # only when BOTH start_date and end_date are sent; given one, it ignores
+    # it and returns the 10,000 most recent requests. That took twenty
+    # seconds on the pilot fleet and was not "this week" at all -- it reached
+    # back further on a quiet week and would have fallen short on a busy one.
+    #
+    # /user/daily/activity is one row per (day, model, key), a second for the
+    # whole fleet's week, and it carries each key's owner and team.
+    #
+    # A day that has ended is history: nothing will be added to it, so it is
+    # read once and kept. Only the days still open are refetched, and those
+    # are cached for a minute -- short enough that a running job's draw
+    # appears while it is still running.
     _SPEND_TTL_S = 60
+    _SPEND_DAYS = 7
+
+    # LiteLLM files a request under the day it STARTED, and writes the
+    # aggregate from a background queue. So a day goes on changing a little
+    # after midnight, until the last request begun in it has finished and
+    # been flushed. An hour is generous for both.
+    _DAY_CLOSE_GRACE_S = 3600
+
+    _DAILY_PAGE_SIZE = 1000
 
     async def usage(self, project: str | None) -> list[dict]:
         """Spend rows for a project, or across all projects when None.
@@ -1021,90 +1045,157 @@ class LiteLLMBackend:
         return out
 
     async def _spend_rows(self) -> list[dict]:
-        """This week's spend log, normalised and cached.
+        """This week's usage, one row per (user, project, model, key).
 
-        LiteLLM's /spend/logs returns one row per request. A busy week is
-        thousands of rows, so they are folded to one per
-        (user, project, model, key) before anything else sees them --
-        otherwise every caller pays to iterate the raw log.
+        The daily rows carry a date as well; it is folded away here so that
+        no caller pays to add seven days together.
         """
-        now = time.monotonic()
-        if self._spend_cache is not None:
-            at, rows = self._spend_cache
-            if (now - at) < self._SPEND_TTL_S:
-                return rows
+        daily = await self._daily_rows(self._SPEND_DAYS)
 
-        start = _dt.datetime.now(_dt.UTC).date() - _dt.timedelta(days=6)
-        resp = await self._request(
-            "GET",
-            "spend/logs",
-            params={"start_date": start.isoformat()},
-        )
-        self._raise_http(resp)
-        raw = resp.json()
-        if isinstance(raw, dict):
-            raw = raw.get("data") or raw.get("logs") or []
-        if not isinstance(raw, list):
-            raw = []
+        # Rows name a team by id; every page groups by project. Resolved here
+        # rather than when the day is stored, so a day that closed before its
+        # team was known is not left without a project for good.
+        seen = {r["team_id"] for rows in daily.values() for r in rows if r["team_id"]}
+        if seen - set(self._team_ids.values()):
+            await self._team_list_rows()
+        project_of = {tid: project for project, tid in self._team_ids.items()}
 
         by_key: dict[tuple, dict] = {}
-        for row in raw:
-            if not isinstance(row, dict):
-                continue
-            meta = row.get("metadata") or {}
-            if not isinstance(meta, dict):
-                meta = {}
-            # user_id is the request's principal; metadata.project is set by
-            # llmao when the key is minted. A row with neither is a call made
-            # with a key llmao did not issue, which is worth keeping rather
-            # than dropping -- it still consumed the fleet.
-            user = row.get("user") or row.get("user_id")
-            project = meta.get("project") or row.get("team_alias")
-            model = row.get("model")
-            token = row.get("api_key") or row.get("token")
-
-            total = row.get("total_tokens")
-            if total is None:
-                total = (row.get("prompt_tokens") or 0) + (row.get("completion_tokens") or 0)
-
-            k = (user, project, model, token)
-            acc = by_key.get(k)
-            if acc is None:
-                acc = by_key[k] = {
-                    "user": user,
-                    "project": project,
-                    "model": model,
-                    "token_id": token,
-                    "total_tokens": 0,
-                    "total_cost_usd": 0.0,
-                    "cost_basis": self._cost_basis.get(model),
-                    "requests": 0,
-                }
-            try:
-                acc["total_tokens"] += int(total or 0)
-                acc["total_cost_usd"] += float(row.get("spend") or 0.0)
-                acc["requests"] += 1
-            except (TypeError, ValueError):
-                continue
-
-        rows = list(by_key.values())
-        self._spend_cache = (now, rows)
-        return rows
+        for rows in daily.values():
+            for row in rows:
+                project = project_of.get(row["team_id"])
+                k = (row["user"], project, row["model"], row["token_id"])
+                acc = by_key.get(k)
+                if acc is None:
+                    acc = by_key[k] = {
+                        "user": row["user"],
+                        "project": project,
+                        "model": row["model"],
+                        "token_id": row["token_id"],
+                        "total_tokens": 0,
+                        "total_cost_usd": 0.0,
+                        "cost_basis": self._cost_basis.get(row["model"]),
+                        "requests": 0,
+                    }
+                acc["total_tokens"] += row["total_tokens"]
+                acc["total_cost_usd"] += row["total_cost_usd"]
+                acc["requests"] += row["requests"]
+        return list(by_key.values())
+
+    async def _daily_rows(self, days: int) -> dict[str, list[dict]]:
+        """Usage rows for each of the last `days` UTC days, today included.
+
+        Closed days come from memory. The fetch covers the earliest day still
+        open through today: only today in the steady state, yesterday as well
+        for the first hour after midnight, the whole window once at startup.
+
+        No coordination between callers. Two that find the open days stale
+        both ask, and the later answer is kept.
+        """
+        now = self._now()
+        today = now.date()
+        wanted = [(today - _dt.timedelta(days=i)).isoformat() for i in range(days)]
+        self._daily_span = max(self._daily_span, days)
+
+        open_days = [d for d in wanted if d not in self._closed_days]
+        fresh = self._daily_at is not None and (time.monotonic() - self._daily_at) < self._SPEND_TTL_S
+        if not fresh or any(d not in self._daily for d in wanted):
+            start = _dt.date.fromisoformat(min(open_days))
+            fetched = await self._fetch_daily(start, today)
+            grace = _dt.timedelta(seconds=self._DAY_CLOSE_GRACE_S)
+            day = start
+            while day <= today:
+                iso = day.isoformat()
+                # A day with no traffic is absent from the answer, and that
+                # absence is the answer: store it empty rather than not at all.
+                self._daily[iso] = fetched.get(iso, [])
+                day += _dt.timedelta(days=1)
+                ended = _dt.datetime.combine(day, _dt.time(), tzinfo=_dt.UTC)
+                if now >= ended + grace:
+                    self._closed_days.add(iso)
+            self._daily_at = time.monotonic()
+
+            oldest = (today - _dt.timedelta(days=self._daily_span - 1)).isoformat()
+            for iso in [d for d in self._daily if d < oldest]:
+                del self._daily[iso]
+                self._closed_days.discard(iso)
+
+        return {d: self._daily.get(d, []) for d in wanted}
+
+    def _now(self) -> _dt.datetime:
+        return _dt.datetime.now(_dt.UTC)
+
+    async def _fetch_daily(self, start: _dt.date, end: _dt.date) -> dict[str, list[dict]]:
+        """One read of LiteLLM's daily aggregate, as rows grouped by date.
+
+        The answer nests day -> model group -> key. The model GROUP is the
+        route name llmao registered, which is what cost_basis is keyed on;
+        the `models` breakdown beside it names the provider's model instead.
+        """
+        out: dict[str, list[dict]] = {}
+        page = 1
+        while True:
+            resp = await self._request(
+                "GET",
+                "user/daily/activity",
+                params={
+                    "start_date": start.isoformat(),
+                    "end_date": end.isoformat(),
+                    "page": page,
+                    "page_size": self._DAILY_PAGE_SIZE,
+                },
+            )
+            self._raise_http(resp)
+            body = resp.json()
+            if not isinstance(body, dict):
+                break
+            for result in body.get("results") or []:
+                if not isinstance(result, dict) or not result.get("date"):
+                    continue
+                rows = out.setdefault(str(result["date"])[:10], [])
+                groups = (result.get("breakdown") or {}).get("model_groups") or {}
+                for model, group in groups.items():
+                    keys = group.get("api_key_breakdown") if isinstance(group, dict) else None
+                    if not isinstance(keys, dict):
+                        continue
+                    for token, entry in keys.items():
+                        if not isinstance(entry, dict):
+                            continue
+                        metrics = entry.get("metrics") or {}
+                        meta = entry.get("metadata") or {}
+                        total = metrics.get("total_tokens")
+                        if total is None:
+                            total = (metrics.get("prompt_tokens") or 0) + (metrics.get("completion_tokens") or 0)
+                        try:
+                            row = {
+                                # The key's owner and team. A key llmao did
+                                # not issue has neither, and is kept rather
+                                # than dropped -- it still consumed the fleet.
+                                "user": meta.get("user_id"),
+                                "team_id": str(meta["team_id"]) if meta.get("team_id") else None,
+                                "model": model,
+                                "token_id": token,
+                                "total_tokens": int(total or 0),
+                                "total_cost_usd": float(metrics.get("spend") or 0.0),
+                                "requests": int(metrics.get("api_requests") or 0),
+                            }
+                        except (TypeError, ValueError):
+                            continue
+                        rows.append(row)
+            if not (body.get("metadata") or {}).get("has_more"):
+                break
+            page += 1
+        return out
 
     def invalidate_spend(self) -> None:
-        """Drop the cached spend rows. Paired with invalidate_allowance."""
-        self._spend_cache = None
-
-    # Allowance reads are cached: a committer refreshing a page should not
-    # cost a spend-log scan each time. Short enough that a long job's draw
-    # shows up while it is still running.
-    _ALLOWANCE_TTL_S = 60
+        """Have the next read ask again. Closed days are history and stay."""
+        self._daily_at = None
 
     async def allowance(self, user: str, *, tier: str = TIER_FREE) -> RollingAllowance:
-        """A principal's rolling allowance, derived from the spend log.
+        """A principal's rolling allowance, derived from recorded usage.
 
-        NOT stored. The buckets are computed from LiteLLM's own spend rows on
-        read, which has two properties worth the query cost:
+        NOT stored. The buckets are computed from LiteLLM's own daily usage
+        on read, which has two properties worth the query cost:
 
         The allowance cannot drift from actual usage, because it IS actual
         usage. A stored counter can disagree with the log after a crash, a
@@ -1117,10 +1208,13 @@ class LiteLLMBackend:
         table to drop and a UI to unwind. Derived, it just stops being
         called.
 
-        The cost is a query per principal per TTL. On a pilot-sized spend log
-        that is cheap; at scale it wants either LiteLLM's daily aggregate
-        table or a materialised counter, and the shape here does not change
-        if that happens.
+        It costs no query of its own: the buckets are this person's share of
+        the same daily rows the spend pages read.
+
+        Counts TOTAL tokens -- prompt plus completion -- because the
+        allowance is a share of the fleet and both halves consume it. Prefill
+        is cheaper per token than decode, which is why the DOLLAR figures are
+        weighted and this one is not: an allowance is a quota, not a bill.
         """
         user = (user or "").strip()
         if not user:
@@ -1135,66 +1229,21 @@ class LiteLLMBackend:
             # silently never exhausts.
             raise BackendUnavailableError(f"tier {tier} has no token_cap; nothing to meter against")
 
-        cached = self._allowance_cache.get(user)
-        if cached is not None:
-            at, rows = cached
-            if (time.monotonic() - at) < self._ALLOWANCE_TTL_S:
-                return RollingAllowance(cap=cap, window_days=window, buckets=dict(rows))
-
-        buckets = await self._spend_buckets(user, window)
-        self._allowance_cache[user] = (time.monotonic(), dict(buckets))
-        return RollingAllowance(cap=cap, window_days=window, buckets=buckets)
-
-    async def _spend_buckets(self, user: str, window_days: int) -> dict[str, int]:
-        """Daily token totals for one user over the window.
-
-        Counts TOTAL tokens -- prompt plus completion -- because the
-        allowance is a share of the fleet and both halves consume it. Prefill
-        is cheaper per token than decode, which is why the DOLLAR figures are
-        weighted and this one is not: an allowance is a quota, not a bill.
-        """
-        start = _dt.datetime.now(_dt.UTC).date() - _dt.timedelta(days=window_days - 1)
-        resp = await self._request(
-            "GET",
-            "spend/logs",
-            params={"user_id": user, "start_date": start.isoformat()},
-        )
-        self._raise_http(resp)
-        rows = resp.json()
-        if isinstance(rows, dict):
-            rows = rows.get("data") or rows.get("logs") or []
-        if not isinstance(rows, list):
-            return {}
-
         buckets: dict[str, int] = {}
-        for row in rows:
-            if not isinstance(row, dict):
-                continue
-            stamp = row.get("startTime") or row.get("start_time")
-            if not stamp:
-                continue
-            day = str(stamp)[:10]
-            total = row.get("total_tokens")
-            if total is None:
-                total = (row.get("prompt_tokens") or 0) + (row.get("completion_tokens") or 0)
-            try:
-                buckets[day] = buckets.get(day, 0) + int(total)
-            except (TypeError, ValueError):
-                continue
-        return buckets
+        for day, rows in (await self._daily_rows(window)).items():
+            spent = sum(r["total_tokens"] for r in rows if r["user"] == user)
+            if spent:
+                buckets[day] = spent
+        return RollingAllowance(cap=cap, window_days=window, buckets=buckets)
 
     def invalidate_allowance(self, user: str | None = None) -> None:
-        """Drop cached allowance rows.
+        """Have the next allowance read ask again.
 
-        Called after a tier change, since the cap is re-read on every check
-        but the cached buckets are not. Without this an admin lowering a cap
-        sees the old remaining figure for up to a minute, which looks like
-        the control not working.
+        Called after a tier change. The cap and window are re-read on every
+        check, so this is only so that a figure an admin has just acted on is
+        not a minute old when they look at it.
         """
-        if user is None:
-            self._allowance_cache.clear()
-        else:
-            self._allowance_cache.pop(user, None)
+        self._daily_at = None
 
     def deployment_body(self, dep, *, api_key: str | None = None) -> dict:
         """POST /model/new payload from models.yaml + this deployment's api_base.
diff --git a/tests/test_spend_rows.py b/tests/test_spend_rows.py
index d2af8f0..94a2ba5 100644
--- a/tests/test_spend_rows.py
+++ b/tests/test_spend_rows.py
@@ -15,35 +15,77 @@
 # specific language governing permissions and limitations
 # under the License.
 
-"""Folding the spend log into rows three pages can group.
+"""Reading usage from LiteLLM's daily aggregate, for three pages to group.
 
 The arithmetic here is invisible when it is wrong: a page renders, the
 numbers look plausible, and nobody can tell a dropped row from a quiet week.
 """
 
 import asyncio
+import datetime as dt
 import types
 
 from llmao.litellm_client import LiteLLMBackend
 
+NOON = dt.datetime(2026, 10, 12, 12, 0, tzinfo=dt.UTC)
+TODAY = "2026-10-12"
+YESTERDAY = "2026-10-11"
 
-def _backend(raw_rows):
-    """A backend whose only live part is the fold.
+# project -> team id, as team/list would have filled it
+TEAMS = {"tooling": "team-tooling", "mahout": "team-mahout"}
+
+
+def _entry(tokens=None, *, user=None, project=None, spend=0.0, requests=1, **metrics):
+    """One key's figures for one model on one day, as LiteLLM nests them."""
+    m = {"spend": spend, "api_requests": requests, **metrics}
+    if tokens is not None:
+        m["total_tokens"] = tokens
+    return {
+        "metrics": m,
+        "metadata": {"user_id": user, "team_id": TEAMS.get(project), "key_alias": None},
+    }
+
+
+def _day(date, groups):
+    """A day of the answer: {model group: {key: entry}}."""
+    return {
+        "date": date,
+        "breakdown": {"model_groups": {g: {"api_key_breakdown": keys} for g, keys in groups.items()}},
+    }
+
+
+def _backend(days, *, now=NOON, page_size=1000):
+    """A backend whose only live part is the read and the fold.
 
     Constructed without __init__ so the test needs no config, no fleet and no
-    HTTP client -- the thing under test is pure data shaping.
+    HTTP client. The fake honours the date range it is asked for, because
+    which days get asked for is half of what is under test.
     """
     b = object.__new__(LiteLLMBackend)
-    b._spend_cache = None
+    b._daily = {}
+    b._closed_days = set()
+    b._daily_at = None
+    b._daily_span = 0
+    b._team_ids = dict(TEAMS)
     b._cost_basis = {"gemma4-26b": "derived_capacity"}
-    b._SPEND_TTL_S = 60
-
-    async def fake_request(method, path, **kw):
-        return types.SimpleNamespace(
-            status_code=200,
-            content=b"x",
-            json=lambda: raw_rows,
-        )
+    b._DAILY_PAGE_SIZE = page_size
+    b._now = lambda: now
+    b.asked = []
+
+    async def fake_request(method, path, params=None, **kw):
+        b.asked.append((path, dict(params or {})))
+        hits = [
+            d
+            for d in days
+            # something that is not a day at all is passed through, to be ignored
+            if not isinstance(d, dict) or params["start_date"] <= d["date"] <= params["end_date"]
+        ]
+        first = (params["page"] - 1) * params["page_size"]
+        body = {
+            "results": hits[first : first + params["page_size"]],
+            "metadata": {"has_more": first + params["page_size"] < len(hits)},
+        }
+        return types.SimpleNamespace(status_code=200, content=b"x", json=lambda: body)
 
     b._request = fake_request
     b._raise_http = lambda resp: None
@@ -54,107 +96,76 @@ def _run(coro):
     return asyncio.run(coro)
 
 
-LOG = [
-    # one person, one model, two calls -- must fold to a single row
-    {
-        "user": "akm",
-        "model": "gemma4-26b",
-        "api_key": "tok1",
-        "spend": 0.0,
-        "total_tokens": 100,
-        "metadata": {"project": "tooling"},
-    },
-    {
-        "user": "akm",
-        "model": "gemma4-26b",
-        "api_key": "tok1",
-        "spend": 0.0,
-        "total_tokens": 50,
-        "metadata": {"project": "tooling"},
-    },
-    # same person, different key -- must NOT fold together
-    {
-        "user": "akm",
-        "model": "gemma4-26b",
-        "api_key": "tok2",
-        "spend": 0.0,
-        "total_tokens": 7,
-        "metadata": {"project": "tooling"},
-    },
-    # different project
-    {
-        "user": "akm",
-        "model": "gemma4-26b",
-        "api_key": "tok3",
-        "spend": 0.0,
-        "total_tokens": 300,
-        "metadata": {"project": "mahout"},
-    },
-    # someone else
-    {
-        "user": "gstein",
-        "model": "gemma4-26b",
-        "api_key": "tok4",
-        "spend": 0.0,
-        "total_tokens": 900,
-        "metadata": {"project": "tooling"},
-    },
-    # a commercial row: real money, no cost_basis tag
-    {
-        "user": "akm",
-        "model": "claude-opus-5",
-        "api_key": "tok1",
-        "spend": 1.25,
-        "total_tokens": 40,
-        "metadata": {"project": "tooling"},
-    },
+WEEK = [
+    _day(
+        TODAY,
+        {
+            "gemma4-26b": {
+                # one person, one model, one key, two days -- must fold to a single row
+                "tok1": _entry(100, user="akm", project="tooling", requests=1),
+                # same person, different key -- must NOT fold together
+                "tok2": _entry(7, user="akm", project="tooling"),
+                # someone else
+                "tok4": _entry(900, user="gstein", project="tooling"),
+            },
+            # a commercial row: real money, no cost_basis tag
+            "claude-opus-5": {"tok1": _entry(40, user="akm", project="tooling", spend=1.25)},
+        },
+    ),
+    _day(
+        YESTERDAY,
+        {
+            "gemma4-26b": {
+                "tok1": _entry(50, user="akm", project="tooling", requests=1),
+                # different project
+                "tok3": _entry(300, user="akm", project="mahout"),
+            },
+        },
+    ),
 ]
 
 
 def test_rows_fold_by_user_project_model_and_key():
-    rows = _run(_backend(LOG)._spend_rows())
-    # six log lines, five distinct (user, project, model, key) combinations
+    rows = _run(_backend(WEEK)._spend_rows())
+    # six entries over two days, five distinct (user, project, model, key) combinations
     assert len(rows) == 5
     folded = [r for r in rows if r["token_id"] == "tok1" and r["model"] == "gemma4-26b"]
     assert len(folded) == 1
     assert folded[0]["total_tokens"] == 150
     assert folded[0]["requests"] == 2
+    assert folded[0]["project"] == "tooling"
 
 
 def test_prompt_and_completion_are_summed_when_total_is_absent():
     """LiteLLM does not always send total_tokens."""
-    rows = _run(
-        _backend(
-            [
-                {
-                    "user": "akm",
-                    "model": "gemma4-26b",
-                    "api_key": "t",
-                    "spend": 0.0,
-                    "prompt_tokens": 40000,
-                    "completion_tokens": 11000,
-                    "metadata": {"project": "tooling"},
-                },
-            ]
-        )._spend_rows()
-    )
+    days = [
+        _day(
+            TODAY,
+            {
+                "gemma4-26b": {
+                    "t": _entry(user="akm", project="tooling", prompt_tokens=40000, completion_tokens=11000),
+                }
+            },
+        )
+    ]
+    rows = _run(_backend(days)._spend_rows())
     assert rows[0]["total_tokens"] == 51000
 
 
 def test_usage_filters_by_project():
-    b = _backend(LOG)
+    b = _backend(WEEK)
     tooling = _run(b.usage("tooling"))
     assert {r["project"] for r in tooling} == {"tooling"}
     assert sum(r["total_tokens"] for r in tooling) == 150 + 7 + 900 + 40
 
 
 def test_usage_with_none_returns_every_project():
-    b = _backend(LOG)
+    b = _backend(WEEK)
     assert {r["project"] for r in _run(b.usage(None))} == {"tooling", "mahout"}
 
 
 def test_key_usage_groups_by_key_for_one_person():
-    b = _backend(LOG)
+    b = _backend(WEEK)
     got = _run(b.key_usage("akm"))
     assert got == {"tok1": 150 + 40, "tok2": 7, "tok3": 300}
     assert "tok4" not in got, "another person's key must not appear"
@@ -167,7 +178,7 @@ def test_cost_basis_distinguishes_capacity_from_money():
     utilisation; a commercial row's is an amount payable. A page that sums
     them without checking produces a figure that means nothing.
     """
-    rows = _run(_backend(LOG)._spend_rows())
+    rows = _run(_backend(WEEK)._spend_rows())
     selfhosted = [r for r in rows if r["model"] == "gemma4-26b"]
     commercial = [r for r in rows if r["model"] == "claude-opus-5"]
     assert all(r["cost_basis"] == "derived_capacity" for r in selfhosted)
@@ -176,61 +187,160 @@ def test_cost_basis_distinguishes_capacity_from_money():
     assert real_money == 1.25
 
 
-def test_a_row_llmao_did_not_issue_is_kept():
-    """No project, no user metadata -- but it still consumed the fleet.
+def test_a_key_llmao_did_not_issue_is_kept():
+    """No owner, no team -- but it still consumed the fleet.
 
     Dropping it would make the project figures add up while the fleet total
     quietly disagreed, which is the harder bug to find.
     """
-    rows = _run(
-        _backend(
-            [
-                {"model": "gemma4-26b", "api_key": "stray", "spend": 0.0, "total_tokens": 500},
-            ]
-        )._spend_rows()
-    )
+    rows = _run(_backend([_day(TODAY, {"gemma4-26b": {"stray": _entry(500)}})])._spend_rows())
     assert len(rows) == 1
     assert rows[0]["total_tokens"] == 500
     assert rows[0]["project"] is None
+    assert rows[0]["user"] is None
+
+
+def test_a_team_not_yet_listed_is_looked_up():
+    """A project created since the last listing must not show as no project."""
+    b = _backend([_day(TODAY, {"gemma4-26b": {"t": _entry(5, user="akm", project="tooling")}})])
+    b._team_ids = {}
+
+    async def listing():
+        b._team_ids.update(TEAMS)
+        return []
+
+    b._team_list_rows = listing
+    assert _run(b._spend_rows())[0]["project"] == "tooling"
 
 
 def test_second_read_is_cached():
-    """A page refresh must not cost another scan."""
-    calls = []
-    b = _backend(LOG)
-    inner = b._request
+    """A page refresh must not cost another read."""
+    b = _backend(WEEK)
+    _run(b._spend_rows())
+    _run(b._spend_rows())
+    assert len(b.asked) == 1
+    b.invalidate_spend()
+    _run(b._spend_rows())
+    assert len(b.asked) == 2
+
+
+def test_malformed_entries_do_not_abort_the_read():
+    """One bad entry must not cost the whole page."""
+    days = [
+        _day(
+            TODAY,
+            {
+                "m": {"t": _entry("not a number", user="akm", project="tooling")},
+                "m2": {"t2": _entry(10, user="akm", project="tooling"), "t3": "a string where a dict should be"},
+                "m3": "a string where a group should be",
+            },
+        ),
+        "a string where a day should be",
+    ]
+    rows = _run(_backend(days)._spend_rows())
+    assert [r["total_tokens"] for r in rows] == [10]
+
+
+def test_every_page_is_read():
+    """A busy week must not be silently cut at the first page."""
+    days = [
+        _day(f"2026-10-{d:02d}", {"gemma4-26b": {"t": _entry(1, user="akm", project="tooling")}}) for d in (10, 11, 12)
+    ]
+    b = _backend(days, page_size=2)
+    rows = _run(b._spend_rows())
+    assert rows[0]["total_tokens"] == 3
+    assert [p["page"] for _, p in b.asked] == [1, 2]
 
-    async def counting(*a, **kw):
-        calls.append(1)
-        return await inner(*a, **kw)
 
-    b._request = counting
+# ---------------------------------------------------------------------------
+# Which days are asked for
+#
+# /spend/logs ignored a start_date sent on its own and returned the last
+# 10,000 requests, so "this week" was whatever that happened to span. Here
+# both ends are always sent, and a day that has ended is read once.
+# ---------------------------------------------------------------------------
+
+
+def test_the_first_read_asks_for_exactly_the_week():
+    b = _backend(WEEK)
     _run(b._spend_rows())
+    assert b.asked == [
+        ("user/daily/activity", {"start_date": "2026-10-06", "end_date": TODAY, "page": 1, "page_size": 1000})
+    ]
+
+
+def test_a_closed_day_is_not_read_again():
+    """History does not change. Only today is worth asking about twice."""
+    b = _backend(WEEK)
+    _run(b._spend_rows())
+    b.invalidate_spend()
+    rows = _run(b._spend_rows())
+    assert b.asked[-1][1]["start_date"] == TODAY
+    # ...and yesterday's figures are still in the answer
+    assert sum(r["total_tokens"] for r in rows) == 100 + 7 + 900 + 40 + 50 + 300
+
+
+def test_yesterday_stays_open_for_an_hour_after_midnight():
+    """A request begun at 23:59 is filed under yesterday when it finishes."""
+    b = _backend(WEEK, now=dt.datetime(2026, 10, 12, 0, 30, tzinfo=dt.UTC))
     _run(b._spend_rows())
-    assert len(calls) == 1
     b.invalidate_spend()
     _run(b._spend_rows())
-    assert len(calls) == 2
+    assert b.asked[-1][1]["start_date"] == YESTERDAY
 
+    b._now = lambda: dt.datetime(2026, 10, 12, 1, 30, tzinfo=dt.UTC)
+    b.invalidate_spend()
+    _run(b._spend_rows())  # read once more after the hour, which closes it
+    b.invalidate_spend()
+    _run(b._spend_rows())
+    assert b.asked[-1][1]["start_date"] == TODAY
 
-def test_malformed_rows_do_not_abort_the_fold():
-    """One bad row must not cost the whole page."""
-    rows = _run(
-        _backend(
-            [
-                {
-                    "user": "akm",
-                    "model": "m",
-                    "api_key": "t",
-                    "total_tokens": "not a number",
-                    "metadata": {"project": "p"},
-                },
-                {"user": "akm", "model": "m2", "api_key": "t2", "total_tokens": 10, "metadata": {"project": "p"}},
-                "a string where a dict should be",
-            ]
-        )._spend_rows()
-    )
-    assert any(r["total_tokens"] == 10 for r in rows)
+
+def test_a_day_that_leaves_the_window_is_dropped():
+    b = _backend(WEEK)
+    _run(b._spend_rows())
+    b._now = lambda: NOON + dt.timedelta(days=7)
+    rows = _run(b._spend_rows())
+    assert rows == []
+    assert min(b._daily) == "2026-10-13"
+
+
+# ---------------------------------------------------------------------------
+# The allowance is the same rows, one person's share, by day
+# ---------------------------------------------------------------------------
+
+
+def _allowance_backend(days, *, window=7):
+    b = _backend(days)
+
+    async def tier_entitlement(tier):
+        return {"token_cap": 1_000_000, "token_window_days": window}
+
+    b.tier_entitlement = tier_entitlement
+    return b
+
+
+def test_allowance_buckets_are_one_persons_tokens_by_day():
+    a = _run(_allowance_backend(WEEK).allowance("akm"))
+    assert a.buckets == {TODAY: 100 + 7 + 40, YESTERDAY: 50 + 300}
+    assert a.cap == 1_000_000
+
+
+def test_allowance_costs_no_read_of_its_own():
+    """It was a second scan of the log, per person. Now it is the same rows."""
+    b = _allowance_backend(WEEK)
+    _run(b._spend_rows())
+    _run(b.allowance("akm"))
+    _run(b.allowance("gstein"))
+    assert len(b.asked) == 1
+
+
+def test_a_longer_allowance_window_reads_the_extra_days():
+    b = _allowance_backend(WEEK, window=10)
+    _run(b._spend_rows())
+    _run(b.allowance("akm"))
+    assert b.asked[-1][1]["start_date"] == "2026-10-03"
+    assert len(b.asked) == 2
 
 
 # ---------------------------------------------------------------------------


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tooling.apache.org
For additional commands, e-mail: commits-help@tooling.apache.org