You can’t budget AI token costs a year ahead in any honest way, so cap them in code instead. That’s the working conclusion I take from Stephanie Kirmer’s piece Drilling Into AI’s Financial Sustainability on Towards Data Science, and it holds all the way down to a single AI feature on a client’s WooCommerce store.
Her argument is that the cost of AI is opaque at the point where budgeting needs precision. Companies that tied performance reviews to AI usage got tokenmaxxing in return, and then quarterly bills nobody had modeled. A better spreadsheet can’t fix that, because the numbers the spreadsheet needs don’t exist before the calls run.
Why AI token costs defeat a yearly budget
She breaks the problem into two unknowns. The cost of a task is input tokens plus output tokens, times the number of attempts, times the price per token, and you control roughly one term of four. OpenAI’s own help center says an input count does not predict how many output tokens the model will generate, so the output side is unknowable in advance. The attempt count can’t be predicted either, because a nondeterministic model can hand back something useless and invite a retry. Output is also the expensive side: in her example an output token costs about five times an input token, and on OpenAI’s pricing page output still runs several times the input rate on current models.
The agentic case is where estimation stops being the issue. When one model constructs prompts for another, you give up control of input length too. A lot of WordPress “AI agent” plugins now work that way, a router model deciding and a worker model executing, and both of them bill you.
Where WordPress features lose control of the bill
On a store, the runaway paths are familiar even if the tokens are new. A description generator hooked to a publish action fires on every edit. A chatbot re-sends its grounding context on every turn, so input grows while the shopper repeats the same question, which is the part I covered in grounding a WooCommerce AI chatbot in real store data. And a WP-Cron job with a retry loop retries on schedule forever, because cron has no opinion about your API bill.
Reasoning tokens are the cost you can’t see. The Responses API reports them under reasoning_tokens in the usage details, bills them at output rates, and never puts them in the text you display, so a short visible answer can sit on a long internal one. The same usage object reports cached_tokens for input that hit the provider’s prompt cache, and cached input bills at a lower rate, which is one reason to keep the system prompt byte-identical between calls. For repeat identical calls, a transient on the WordPress side cuts them off before the provider sees them, which I wrote about in AI agent token costs and the cache you already have. (I haven’t tested how reliably provider-side caching applies across requests from different server IPs, so I won’t promise that saving here.)
How I cap the spend in code
Since estimation fails, the fix is enforcement: set ceilings the code can’t exceed, then log what you spend. Every LLM call in a plugin I ship goes through one wrapper. A minimal version looks like this:
<?php
function bbioon_generate_product_copy( int $product_id, string $prompt ): string {
$cache_key = 'bbioon_copy_' . md5( $prompt . '|' . $product_id );
$cached = get_transient( $cache_key );
if ( false !== $cached ) {
return $cached;
}
// Kill switch, checked before we spend anything.
$spent = (float) get_option( 'bbioon_llm_usd_this_month', 0 );
if ( $spent > 200 ) {
return '';
}
$response = bbioon_llm_request( $prompt, array(
'max_output_tokens' => 600, // visible text plus reasoning tokens
'max_attempts' => 2,
) );
if ( is_wp_error( $response ) ) {
return '';
}
bbioon_log_llm_usage( 'product_copy', $product_id, $response['usage'] );
set_transient( $cache_key, $response['text'], 6 * HOUR_IN_SECONDS );
return $response['text'];
}The wrapper returns the provider’s usage object next to the text, and bbioon_log_llm_usage writes input_tokens, output_tokens, and reasoning_tokens to a custom table keyed by feature, then adds the call’s computed cost to the bbioon_llm_usd_this_month option, which is what the kill switch checks. Keep the per-token prices in one config constant, because providers reprice when they swap models. A monthly WP-Cron event resets the option, so the ceiling resets with it. Two attempts is deliberate: one retry catches the transient failure, and a third usually reproduces the same wrong answer at full price.
What still slips past the caps
max_output_tokens has a visible failure mode: the capped answer still comes back as text. The Responses API returns an incomplete status with a reason such as reaching max_output_tokens, and if you treat that as a success, the client gets product copy that stops mid-clause. Treat incomplete as a failure, log the reason, and tighten the prompt instead of raising the cap.
Pin the model version in that same config. Providers deprecate models and replacements aren’t always the same price, so an unpinned model string can move your bill with no code change. And if the API key belongs to the store owner rather than to you, surface the monthly spend in a wp-admin column so they can see what the feature costs.
If you’re adding an AI feature to a WooCommerce site and want the caps, the usage logging, and the kill switch in place before the first bill rather than after it, that’s the kind of work I take on.
The field I watch closest is the incomplete status on each response. When it says the answer stopped at the cap, the next move is a shorter prompt or a cheaper model.