$response = wp_remote_post( $primary_url, $args );
if ( 429 === wp_remote_retrieve_response_code( $response ) ) {
$response = wp_remote_post( $fallback_url, $args );
}
$data = json_decode( wp_remote_retrieve_body( $response ), true );
update_post_meta( $post_id, 'ai_summary', $data['result'] );
This is what most LLM fallback code looks like in PHP. You catch the rate limit, change the endpoint, resend the identical payload, and save whatever comes back. The job finishes, the HTTP call returns text, nothing logs an error, and the row gets written.
Emmimal P Alexander built a controlled benchmark around exactly this problem and wrote it up on Towards Data Science. Her basic router, which swaps the model ID and forwards the original payload, completed 100% of its runs. Schema integrity, the share of runs whose final output matched the expected JSON, sat at 0%. That was ten runs against a deterministic mock provider with a fixed seed. The fallback model received a request built for a different API contract and guessed its way through, and nothing downstream threw an error.
The damage an LLM fallback does on a WordPress site is worse than in her setup, because the output usually lands somewhere durable: postmeta, order meta, a custom table behind a WooCommerce feature. Broken confidence fields and result strings like “incomplete, schema mismatch during swap” sit in the database until someone notices the storefront rendering wrong. The cause ends up three steps removed from the symptom, so nobody connects them. A hard crash would have been fixed that day.
The first half of her fix is payload adaptation, and it’s the cheap half. The request gets rebuilt for the target model rather than forwarded. If the fallback has no dedicated system prompt, the instructions get merged into the first user message, and the response_format key only goes in if the target model supports it. In her benchmark the entire 0% to 100% swing on schema integrity came from one check on supports_system_prompt before deciding where the system content goes. In PHP that means each model profile gets its own args builder, and the $args array never crosses a provider boundary. It’s one conditional and it costs almost nothing.
Validate the LLM fallback output before you store it
I’d build this part first. Even with a clean payload, a cheaper model can wrap JSON in prose, drop a key, or return a string where you expect a float. The HTTP API helpers hand you the body, and nothing in them stops you from decoding garbage and writing it to postmeta. So check the shape first:
function bbioon_llm_output_is_valid( $raw, array $required ) {
$data = json_decode( $raw, true );
if ( ! is_array( $data ) ) {
return false;
}
foreach ( $required as $key ) {
if ( ! isset( $data[ $key ] ) ) {
return false;
}
}
return true;
}
Gate every write with it. On failure, log the model name and the response code, and fail the step instead of writing a partial record. Once the gate is in place, your metric changes from “did the job run” to “did the job store valid data”. That’s the only number a store owner can act on.
If you have an AI feature writing into orders or postmeta and you’re not sure what it does when the provider throttles, that’s the kind of thing I audit and fix, and I’m glad to look at it before it bites.
Where the state lives
Her recovery layer also snapshots the pipeline before the swap: message history, step index, partial output, target schema. She’s upfront that this is the weakest piece, because the snapshot lives in memory and dies with the process, and she wants to move it to SQLite. WordPress already ships that layer. Action Scheduler, the queue WooCommerce bundles, stores each action’s hook and args in its own database tables, so a scheduled action is a durable snapshot:
as_schedule_single_action(
time() + 300,
'bbioon_run_llm_step',
array( 'post_id' => $post_id, 'step' => 2, 'attempt' => $attempt + 1 )
);
The queue runner picks it up with the args intact, whether or not the process survived in between. Retry with backoff by pushing the timestamp out instead of looping, which keeps you inside PHP’s max_execution_time. Running a Planner, Executor, Validator chain inline in one request is the other common mistake, since you’ll hit the timeout before you hit the rate limit. (I’m not sure a shared host with a 30-second limit can run a three-step chain synchronously at all.) Big prompts also add their own cost on the queue; I covered that in where LLM padding overhead meets Action Scheduler.
The last piece of her design still applies in PHP. The fallback model starts cold, and in her tests it re-ran the planner’s step because nothing told it where it had landed. Her resume message fixes that by stating the step number, the partial output, and the required schema. Do the same in the prompt you build for the retry action. If you’re porting a multi-agent workflow into WordPress, plan that part before writing queue code.
Skip all of this if your feature makes one call a day and a failure just means a field stays empty until the next run. Then the validator is all you need. Before building any router, test the failure on purpose: point the primary provider at an invalid key, run the chain, and look for broken rows. If they’re there, the gate goes in first. The router can wait until the gate has caught something.