AIForum.

How to Queue and Batch AI Requests in WordPress to Avoid Timeouts

On this page

THE SHORT ANSWER

AI calls are slow, variable, and expensive, and WordPress is built for routing and rendering rather than long-running I/O. The fix is to stop calling providers inside a page request: write each request to a job table, process it through Action Scheduler or an external worker, and accept the result through a signed webhook or a polled status endpoint.
Quick facts

Queueing AI requests in WordPress means never calling a model provider inside the request that renders a page. Instead, the request is written to a job table, handed to a background processor or an external worker, and the result is written back later through a signed webhook or a polled status endpoint. That one change removes the timeout errors that appear when a chat completion, embedding batch, or image generation runs longer than the PHP process, the web server, or the browser is willing to wait.

This guide is for developers and technically curious site builders who already have a working AI integration in WordPress and are now hitting 504 responses, truncated output, or duplicate jobs. It covers the architecture, the database schema, batching rules, and the tuning knobs that make the pattern reliable.

Why synchronous AI calls fail in WordPress

WordPress is built for routing and rendering, not for long-running I/O. A widely referenced production pattern describes AI calls as ‘slow, variable, and expensive’ and argues that they need retries, quotas, and tracing — none of which a page request provides. The same write-up states the design goal plainly: push the job to an external worker and accept the result through a signed webhook.

When you call a provider synchronously, at least three independent clocks are running at once:

  • PHP’s execution limit. max_execution_time ends the script, often producing a fatal error rather than a clean response.
  • The web server and proxy chain. FastCGI, nginx, and load balancers each apply their own idle timeout, and the shortest one wins.
  • The browser. A fetch that has not resolved within a minute or two is usually abandoned by the user regardless of what the server does next.

There is a fourth, quieter cost: PHP-FPM worker occupancy. Every slow AI call holds a worker that could be serving a normal page. A handful of concurrent generations can exhaust the pool and make the whole site feel down, even though nothing has technically crashed.

Two ways to add a queue

Action Scheduler inside WordPress

Action Scheduler is the job queue used by WooCommerce and many other plugins. It stores actions in its own tables and processes them through a queue runner. Its defaults are deliberately conservative so that it works on unknown hosting. According to the project’s performance documentation, the runner will only process actions in a request until:

  • 90% of available memory is used,
  • processing another three actions would exceed 30 seconds of total request time, based on the average processing time for the current batch,
  • the single concurrent queue is busy.

Action Scheduler also initiates an asynchronous loopback request to keep processing, but on sites with very large queues this can result in slow processing times. The documentation is explicit that using WP-CLI to process queues is the best approach to increasing processing speed, with threshold tuning as the fallback when WP-CLI is not viable.

An external worker with signed webhooks

The alternative is to keep WordPress thin and move the AI call to a Python or Node worker. The production pattern for this is well documented: the client submits a request to a WordPress REST route, WordPress writes a pending job row and enqueues it to an external queue or posts it to a worker gateway, the worker pulls the job and calls the provider, then posts a signed webhook back to WordPress. WordPress verifies the signature, stores the result, and invalidates the relevant cache. The frontend then polls or uses SSE or WebSocket through a lightweight proxy for updates.

This is more moving parts, but it decouples the AI provider’s latency from your hosting entirely, and it gives you a place to enforce quotas and tracing outside PHP.

The async job pattern, step by step

1. Create a job table on plugin activation

A custom table gives you status, idempotency, and error text in one place. Use dbDelta() on activation with utf8mb4 and the indexes that match your query patterns.

function ai_integration_activate() {
    global $wpdb;
    $table   = $wpdb->prefix . 'ai_jobs';
    $charset = $wpdb->get_charset_collate();

    $sql = "CREATE TABLE $table (
        id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
        user_id bigint(20) unsigned NOT NULL DEFAULT 0,
        status enum('pending','running','succeeded','failed') NOT NULL DEFAULT 'pending',
        input_hash char(64) NOT NULL,
        request_json longtext NOT NULL,
        result_json longtext NULL,
        error_text text NULL,
        idempotency_key varchar(64) NOT NULL,
        webhook_ts datetime NULL,
        created_at datetime NOT NULL,
        updated_at datetime NOT NULL,
        PRIMARY KEY  (id),
        UNIQUE KEY idempotency_key (idempotency_key),
        KEY status_created (status, created_at),
        KEY input_hash (input_hash)
    ) $charset;";

    require_once ABSPATH . 'wp-admin/includes/upgrade.php';
    dbDelta( $sql );
}

The status_created index supports the worker’s query for the oldest pending job. The unique idempotency_key index is what prevents a retried HTTP request from creating a second paid AI call. The input_hash index lets you detect that the same prompt was already submitted.

2. Accept requests through a REST route

Register a route that validates the user, hashes the normalized input, generates or accepts an idempotency key, inserts the row, and returns the job ID immediately. The response should be fast and should never wait on the provider.

3. Enqueue outward

Either schedule an Action Scheduler action, or POST the job payload to the worker gateway. Keep the payload small: the job ID plus the parameters. The worker can read the full request from the database if it has access, or receive it inline if it does not.

4. Process and call the provider

The worker updates the row to running, calls the model, and writes either result_json or error_text. Long provider calls belong here, not in PHP.

5. Verify the signed webhook

The callback endpoint must verify an HMAC signature over the raw request body and reject stale timestamps to limit replay. Store the verification time in webhook_ts. Never trust a webhook that only carries the job ID — anyone who guesses an ID could otherwise inject a result.

6. Store the result and invalidate cache

Write the result, flip the status to succeeded, and clear any transient or object-cache entry that the frontend reads. This is also the point to record usage for quota accounting.

7. Deliver to the frontend

Polling a status endpoint is the simplest option and works everywhere. SSE or WebSocket through a lightweight proxy gives a better experience but adds infrastructure. Either way, the browser never holds a connection open to the AI provider.

Batching rules that keep queues healthy

Batching is not just sending many prompts at once. The useful rules are:

  • Chunk by the provider’s limit. Embedding endpoints usually accept arrays; chat endpoints usually do not. Respect the documented maximum per request.
  • One job per unit of retry. If a batch of 50 items fails because item 37 was rejected, a single job forces you to redo all 50. Smaller jobs retry cheaply.
  • Cap concurrency. A queue that fires 200 parallel requests will hit rate limits and produce a wave of failures. Process a fixed number at a time.
  • Use exponential backoff with jitter. Retrying immediately after a 429 makes the rate limit worse.
  • Keep a terminal failed state. Jobs that exhaust retries should stop and be visible, not loop forever.

Idempotency is the connective tissue. The unique key on idempotency_key means a duplicate submission returns the existing job instead of paying for the same work twice.

Tuning Action Scheduler thresholds

If you stay inside WordPress, the default 30-second processing window is the first thing to raise. The documented filter is:

function eg_increase_time_limit( $time_limit ) {
    return 120;
}
add_filter( 'action_scheduler_queue_runner_time_limit', 'eg_increase_time_limit' );

Only raise it if you know your host allows longer web requests. The documentation lists 60 seconds on WP Engine and 120 seconds on Pantheon as known host time limits, which is exactly why the default is conservative. For large backlogs, WP-CLI remains the recommended processing path.

Secrets and configuration

Provider keys and worker URLs do not belong in the options table. The production pattern places them in wp-config.php as constants such as AI_WORKER_URL. Options are exported, synced between environments, and visible to anyone with database access; constants are not.

Limitations and tradeoffs

An external worker adds a deployment target, a network hop, and a public webhook endpoint that must be secured. Action Scheduler avoids all of that but shares the web request budget, and on low-traffic sites the loopback runner may not fire often enough without WP-CLI or a real cron schedule.

Local models do not escape the problem. If your provider is a model running on your own hardware, inference is still slow and variable, and a queue is still the right answer — the difference is that you control the rate limits.

Finally, none of this removes the need to tell the user something. A queued job that silently never completes is worse than a timeout that at least fails loudly. Surface job status in the admin UI.

A verification checklist

  1. Submit a request and confirm the REST response returns in well under a second with a job ID.
  2. Confirm the row exists with status pending.
  3. Confirm the worker flips it to running and then succeeded.
  4. Replay the same webhook with a valid signature and confirm the result is not applied twice.
  5. Send a webhook with a bad signature and confirm it is rejected.
  6. Submit the same idempotency key twice and confirm only one job row exists.
  7. Force a provider error and confirm the job ends in failed with readable error_text.

Where to go next

Start by moving a single slow endpoint — the one that times out most often — onto a job table and Action Scheduler, and keep the synchronous path as a fallback while you compare. If your volume grows past what the loopback runner can handle, the external worker pattern is the next step. Readers working through this in production are welcome to share their environment, queue volume, and the exact error they hit in the community so others can compare notes.

FAQ

Can I fix AI timeouts just by raising PHP's max_execution_time?

Raising the PHP limit only removes one of several clocks. The web server, proxy chain, and browser each apply their own timeouts, and a long AI call still occupies a PHP-FPM worker that could be serving normal traffic. Queueing addresses all of these at once.

Do I always need an external worker to queue AI requests?

No. Action Scheduler can handle moderate volumes entirely inside WordPress. An external worker becomes worthwhile when you need to decouple provider latency from your hosting, enforce quotas outside PHP, or process queues faster than the loopback runner allows. The Action Scheduler documentation notes that WP-CLI is the best way to increase processing speed for large queues.

How do I avoid paying twice for the same AI request?

Store an idempotency key per logical request with a unique index on the job table. A duplicate submission then returns the existing job row instead of creating a new one. An input_hash column provides a second check when the same prompt is resubmitted.

Does running a local model remove the need for a queue?

No. Local inference is still slow and variable, so a synchronous call can still exceed PHP, proxy, or browser timeouts. The advantage is that you control the rate limits and do not pay per token, not that the latency disappears.

References

  1. https://aiguyinla.com/blog/a-production-ready-pattern-for-ai-in-wordpress-async-jobs-signed-webhooks-and-external-workers
  2. https://actionscheduler.org/perf/

Related terms

Async job
A unit of work recorded in a table with a status field, processed outside the request that created it, and completed later.
Idempotency key
A unique value attached to a request so that retries or duplicate submissions resolve to the same job instead of triggering a second AI call.
Signed webhook
A callback whose payload carries an HMAC signature, allowing WordPress to verify that the result really came from the worker and not from an attacker guessing job IDs.
Action Scheduler
The background job queue used by WooCommerce and other plugins, which stores actions in its own tables and processes them under conservative memory and time thresholds.
Exponential backoff with jitter
A retry strategy that waits progressively longer between attempts and adds randomness, reducing the chance that many failed jobs retry simultaneously and worsen a rate limit.

Leave a Reply

Your email address will not be published. Required fields are marked *