Skip to content

AI Request Logs: Show usage by plugin, theme, or core source - #1418

Open
HILAYTRIVEDI wants to merge 12 commits into
WordPress:developfrom
HILAYTRIVEDI:feature/348-usage-by-source
Open

HILAYTRIVEDI wants to merge 12 commits into
WordPress:developfrom
HILAYTRIVEDI:feature/348-usage-by-source

Conversation

@HILAYTRIVEDI

@HILAYTRIVEDI HILAYTRIVEDI commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

What?

See #348

Adds a Usage by source breakdown to Tools → AI Request Logs, showing requests, tokens and failed requests per plugin, theme or WordPress core.

Why?

#348 calls for usage metering with per-plugin tracking. The request logger already detects which plugin or theme made each request, but the result was not stored in a queryable way, so admins could not see which extension was responsible for their AI usage.

How?

  • Moves source detection out of Logging_Http_Transporter into a new Request_Source_Detector class, used both by the HTTP transporter and by rows logged from generation events (Logging_Event_Listener).
  • Adds an indexed source column to the logs table (schema v2). The column and index are added in a single ALTER. Schema v2 is recorded only once the column exists; if the upgrade fails, logging continues without the column.
  • Overlong source keys are shortened on a UTF-8 boundary with a hash suffix, and the column uses a binary collation so distinct sources are never merged.
  • AI_Request_Log_Repository returns a by_source breakdown in the logs summary. Only the raw counts are cached; translated source names are resolved per request.
  • New SourceUsage React component renders the breakdown as a table on the logs page.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Help with implementation, tests and the PR description; all changes were reviewed, tested and edited by me.

Testing Instructions

  1. Enable the AI Request Logging experiment under Settings → AI.
  2. Trigger a few AI requests (e.g. from an AI plugin feature, and from another plugin or theme if available).
  3. Go to Tools → AI Request Logs.
  4. Confirm a Usage by source table is shown, listing each source with its type (Plugin / Theme / Core), request count, tokens and failed requests.
  5. Change the period selector and confirm the breakdown updates.
  6. On an existing install with logged data, confirm the table upgrades to schema v2 (a source column with an index) and logging keeps working.

Automated tests:

  • npm run test:e2e -- tests/e2e/specs/experiments/ai-request-logging.spec.js
  • npm run test:php -- --filter 'AI_Request_Log|Logging_'

Screenshots or screencast

Before After
Screenshot 2026-11-20 at 1 27 50 PM Screenshot 2026-11-20 at 1 28 15 PM

Changelog Entry

Added - AI Request Logs now show usage (requests, tokens, failures) broken down by plugin, theme or core source.

Open WordPress Playground Preview

Store the detected request source in an indexed `source` column and add a
`by_source` breakdown (requests, tokens, failed requests) to the logs
summary, shown as a "Usage by source" table on Tools → AI Request Logs.

- Move source detection into Request_Source_Detector and use it for rows
  logged from generation events as well as the HTTP transporter.
- Record schema v2 only once the column exists; keep logging without it if
  the upgrade fails, and add the column and index in one ALTER.
- Shorten overlong source keys on a UTF-8 boundary with a hash suffix and
  use a binary collation so distinct sources are never merged.
- Cache source counts only and resolve translated names per request.

See WordPress#348.
@HILAYTRIVEDI
HILAYTRIVEDI requested a review from a team November 20, 2026 07:52
@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: HILAYTRIVEDI <hilayt24@git.wordpress.org>
Co-authored-by: jeffpaul <jeffpaul@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.54545% with 15 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.96%. Comparing base (b2bdae5) to head (9b71d1f).

Files with missing lines Patch % Lines
includes/Logging/AI_Request_Log_Repository.php 90.35% 11 Missing ⚠️
includes/Logging/Request_Source_Detector.php 96.52% 4 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##             develop    #1418      +/-   ##
=============================================
+ Coverage      85.62%   85.96%   +0.33%     
- Complexity      5155     5217      +62     
=============================================
  Files            194      195       +1     
  Lines          18637    18784     +147     
=============================================
+ Hits           15958    16147     +189     
+ Misses          2679     2637      -42     
Flag Coverage Δ
unit 85.96% <94.54%> (+0.33%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Add Request_Source_DetectorTest so the detector is covered under
strict coverage, and cover the remaining source breakdown branches in
AI_Request_Log_Repository and AI_Request_Log_Schema.

See WordPress#348.
@jeffpaul

Copy link
Copy Markdown
Member
  1. maybe start with Usage by source section collapsed so that main table below is still farther up the page and not pushed down so far?
  2. maybe hyperlink on the values in the Source, Requests, and Failed columns such that clicking on any of those filters the table below to that info?

@jeffpaul jeffpaul moved this from Triage to Needs review in WordPress AI Roadmap Nov 30, 2026
@HILAYTRIVEDI

Copy link
Copy Markdown
Contributor Author
  1. maybe start with Usage by source section collapsed so that main table below is still farther up the page and not pushed down so far?
  2. maybe hyperlink on the values in the Source, Requests, and Failed columns such that clicking on any of those filters the table below to that info?

Thanks,@jeffpaul. Usage by source now starts collapsed so the main request log stays higher on the page. Clicking a source name or its request count shows that source’s requests for the selected period. Clicking the failed count shows only unsuccessful requests from that source, including timeouts. The active filter is visible above the table and can be cleared there.

jeffpaul
jeffpaul previously approved these changes Dec 1, 2026
…elop

# Conflicts:
#	includes/Logging/Logging_Http_Transporter.php
@HILAYTRIVEDI
HILAYTRIVEDI requested a review from jeffpaul December 3, 2026 10:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Needs review

Development

Successfully merging this pull request may close these issues.

3 participants