Skip to content

Conversation

@Jameswlepage
Copy link
Contributor

@Jameswlepage Jameswlepage commented Dec 19, 2025

ai-logs

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, cost estimate)
  • React-powered admin dashboard under Settings → AI Request Logs
  • REST API endpoints for programmatic access
  • Configurable retention period and automatic cleanup

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

Supports 14 providers out of the box: OpenAI, Anthropic, Google, Fal, Cloudflare, Groq, Grok, HuggingFace, DeepSeek, Ollama, OpenRouter, Azure, Cohere, and Mistral.

Testing Instructions

  1. Enable the AI Request Logging experiment in Settings → AI → Experiments
  2. Navigate to Settings → AI Request Logs and toggle "Logging enabled"
  3. Trigger an AI-powered feature (e.g., Title Generation, Excerpt Generation)
  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

Adds a comprehensive logging system for AI requests that provides:
- Observability: Track all AI operations with request/response previews
- Cost tracking: Automatic cost estimation based on provider/model pricing
- Admin UI: Full-featured log viewer with filtering, search, and stats
- REST API: Endpoints for querying logs programmatically

Architecture:
- AI_Request_Log_Manager: Facade coordinating all logging operations
- AI_Request_Log_Schema: Database table creation and management
- AI_Request_Log_Repository: CRUD with caching and cursor pagination
- AI_Request_Cost_Calculator: Provider pricing and cost estimation
- Logging_HTTP_Client: Decorator for automatic request capture

Features:
- Configurable retention period and max rows
- Daily cleanup cron job
- FULLTEXT search with LIKE fallback
- Filter by type, provider, status, operation, date range
- Summary statistics by period
Replace the PSR-18 client discovery approach with a cleaner architecture
that uses the AiClient SDK's public setHttpTransporter() API:

- Add Logging_Http_Transporter: decorator implementing HttpTransporterInterface
- Add Logging_Integration: initializes logging by wrapping the SDK transporter
- Remove Logging_HTTP_Client and Logging_Discovery_Strategy (reflection-based)
- Update bootstrap to use new Logging_Integration class
- Update documentation with new architecture details

This approach is more production-ready as it:
- Uses the SDK's public API rather than reflection hacks
- Wraps at the correct abstraction level (transporter, not HTTP client)
- Is resilient to SDK internal changes
- Follows the decorator pattern cleanly
Extract data parsing logic from Logging_Http_Transporter into a dedicated
Log_Data_Extractor class with filter hooks for extensibility:

- ai_request_log_providers: customize provider detection patterns
- ai_request_log_context: filter context data before storage
- ai_request_log_tokens: custom token extraction for new providers
- ai_request_log_kind: customize request kind detection

Architecture improvements:
- Separation of concerns: transport vs extraction logic
- Transporter reduced from 574 to 130 lines
- Media metadata stored instead of full base64 data (prevents DB bloat)
- All extraction methods are public for testability

Supports 14 providers out of the box: OpenAI, Anthropic, Google, Fal,
Cloudflare, Groq, Grok, HuggingFace, DeepSeek, Ollama, OpenRouter,
Azure, Cohere, and Mistral.
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a comprehensive AI Request Logging experiment that provides observability for AI operations through real-time logging, a React-powered admin dashboard, REST API endpoints, and configurable retention settings. The implementation uses the decorator pattern to wrap the SDK's HTTP transporter and supports 14 AI providers out of the box.

Key changes:

  • Logging infrastructure with database schema, repository pattern, and cost estimation
  • React-based admin dashboard with DataViews for log viewing and filtering
  • REST API endpoints for programmatic access to logs and settings
  • Automatic cleanup based on retention period and row limits

Reviewed changes

Copilot reviewed 46 out of 47 changed files in this pull request and generated no comments.

Show a summary per file
File Description
webpack.config.js Adds webpack entry point for AI request logs admin bundle
src/admin/hooks/usePersistedView.ts Custom hook wrapping @wordpress/views for persisting DataViews state
src/admin/components/provider-icons.tsx Provider icon component mapping for displaying AI provider logos
src/admin/components/icons/* SVG icon components for various AI providers
src/admin/ai-request-logs/index.tsx Main React app component with state management and API integration
src/admin/ai-request-logs/components/* UI components for logs table, summary cards, settings panel, and modal
src/admin/ai-request-logs/style.scss Comprehensive styles for the admin dashboard
includes/helpers.php Helper functions for experiment checking, icon loading, and timeout configuration
includes/bootstrap.php Bootstrap integration for logging initialization
includes/Logging/* Core logging infrastructure (Manager, Schema, Repository, HTTP Transporter)
includes/Experiment_Loader.php Registers the AI Request Logging experiment
tests/Integration/Includes/Logging/* Integration tests for log manager functionality

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@sethrubenstein
Copy link

This is great. Building something like this was at the top of my to-do list for January, before I start developing more AI abilities for our staff.

Copy link
Member

Choose a reason for hiding this comment

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

Can we use the colored variant like https://github.com/WordPress/ai/blob/develop/.wordpress-org/icon.svg instead?

@jeffpaul
Copy link
Member

@jeffpaul
Copy link
Member

  • Reminder for us to add a screenshot gif and entry in the readme.txt for this feature before merging/releasing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Needs review

Development

Successfully merging this pull request may close these issues.

3 participants