Back to Blog
wordpress-7 ai-governance developers wp-ai-client tutorial

WordPress AI Client Budget Guide: wp_ai_client_prevent_prompt Explained

Axtolab

WordPress 7.0 ships one native governance hook for its AI infrastructure: wp_ai_client_prevent_prompt. It fires before every outbound AI call, and it gives you a single binary choice — block the call, or let it through.

Developers are treating this filter as a budget solution. It is not. But understanding what it actually does is the first step to building one.

This post is a developer reference. We’ll walk through the filter, show working code, name the gaps, and outline what you’d need to build a real budget enforcement layer on top of it.


The WordPress AI Client in 60 Seconds

WordPress 7.0 introduces a centralized AI client. The key pieces:

  • wp_ai_client_prompt() is the unified function that all WP 7.0 plugins use to make outbound AI calls. Whether a plugin is generating product descriptions, summarizing posts, or running a chatbot, it goes through this function.
  • One API key, configured at Settings > Connectors. You set up your provider credentials once — OpenAI, Anthropic, Google — and every plugin on the site draws from it.
  • wp_ai_client_prevent_prompt is a WordPress filter that fires before every outbound call made through the AI client. Your callback receives the request arguments, and you return a boolean: true to block the call, false (the default) to let it through.

That’s the entire governance surface area in core. One filter. Block or allow.


Using the Filter — A Real Example

Here’s a complete, working plugin that hooks into wp_ai_client_prevent_prompt to selectively block AI calls from a specific plugin:

<?php
/**
 * Plugin Name: Block AI for Specific Plugin
 * Description: Blocks wp_ai_client calls originating from a target plugin.
 */

add_filter( 'wp_ai_client_prevent_prompt', 'axto_block_plugin_ai_calls', 10, 2 );

/**
 * Inspect outbound AI calls and block those from a specific plugin.
 *
 * @param bool  $prevent Whether to block this call. Default false.
 * @param array $args    Request arguments passed to wp_ai_client_prompt().
 * @return bool True to block, false to allow.
 */
function axto_block_plugin_ai_calls( $prevent, $args ) {
    // Already blocked by another filter — respect that decision
    if ( $prevent ) {
        return $prevent;
    }

    // The namespace or plugin slug that initiated the call
    $source = $args['source'] ?? '';

    // Block all AI calls from 'bulk-product-writer'
    if ( str_starts_with( $source, 'bulk-product-writer' ) ) {
        error_log( sprintf(
            '[ai-block] Blocked call from %s | model: %s | timestamp: %s',
            $source,
            $args['model'] ?? 'unknown',
            current_time( 'mysql' )
        ) );
        return true;
    }

    return false;
}

Walk through the key decisions:

  1. Check $prevent first. Another filter callback may have already blocked this call. Respect the chain — don’t override a block with an allow.

  2. Inspect $args['source']. The $args array contains metadata about the request, including which plugin or namespace initiated it. This is how you attribute calls.

  3. Return true to block. The AI client sees the true return value and short-circuits before making the HTTP request. No tokens consumed, no cost incurred.

  4. Log blocked calls. Without logging, you’re flying blind. error_log() is the simplest option; in production you’d write to a custom table or external service.

A note on $args: The array structure shown here is illustrative of what a well-designed implementation would expose. Check the WP 7.0 source (specifically wp-includes/ai/class-wp-ai-client.php) for exact parameter names and structure. The core team may use different keys or nest data differently.


What the Filter Cannot Do

The filter is a gate. It blocks or allows. That’s the extent of its capability.

Here’s what it does not provide:

No token accumulator. Core does not track how many tokens each plugin has consumed. There is no running total. If you want to know that bulk-product-writer has used 450,000 tokens this month, you need to build the counter yourself — intercepting responses, parsing token usage from provider headers or response bodies, and storing the totals.

No monthly budget ceiling. There is no concept of “this plugin can spend $20/month” in core. The filter doesn’t know what a budget is. It doesn’t know what month it is, and it doesn’t know what anything costs.

No cost estimation by model. Token prices vary by provider, model, and request type. An input token on GPT-4o costs a different amount than an output token on Claude Sonnet. The filter receives a request — it has no pricing table and no cost calculator.

No admin UI. The filter is pure PHP. There is no dashboard, no settings screen, no graph showing spend over time. Every piece of visibility requires custom code.

No multisite awareness. If you’re running WordPress Multisite, the filter fires per-site, but there’s no network-level aggregation or cross-site budget management in core.

For a developer maintaining one plugin on one site, these gaps are manageable. For an agency managing 50+ client sites — each running a different mix of AI-enabled plugins — this is infrastructure that needs to exist, not custom code repeated per deployment.


Building a Minimal Token Counter on Top of It

If you wanted to build budget enforcement from scratch, you’d need three components working together.

Component 1: A storage layer. A custom database table to log every AI call — plugin source, model, token count (input and output), estimated cost, and timestamp. Something like:

wp_ai_usage_log
├── id (bigint, auto-increment)
├── plugin_source (varchar)
├── model (varchar)
├── tokens_input (int)
├── tokens_output (int)
├── estimated_cost (decimal)
├── created_at (datetime)

You’d also need a budgets table to store per-plugin spending limits and the current period’s accumulated spend.

Component 2: A two-phase filter. Your wp_ai_client_prevent_prompt callback checks the budget before the call, but the actual token count comes back in the response. So you need a second hook — likely wp_ai_client_after_prompt or equivalent — to log the real usage and update the running total. The pre-call check estimates whether there’s budget remaining. The post-call hook records the actual consumption.

Component 3: An admin interface. A settings page where you set per-plugin monthly budgets, view accumulated spend, see a breakdown by plugin and by day, and get alerts when a plugin approaches its ceiling.

That’s the minimum. In practice, you’d also need:

  • WP version compatibility testing across major and minor releases as the AI client API evolves
  • Provider-specific response parsing — OpenAI, Anthropic, and Google return token counts in different response formats
  • Multisite support if any of your sites run on a network
  • Cron cleanup to archive old log entries and reset monthly counters
  • Error handling for edge cases like failed API calls that still consume tokens on the provider side

This is roughly what the Axtolab AI Governance Plugin automates. We built it because we got tired of scaffolding this infrastructure repeatedly.


When to Use the Filter Directly vs. a Governance Plugin

Use wp_ai_client_prevent_prompt directly when:

  • You’re building a single plugin and want to self-limit its own AI usage. For example, your plugin knows it shouldn’t make more than 100 calls per hour, so it tracks its own count internally and returns true from the filter when it hits the limit.
  • You have one site with simple blocking rules. “Block all AI calls from plugin X” or “only allow AI calls during business hours” — straightforward conditionals that don’t need a database or UI.
  • You’re debugging. Hooking into the filter with error_log() is the fastest way to see which plugins are making AI calls and how often.

Use a governance plugin when:

  • You’re managing multiple plugins that share the same API key, and you need per-plugin attribution and limits.
  • You need a dashboard. Stakeholders, clients, or site owners want to see spend data without reading server logs.
  • You manage multiple sites and need consistent budget enforcement across all of them without maintaining bespoke code per deployment.
  • You want cost attribution — not just token counts, but dollar amounts broken down by plugin, model, and time period.
  • You’d rather ship features than maintain governance infrastructure through WordPress core updates and provider API changes.

The Axtolab AI Governance Plugin is built on this same wp_ai_client_prevent_prompt filter. It doesn’t use private APIs or undocumented hooks. It handles the storage, the counting, the cost estimation, the admin UI, and the multisite support so that you don’t have to build and maintain that infrastructure yourself.


Further Reading


Axtolab builds governance and permissions tools for WordPress sites using AI. The AI Governance Plugin — per-plugin budget enforcement for WP 7.0 — is in development. Learn more or join the waitlist at axtolab.com.