Skip to content

Count Daily AI Gateway Usage by Agent UID, User, and Copilot Route - #668

Merged
Pengfei Ni (feiskyer) merged 2 commits into
mainfrom
feat/gateway-daily-usage-v2
Nov 25, 2026
Merged

Pengfei Ni (feiskyer) merged 2 commits into
mainfrom
feat/gateway-daily-usage-v2

Conversation

@Zhupku

@Zhupku Zhupku commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The daily AI Gateway usage view GatewayDailyUsage cannot answer two questions that a usage report needs:

  • Who used it. The view groups by the Agent's name and by the owner's alias (cetirun_user in the key metadata). Both can repeat: a new Agent can take the name of a deleted Agent, and users from two domains can have the same alias. The view has no Agent UID and no user ID.
  • Who paid for it. A GitHub Copilot model of the owner and a shared model with the same name are served under one model name. The view groups by that name only, so it adds the calls of the owner's Copilot route (paid by the owner's Copilot subscription, and failures of that route) to the calls of the shared deployments.

Kusto cannot change how a materialized view groups its rows (.alter materialized-view cannot change the group-by keys), and the template creates the view with .create ifnotexists. This PR therefore adds a new view, GatewayDailyUsageV2, next to the earlier one.

Changes

  • infra/cluster/modules/adx/cetirun-gateway-usage.kql: creates GatewayDailyUsageV2 with the same retention parameter and hot cache. Its keys are the earlier ones plus:
    • AgentUid, from the key metadata cetirun_agent_uid.
    • UserId, the AI Gateway user of the key. Since v0.2.0 (Use Verified Owner IDs for Portal and Agent Access #628) this is <tenant>/<object> of the owner, or agent:<Agent UID> for an Agent without owner IDs. Before v0.2.0 it was the alias; those rows keep it.
    • ProviderModel and Provider. For a router call, Model is the router and ProviderModel is the tier model that answered.
    • CopilotRoute: true when the deployment ID starts with cetirun-copilot-. The operator registers every Copilot route of an Agent with this prefix (copilotRegistrationID), and the request log records LiteLLM's model ID as deployment_id.
    • It also sums CacheCreationTokens, which GatewayRequests() already has.
  • No backfill. The view counts the calls from its creation. Kusto refuses to create a view with backfill while rows of its table are outside the hot cache (7 days in the template), so backfill = true fails the script on every existing database. The first deployment of this branch over a test cluster's logs database failed this way; the second commit removes it. infrastructure.md describes how a database admin creates the view again once with backfill, with the Gateway table in the hot cache for the time.
  • The script does not create, change or drop GatewayDailyUsage. Where an earlier template created it, it keeps its rows and keeps counting. The docs show how a database admin drops it.
  • GatewayRequests() gets the column CopilotRoute, with the same rule.
  • Docs: query functions table and two example queries (observability.md), the ADX resources, the earlier view and the optional backfill (infrastructure.md), the Copilot column in the spend query (budgets.md), the retention and the identities that the view keeps (PRIVACY.md), and a note that make deploy does not apply ADX script changes (upgrade.md).
  • Tests in scripts/tests/test_cluster_deploy.py: the new view name and policies, no backfill, that the script never names the earlier view, the new keys and aggregations, and that the Copilot prefix in both scripts matches the operator's prefix, its route ID format and the request log's deployment_id.

Upgrade

make deploy does not run the ADX scripts. An existing installation gets the new view when the cluster template is deployed again, or when an administrator runs cetirun-logs.kql and cetirun-gateway-usage.kql in the logs database. The new view then counts the calls from that time. The earlier days stay in GatewayDailyUsage, and in GatewayRequests() for the logs retention, unless an admin backfills the new view as infrastructure.md describes.

Validation

Offline:

  • python3 -m unittest scripts.tests.test_cluster_deploy in a Linux container with Bicep 0.47.16 and PyYAML: 28 tests OK, none skipped.
  • Mutation check of the new tests: 14 of 14 mutations fail a test (removed keys, another prefix in either script, the earlier view name, a drop of the earlier view, a backfill, another prefix or route ID format in the operator, another request log field).
  • bicep build --no-restore of main.bicep and every module: OK, no warnings.
  • site/tests: 157 tests OK. Source-level site checks: 0 content errors, no new style findings in the changed pages.
  • scripts/ci/validate.py license-check and python-check, git diff --check: OK.

Live, on a test cluster with CetiRun v0.2.2 from MCR, ADX-Mon v0.4.1, and a Dev SKU Kusto cluster whose Gateway table had rows since 2026-11-06:

  • Template over existing databases. The ADX module of this branch, deployed with the parameters of the earlier deployment: with backfill it failed ("Materialized view creation with backfill option is not supported for tables with extents in cold cache"); without backfill it succeeded. GatewayDailyUsageV2 is healthy with 400 days of retention and 31 hot days, GatewayDailyUsage is unchanged, and GatewayRequests() has CopilotRoute.
  • Optional backfill. The procedure of infrastructure.md, run as written: the recreated view counted 26,701 calls and 4.154529 USD from 2026-11-06, the same as GatewayDailyUsage; the table's caching policy was removed again.
  • G16. Three temporary keys made like the operator's (synthetic IDs, deleted afterwards): one Agent name with two Agent UIDs of user A, and an Agent of user B, whose alias is the same as user A's. GatewayDailyUsage shows one owner with 6 calls and one Agent name with 3 calls. GatewayDailyUsageV2 shows the two users with 3 calls each, and the three Agent UIDs with 2, 1 and 3 calls. The real Agents of the cluster show the three forms of UserId: the alias before the upgrade, agent:<UID> for Agents without owner IDs, and <tenant>/<object> for an Agent with them.
  • G11. A temporary Agent with a synthetic GitHub Copilot sign-in (a fake token, so its Copilot calls fail at GitHub; deleted afterwards). The operator merged its Copilot gpt-6-astra with the shared gpt-6-astra and registered claude-opus-5.5 as Copilot only. Of 34 requests for gpt-6-astra, 21 first went to the Copilot route, failed, and the shared deployment answered. GatewayDailyUsage shows gpt-6-astra with 55 calls, 21 of them failed. GatewayDailyUsageV2 shows 34 successful calls with CopilotRoute = false and 21 failed tries with CopilotRoute = true, and the 2 claude-opus-5.5 tries with CopilotRoute = true.
  • G11 with a real sign-in. A second temporary Agent, with a real GitHub Copilot sign-in of its owner (24 Copilot routes; deleted afterwards). Its calls to claude-haiku-5.5 (2), gemini-3.8-flash (1) and the merged gpt-6-astra (8) were all answered by its Copilot routes, at the list price. For the client calls of gpt-6-astra that day, GatewayDailyUsage has one row: 77 calls, 25 failed, 0.02032 USD. GatewayDailyUsageV2 has 44 calls of the shared deployment (0.0166 USD, none failed) and 33 of Copilot routes (8 answered, 0.00372 USD at the list price; 25 failed tries of the synthetic sign-in).

Questions for review

  • The name GatewayDailyUsageV2. Another name works the same way.
  • Whether the template should backfill the view itself, by raising the Gateway table's hot cache for the creation and removing it after. This PR leaves that to an admin, because it changes a caching policy at every deployment.
  • Whether the template should ever drop GatewayDailyUsage itself. This PR keeps it, because a drop would erase days that are older than the request log.

GatewayDailyUsage groups by the Agent's name and the owner's alias. Both can
repeat: a new Agent can take the name of a deleted one, and users from two
domains can have the same alias. It also counts the calls that an owner's
GitHub Copilot route answered together with the shared deployments of the same
model name.

Kusto cannot change how a materialized view groups its rows, so the cluster
template now creates GatewayDailyUsageV2 next to it. The new view adds the
Agent UID, the AI Gateway user ID of the key, the provider and provider model,
CopilotRoute, and the cache creation tokens. It is backfilled from the rows
that the Gateway table still keeps. The script leaves the earlier view and its
rows alone; the docs say how to drop it.

GatewayRequests() gains CopilotRoute. The upgrade page says that make deploy
does not change the ADX scripts.
@Zhupku Zhupku self-assigned this Nov 25, 2026
…n existing databases

A redeployment of the cluster template over the logs database of a test cluster
failed at the view: Kusto refuses to create a materialized view with backfill
while rows of its table are outside the hot cache, and a Gateway table older
than the seven days of the hot cache has such rows. The script would fail at
every deployment of an existing installation.

The view now counts the calls from its creation. infrastructure.md describes
how a database admin creates it again once with backfill, with the Gateway table
in the hot cache for the time; on the test cluster this gave the same totals as
GatewayDailyUsage.

@feiskyer Pengfei Ni (feiskyer) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@feiskyer
Pengfei Ni (feiskyer) merged commit 3af85dc into main Nov 25, 2026
42 checks passed
@feiskyer
Pengfei Ni (feiskyer) deleted the feat/gateway-daily-usage-v2 branch November 25, 2026 12:02
Pengfei Ni (feiskyer) pushed a commit that referenced this pull request Nov 27, 2026
…Group (#669)

* Rebuild the daily usage view only while the Gateway table has all its days

The backfill procedure that #668 documents drops GatewayDailyUsageV2 and
creates it again from the Gateway table. The view keeps 400 days by default,
the table 90, so dropping a view that already has older days erases them, and
a warm cache does not bring back deleted rows.

The procedure now applies only while the table still has every day of the
view, with a query that compares the oldest row of the table with the first
day of the view.

* Keep the daily usage view until a backfilled copy accounts for every day

The comparison of the oldest Gateway row with the first day of the view does
not prove that the table still has every counted request: the timestamp
covers all Gateway logs, and the table deletes rows by the time of their
ingestion. The documented backfill now never drops GatewayDailyUsageV2. It
builds GatewayDailyUsageV2Backfill, compares the calls and the spend of each
day and cluster, and renames the views only when the copy has at least the
totals of every day. The previous view stays until an admin drops it.

* Compare every group of the daily usage view, and stage under an unused name

Review of e15c260:

- Merged extents keep older rows after newer, unmerged rows expire, so day
  and cluster totals can match while one Agent's calls are lost. The
  comparison now covers each group of GatewayDailyUsageV2 (its 12 keys) and
  all its counters, as the second view minus the current one.
- With .create ifnotexists, an existing GatewayDailyUsageV2Backfill was left
  as it was, and the next steps changed and dropped it. The second view now
  gets a dated name that the database does not have, is created with
  .create async (without ifnotexists, so the creation fails when the name
  exists), and the steps continue only when .show operations reports the
  operation as Completed. The current view is kept under a dated name too,
  and the page says what to do when a rename fails.

* Keep deployments out of the swap, and recover after an interrupted one

Review of 0ea5eb4: a deployment of the cluster template between the two
renames creates GatewayDailyUsageV2 again without backfill. Promotion and
rollback then both target an occupied name, and reports read the new view
without its history.

- The steps say not to deploy the cluster template while they run.
- Step 5 runs both renames in one .execute database script with
  ThrowOnErrors, so no command runs between them. The script stops at the
  first failure and does not undo the earlier rename.
- After a failed script, .show materialized-views lists the views with their
  EffectiveDateTime. Without the previous view, nothing was replaced. With it,
  a GatewayDailyUsageV2 next to it was created by a deployment (its
  EffectiveDateTime is after the start of step 5) and counts only calls that
  the other two views also count: drop it, then give the name back to the
  second view or to the previous one.

* Recover by the views that exist, and run one backfill at a time

Review of c66b21c:

- When both renames ran but the response was lost, a second run of the
  script fails at its first rename. The recovery then took the promoted view
  for a deployment's view and dropped it. The recovery now decides by the
  views that exist: without the second view and with the previous one, both
  renames ran, and GatewayDailyUsageV2 stays when its EffectiveDateTime is
  the one noted in step 3 (step 3 now notes it). GatewayDailyUsageV2 is
  dropped only while the second view still exists. Step 5 says to run the
  script only once.
- Two backfill runs with different staging names can both pass the
  comparison against the same view, and the later one can replace the
  earlier, more complete view. The page now says to run one backfill at a
  time and to list the views first (another GatewayDailyUsageV2Backfill view
  shows a run that has not ended), and step 5 follows the comparison right
  away.

---------

Co-authored-by: Scott Zhu <mengzezhu@microsoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants