Repository navigation
AI Request Logs: Warn when monthly token usage nears a limit - #1419
HILAYTRIVEDI wants to merge 7 commits into
Conversation
Add a monthly token limit and an alert threshold to the AI Request Logging experiment. When usage for the current calendar month reaches the threshold or the limit, administrators see an admin notice and get one email per state per month, and the logs screen shows a usage meter. Requests are never blocked. - Usage_Limit_Monitor sums tokens since the start of the month in the site's time zone, caches the total and tops it up as requests are logged. - Filters: wpai_monthly_token_limit, wpai_usage_limit_send_email and wpai_usage_limit_email_recipients. See WordPress#348.
|
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. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #1419 +/- ##
=============================================
+ Coverage 85.62% 85.80% +0.17%
- Complexity 5155 5218 +63
=============================================
Files 194 195 +1
Lines 18637 18881 +244
=============================================
+ Hits 15958 16200 +242
- Misses 2679 2681 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Perhaps a filter so that while requests are not blocked at the limit by default, the filter could adjust things so that token usage is halted at the limit (again, not the default setting)? |
Adds the wpai_usage_limit_block_requests filter (default false). When it returns true and this month's usage has reached the limit, the monitor hooks core's wp_ai_client_prevent_prompt so prompts are prevented until usage resets. The admin notice, email and usage card say requests are paused when blocking is on.
Good idea, added in the latest commit. There's now a |
|
Hi @jeffpaul , The testes are failing because of the memory , can you please check ? |
What?
See #348
Adds a monthly token limit with an alert threshold to the AI Request Logging experiment. When usage for the current month reaches the threshold or the limit, administrators get an admin notice and an email, and Tools → AI Request Logs shows a usage meter. Requests are not blocked by default; a filter can opt in to blocking them once the limit is reached.
Why?
In #348 we agreed on a phased approach: v1 covers usage visibility (#1418), and a monthly limit warning was the planned fast follow. Site owners currently have no way to find out that AI usage is climbing until a provider bill arrives. This adds that early warning without changing how requests behave by default. Sites that want a hard stop can opt in to blocking with a filter. Per-plugin limits are left for a follow-up.
How?
0, off) and Alert threshold (%) (default80, range 1–100). Both are registered with sanitizers and preloaded with the feature toggles.Usage_Limit_Monitor(new):tokens_totalfrom the start of the current calendar month in the site's time zone, using the newAI_Request_Log_Repository::get_tokens_since().wpai_request_logged, so logging a request doesn't trigger a full recount.ok,warning(at or above the threshold) orexceeded(at or above the limit).0.UsageLimitCardshows tokens used, the threshold marker, the state and the reset date, with a link to change the limit.wpai_usage_limit_block_requestsfilter (defaultfalse) is checked only once usage reaches the limit. When it returnstrue, the monitor hooks core'swp_ai_client_prevent_prompt, sogenerate_*calls return aprompt_preventederror andis_supported*checks returnfalseuntil next month. The notice, email and usage card then say requests are paused until the reset date. It's a soft cap: usage is cached for up to five minutes and the request that crosses the limit still completes.wpai_monthly_token_limit,wpai_usage_limit_block_requests,wpai_usage_limit_send_emailandwpai_usage_limit_email_recipients.docs/experiments/ai-request-logging.md.This branch is based on
developand doesn't depend on #1418.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
10000) and leave Alert threshold at80.wp evalwith a few\WordPress\AI\Logging\Logging_Integration::get_log_manager()->log( [...] )calls that includetokens_input.wpai_usage_limit_alertsinwp_options).add_filter( 'wpai_usage_limit_block_requests', '__return_true' );(e.g. in an mu-plugin). The notice and card now say AI requests are paused until the reset date, and AI features fail withprompt_prevented. Remove the filter and requests work again.0. The notice and the card disappear.Automated tests:
npm run test:php -- --filter 'Usage_Limit_Monitor|AI_Request_Log|AI_Request_Logging'Screenshots or screencast
Changelog Entry