Skip to content

[Feature]: AI Request Logging#437

Merged
dkotter merged 77 commits into
WordPress:developfrom
TylerB24890:feature/ai-request-logging
May 19, 2026
Merged

[Feature]: AI Request Logging#437
dkotter merged 77 commits into
WordPress:developfrom
TylerB24890:feature/ai-request-logging

Conversation

@TylerB24890
Copy link
Copy Markdown
Contributor

@TylerB24890 TylerB24890 commented Apr 16, 2026

Original PR: #149

Screenshot 2026-05-07 at 11 16 00 AM

What?

Adds a comprehensive AI Request Logging experiment that provides observability for all AI operations:

  • Real-time logging of every AI request (provider, model, tokens, duration)
  • React-powered admin dashboard under Tools → AI Request Logs
  • REST API endpoints for programmatic access
  • Configurable retention period and automatic cleanup

Closes #345.

Why?

AI operations are opaque—users have no visibility into what requests are being made, how much they cost, or whether they're succeeding. This experiment provides the observability surface needed for debugging, cost tracking, and usage analysis.

How?

Architecture uses the decorator pattern to wrap the SDK's HTTP transporter:

  • Logging_Integration wraps the transporter on wp_loaded/admin_init using the public setHttpTransporter() API
  • Logging_Http_Transporter decorates requests, capturing timing and delegating to Log_Data_Extractor
  • Log_Data_Extractor parses request/response payloads with 4 filter hooks for extensibility:
    • ai_request_log_providers - customize provider detection
    • ai_request_log_context - filter context data
    • ai_request_log_tokens - custom token extraction
    • ai_request_log_kind - request type detection
  • AI_Request_Log_Manager coordinates schema, repository, and cost calculation
  • AI_Request_Cost_Calculator estimates costs based on provider pricing tables

Use of AI Tools

  • AI assistance: Yes
  • Tool(s): Claude
  • Model(s): Opus 4.7
  • Used for:
    • Thorough review and analysis of original PR codebase & changes
    • Assistance in updating unit tests
    • Assistance in tracking down duplicate logic/unnecessary function wrappers.

Testing Instructions

  1. Enable the AI Request Logging experiment in Settings → AI
  2. Trigger an AI-powered feature (e.g., Title Generation, Excerpt Generation)
  3. Navigate to the logs via Tools → AI Request Logs
  4. Verify the log table populates with request details (provider, model, tokens, duration)
  5. Test filtering by provider, status, and date range
  6. Verify "Purge logs" clears all entries
  7. Disable the experiment and confirm no new logs appear
Open WordPress Playground Preview

Jameswlepage and others added 16 commits March 25, 2026 16:52
- Replace hand-rolled table, filters, search, and pagination with
  @wordpress/dataviews for the Request Logs screen
- Bundle @wordpress/dataviews and @wordpress/ui (not registered in
  WP 7.0-RC1) via custom DependencyExtractionWebpackPlugin config
- Copy DataViews CSS into build/ and enqueue from PHP with fallback
  to wp-dataviews style handle when available
- Filter unregistered script dependencies in Asset_Loader to prevent
  wp_enqueue_script notices
- Expand AI_Request_Cost_Calculator from 3 providers / 25 models to
  8 providers / 50+ models (adds DeepSeek, Mistral, Cohere, Groq,
  Grok, Ollama; adds o-series reasoning models, Claude 4.5/4.6 IDs)
- Add Azure → OpenAI provider alias for cost lookup
- Return $0.00 for local providers (Ollama) instead of null
- Fix provider tooltip: click-to-toggle instead of hover so links
  are reachable
- Compact operation cell to max 2 rows (code + kind inline)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Fix PHPCS array arrow alignment in cost calculator (phpcbf auto-fix)
- Fix PHPStan: cast filemtime() to string for wp_enqueue_style version
- Fix ESLint a11y: add role, tabIndex, onKeyDown to provider cell
- Fix ESLint prettier: auto-formatted multiline expressions
- Fix ESLint import: suppress no-extraneous-dependencies for webpack
  build-tool requires (dependency-extraction-webpack-plugin,
  copy-webpack-plugin)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 16, 2026

Codecov Report

❌ Patch coverage is 72.96443% with 342 lines in your changes missing coverage. Please review.
✅ Project coverage is 73.16%. Comparing base (841b3da) to head (4f9b9f5).

Files with missing lines Patch % Lines
includes/Logging/Logging_Http_Transporter.php 19.17% 118 Missing ⚠️
includes/Logging/AI_Request_Log_Repository.php 81.62% 70 Missing ⚠️
includes/Logging/AI_Request_Log_Page.php 18.30% 58 Missing ⚠️
includes/Logging/Log_Data_Extractor.php 85.65% 36 Missing ⚠️
includes/Logging/AI_Request_Log_Manager.php 69.01% 22 Missing ⚠️
includes/Logging/AI_Request_Log_Schema.php 76.34% 22 Missing ⚠️
includes/Logging/Logging_Integration.php 36.00% 16 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##             develop     #437      +/-   ##
=============================================
+ Coverage      72.24%   73.16%   +0.92%     
- Complexity      1162     1526     +364     
=============================================
  Files             68       77       +9     
  Lines           5609     6867    +1258     
=============================================
+ Hits            4052     5024     +972     
- Misses          1557     1843     +286     
Flag Coverage Δ
unit 73.16% <72.96%> (+0.92%) ⬆️

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

☔ View full report in Codecov by Sentry.
📢 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.

@TylerB24890 TylerB24890 changed the title Feature/ai request logging [Feature]: AI Request Logging Apr 16, 2026
Copy link
Copy Markdown
Collaborator

@dkotter dkotter left a comment

Choose a reason for hiding this comment

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

Overall this looks decent to me but have a couple bigger questions:

  1. I'd love to not have to manually support a bunch of providers. If possible, I'd suggest we remove any hardcoded provider references and rely on what information we can get from the AI Client
  2. I'd suggest we drop cost tracking / estimation from this as it gets overly complicated. My opinion is that should be implemented at the provider level

Comment thread build/admin/ai-request-logs.asset.php Outdated
Comment thread includes/Admin/Provider_Metadata_Registry.php Outdated
Comment thread includes/Admin/Provider_Metadata_Registry.php Outdated
Comment thread includes/Admin/Provider_Metadata_Registry.php Outdated
Comment thread includes/Admin/Provider_Metadata_Registry.php Outdated
Comment thread stubs/wordpress-ai-client.php Outdated
Comment thread phpstan.neon.dist Outdated
Comment thread webpack.config.js
Comment on lines +143 to +144
// Copy DataViews CSS into build/ so the plugin can enqueue it
// on WP versions where wp-dataviews is not a registered style.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If all the code changes in this file are only to support older WP versions, I think we can just remove all of this as we only support 7.0+ anyway

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It seems WP 7.0 doesn't actually register the wp-dataviews stylesheet handle. I tried removing this but the UI ended up breaking.

Is there a different handle for the wp-dataviews stylesheet I should be targeting? Left this in place for now until we figure this out.

Comment thread includes/Admin/Provider_Metadata_Registry.php Outdated
Comment thread src/admin/components/provider-icons.tsx Outdated
@TylerB24890 TylerB24890 requested a review from dkotter May 7, 2026 15:18
@dkotter dkotter marked this pull request as ready for review May 11, 2026 14:05
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 11, 2026

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

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: Jameswlepage <isotropic@git.wordpress.org>
Co-authored-by: TylerB24890 <tyb@git.wordpress.org>
Co-authored-by: jeffpaul <jeffpaul@git.wordpress.org>
Co-authored-by: dkotter <dkotter@git.wordpress.org>
Co-authored-by: justlevine <justlevine@git.wordpress.org>
Co-authored-by: gohelkunjan <gohelkunjan@git.wordpress.org>

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

@dkotter
Copy link
Copy Markdown
Collaborator

dkotter commented May 11, 2026

This is looking good to me. I think final thing needed here is some discussion around the introduction of a new database table in this PR. I don't have any concerns with that but others may.

If we decide we're fine with that, I think final step is to ensure we are okay with the structure of the table and the data we are storing as it's harder to change that once this goes live. cc / @jeffpaul

$table_name = $this->get_table_name();
$charset_collate = $wpdb->get_charset_collate();

$sql = "CREATE TABLE {$table_name} (
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I asked for feedback on this new table and schema on our last AI call and one question was raised around the logging of cached tokens. We log input and output tokens here but not cached tokens.

Looking into it, I think this will require changes both in the PHP AI Client and within each AI Provider plugin, as right now the AI Client has a TokenUsage class that doesn't consider cached tokens.

And looking at the OpenAI Provider plugin as an example, it removes all usage information after sending it to the TokenUsage class so you can't even manually pull that information yourself.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hmmm, that's a good flag. I didn't really even think about cached tokens as a separate metric here. Are cached tokens returned as a value in that usage key? I think supplying them as another parameter to the GenerativeAiResult object would be the best path forward, as that already stores tokenUsage.

Given the GenerativeAiResult object doesn't currently do anything for cached tokens is that something we could add support for in like a phase 2 of this experiment or should we aim to get that squared away now?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I didn't dive too far into it but I think upstream changes are needed to fully support this, both in the PHP AI Client and in each individual provider plugin. As such, I wouldn't put that as a blocker here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah I reviewed a bit and that was my understanding as well 👍

@jeffpaul
Copy link
Copy Markdown
Member

Using WordPress 7.0-RC4, OAI provider Version 1.0.3, and the latest ZIP here I'm not seeing any data appearing in the AI Request Logs screen.

@jeffpaul
Copy link
Copy Markdown
Member

Tested locally again, still nothing coming through. Tested on Playground with WordPress 7.0-RC4 running, OAI Version 1.0.3, and things worked as expected, so maybe just something off with my local.

jeffpaul
jeffpaul previously approved these changes May 19, 2026
Comment thread assets/svg/wpai-icon.svg Outdated
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.

May end up replacing this with the updated WPORG icon asset from #546

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ah, yep, good catch. I can update that now that #546 has been merged

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Actually it doesn't look like that icon was even being used, it was already defaulting to the same icon we use everywhere else. I've gone ahead and removed that extra icon file

@dkotter dkotter merged commit 01f6393 into WordPress:develop May 19, 2026
21 of 35 checks passed
simison pushed a commit to simison/ai that referenced this pull request May 25, 2026
Added - Request Logging experiment that provides observability for all AI operations

Co-authored-by: Jameswlepage <isotropic@git.wordpress.org>
Co-authored-by: TylerB24890 <tyb@git.wordpress.org>
Co-authored-by: jeffpaul <jeffpaul@git.wordpress.org>
Co-authored-by: dkotter <dkotter@git.wordpress.org>
Co-authored-by: justlevine <justlevine@git.wordpress.org>
Co-authored-by: gohelkunjan <gohelkunjan@git.wordpress.org>
Comment thread phpstan.neon.dist
- tests/
- vendor/
- includes/Logging/Logging_Integration.php
- includes/Logging/Logging_Http_Transporter.php
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I know I'm late here, but I'm not seeing it in the previous comments.
@TylerB24890 @dkotter Why aren't we analyzing these files?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I believe the issue here is these files reference code coming from the AI Client and that code isn't stubbed yet, so PHPStan throws errors. And the errors thrown were internal to PHPStan so they weren't errors we could suppress individually.

I couldn't find the discussion in a quick search but the idea was we could either stub the AI Client ourselves or ignore those files until the upstream WordPress Core stubs are updated. We decided on the latter but open to revisiting that decision

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Definitely no need to revisit here, we should be able to update to the latest WordPress stubs as soon as wp-cli-stubs cuts a new release. If it's not out this week though, I'll open a PR to add in a # Reason why we're ignoring these

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.

Add usage safeguards to AI Client (limits, visibility, and cost awareness)

5 participants