Developer reference
Hooks, filters, database schema, and CSV format for AI Spend Monitor.
Updated
The Monitor is 17 files of GPL PHP with a deliberately small public surface. Everything below is the supported integration contract.
Filters
aismon_cost_rates
The model price table. Keys are lowercase model-id prefixes (longest match wins); values are [input, output] in USD per 1M tokens.
add_filter( 'aismon_cost_rates', function ( $rates ) {
// Your contracted rate for GPT-4o.
$rates['gpt-4o'] = array( 2.00, 8.00 );
// A model missing from the bundled table.
$rates['grok-3'] = array( 3.00, 15.00 );
return $rates;
} );
Unknown models record tokens but show ”—” for cost.
aismon_retention_days
Retention window for recorded calls (default 90, minimum 7). A daily cron prunes older rows.
add_filter( 'aismon_retention_days', fn() => 30 );
Actions
aismon_usage_recorded
Fires after each call is stored. Receives the stored row (array $data, column => value) and the row id. This is the integration point AI Spend Governance uses; it’s stable API.
aismon_dashboard_after_summary
Fires after the dashboard summary cards, with $totals and $period. Render extra panels here (Governance injects its budget card this way).
Database
One table: {$wpdb->prefix}aismon_usage, indexed on created_at, source_slug, and status.
| Column | Notes |
|---|---|
| created_at | UTC datetime |
| status | completed or blocked (blocked rows are written by Governance) |
| source_type / source_slug / source_name | plugin, theme, or core attribution |
| provider / model / capability | from the AI Client’s result metadata |
| prompt_tokens / completion_tokens / total_tokens | total = prompt + completion |
| est_cost_usd | NULL when the model has no rate |
Uninstalling drops the table and all options/transients.
How attribution works
The recorder listens to the AI Client’s wp_ai_client_after_generate_result lifecycle hook and walks the backtrace to the first file inside a plugin or theme directory — the same technique Query Monitor uses. Calls from WP-Cron attribute to the plugin whose callback ran; calls from WP-CLI or custom code outside plugins attribute as core.
CSV export format
One row per call, columns matching the table above, formula-injection-safe quoting, capped at 50,000 rows per export.
What there is no API for
Recording is automatic and can’t be triggered manually; there is intentionally no public write API into the usage table beyond the recorder itself (Governance’s blocked-row logging is the one internal exception).
Question about this page?
This form tags your question with the product, docs page, and category so support can triage it quickly.