AIForum.

How to Cache AI API Responses in WordPress to Reduce Duplicate Calls

On this page

THE SHORT ANSWER

Duplicate AI calls are usually a caching problem, not a model problem. WordPress core exposes a PSR-16 cache hook through AiClient::setCache(), and you can add a versioned transient layer around your own requests so an identical question never reaches the API twice. The key is a stable cache key that includes the prompt version, the model, and the normalized input — and never caching errors.
Quick facts

Duplicate AI calls are usually a caching problem, not a model problem. If the same question reaches your plugin twice, the model is asked twice, and you pay twice in latency and tokens. The practical fix has two layers: let the AI client reuse a PSR-16 cache for its own responses, and wrap your application’s request in a versioned cache key so an identical request never leaves your server.

This guide is for WordPress developers and site builders who already make AI calls from a plugin, theme, or the core AI Client and want predictable behaviour. It covers what WordPress ships, what you still have to build yourself, and where caching can quietly make answers wrong.

Why the same request is sent more than once

Duplicate calls rarely come from a broken model integration. They come from ordinary web behaviour: a form that is reloaded or double-clicked, a scheduled job that runs again after a timeout, a block or shortcode that renders more than once in a single page load, or a REST request that a client retries. As one practitioner summary puts it, a reloaded form, a double click, or a repeated job can send the same input several times, and if every pass calls the API immediately, latency and consumption rise without producing new value.

The consequence is not just cost. It is unpredictability: two users asking the same question at the same moment can receive two different answers, and a retried job can produce two side effects. Caching is the first line of defence because it converts an identical request into a lookup instead of a network call.

Two layers where caching can happen

Layer 1: the AI client’s own cache

The AI client can hold a PSR-16 cache instance and use it to store responses and other data. This is the broadest layer: anything routed through the client can benefit, without you writing per-feature caching code.

Layer 2: your application’s response cache

Your plugin or theme can also cache the final answer it produces, keyed on the exact inputs that determine that answer. This layer gives you control over the time-to-live (TTL), the key structure, and what is allowed into storage. In most production setups you want both: the client cache to absorb repeated internal calls, and your own key to absorb repeated user-facing requests.

What the WordPress AI Client already exposes

WordPress core ships a cache adapter for its AI client. WP_AI_Client_Cache implements the PSR-16 CacheInterface and, in the reference wording, bridges PSR-16 cache operations to WordPress object cache functions so the AI client can use WordPress caching infrastructure. Its cache group is wp_ai_client, defined since 7.0.0, and get_cache_group() applies a filter for customization, added in 7.1.0.

There is an important restriction: the reference marks this class’s access as private, meaning it is not intended for use by plugin or theme developers, only by core, and it is listed only for completeness. Do not instantiate WP_AI_Client_Cache from your own code; treat it as core plumbing that may change.

The hook you can use is AiClient::setCache(). Its documented signature is:

AiClient::setCache( ?CacheInterface $cache ): void

The documentation describes it as setting the PSR-16 cache for storing and retrieving cached data, noting that the cache can be used to store AI responses and other data to avoid redundant API calls and improve performance. Passing null disables caching. The changelog lists the method as introduced in 0.4.0.

// Pass a PSR-16 cache implementation, or null to disable caching.
AiClient::setCache( $my_psr16_cache );

// Turn caching back off.
AiClient::setCache( null );

Two caveats before you rely on this. First, the reference documents the mechanism, not a default: it does not state whether a cache is attached automatically in a given WordPress version, so verify the behaviour on your own install rather than assuming responses are already cached. Second, the parameter type is listed as a PSR-16 cache interface under the AI Client’s dependency namespace; confirm the exact namespace in the package version you have installed before writing a use statement.

Building a versioned transient cache around your own call

Even with a client-level cache, most WordPress integrations need their own key so they control TTL, versioning, and what is stored. The pattern below follows the approach described by Luca Rimediotti: normalize the input, build a key from the input plus the model plus a prompt version, read a transient, call the API only on a miss, and store the result only when the call succeeded.

Step 1: Normalize only the fields that determine the answer

Lowercase and trim the user input so that cosmetic differences do not create separate cache entries. Do not normalize away anything that genuinely changes the answer.

Step 2: Include the model and a prompt version

Different models produce different answers, and so do different instructions. The source pattern includes a version number precisely because changing a prompt while keeping the key would let WordPress return an answer produced under the old rules.

Step 3: Hash the payload into a fixed-length key

Hashing keeps the key short and avoids putting raw user text into option names.

Step 4: Read before you call

A get_transient() call that returns something other than false is a hit, and the API call is skipped entirely.

Step 5: Store only after a valid response

In the source pattern, the cache is written after a valid response, never after an error.

$question = isset( $_POST['question'] )
    ? sanitize_text_field( wp_unslash( $_POST['question'] ) )
    : '';

$cache_payload = array(
    'v'        => 2, // bump this when the prompt or output format changes
    'model'    => 'the-model-configured-in-settings',
    'question' => mb_strtolower( trim( $question ) ),
);

$cache_key = 'myplugin_ai_' . hash( 'sha256', wp_json_encode( $cache_payload ) );
$answer    = get_transient( $cache_key );

if ( false === $answer ) {
    $answer = myplugin_call_ai( $question );

    if ( ! is_wp_error( $answer ) ) {
        set_transient( $cache_key, $answer, HOUR_IN_SECONDS );
    }
}

if ( is_wp_error( $answer ) ) {
    return new WP_Error( 'ai_unavailable', __( 'Service temporarily unavailable.', 'my-plugin' ) );
}

return (string) $answer;

This snippet is illustrative and adapted from the published pattern; it assumes myplugin_call_ai() already handles authentication and timeouts. The cache sits after the call, not inside it.

What belongs in the cache key

FieldWhy it mattersExample value
Prompt or instruction versionChanging the prompt without changing the key can return an answer generated under the old rulesv => 2
Model identifierDifferent models answer differently, so they must not share a keymodel => 'configured-model'
Normalized inputPrevents whitespace and case differences from creating duplicate entriesmb_strtolower( trim( $input ) )
Locale or requested output formatThe same question in a different language is a different requestlocale => 'en_US'
User or tenant scopeOnly needed when the answer is personalized; otherwise it destroys the hit rateuser_id => 42

What should never be cached

  • Errors. The source pattern is explicit that the cache is written after a valid response and never after an error. Caching a WP_Error or an empty string turns a brief outage into a long one for the duration of the TTL.
  • Personalized or sensitive content, unless the key is scoped to the user and the storage location is appropriate for that data.
  • Streaming or partial output, which is incomplete by definition at the moment you would be storing it.
  • Requests with side effects, such as sending mail or creating posts. Caching those changes behaviour rather than saving work.

Choosing a TTL

The published example uses HOUR_IN_SECONDS, which is a reasonable default for question-and-answer features. The right value depends on how quickly the underlying facts change: short for anything time-sensitive, longer for classification or stable transformations. When you change a prompt, prefer bumping the version number in the key over waiting for entries to expire, because that invalidates old answers immediately instead of gradually.

Treat a cache hit as an optimization, not a guarantee. Transients can be evicted early by a persistent object cache or a cleanup routine, so your code must always be correct when the cache misses.

Verifying that caching actually happens

  1. Log a counter immediately before the API call, then send the same input twice. The second run should not increment the counter.
  2. Read the entry directly with get_transient( $cache_key ). A return of false means a miss; anything else means the value was found.
  3. Time both requests. A hit should skip network time, though the exact saving depends on the provider and connection.
  4. Inspect the calls with Query Monitor or your object cache backend to confirm get_transient and set_transient are firing where you expect.
  5. Change one field — the model or the version — and confirm you get a miss, which proves the key is actually sensitive to the right inputs.

Troubleshooting common failures

  • The cache never hits. Something in the key changes on every request: a timestamp, a session identifier, raw whitespace, or inconsistent case. Print the generated key on two identical requests and compare them character by character.
  • Answers are stale after a prompt edit. The prompt version is not part of the key, so old entries still match. Bump the version.
  • The site shows “temporarily unavailable” long after the outage. An error response was cached. Only store successful results.
  • The options table keeps growing. On sites without a persistent object cache, transients live in the options table, so many unique keys add rows. Hashing keeps keys short, and a realistic TTL lets entries expire.
  • Multisite behaves oddly. Transients are per site; a network-wide cache needs a different storage strategy.

Limits and tradeoffs

Caching does not help unique prompts. Hit rate depends entirely on repetition, so measure it before assuming a large saving. There is also a correctness tradeoff: a cached answer is by definition an older answer, and for anything time-sensitive that can be worse than the extra call.

Privacy deserves a decision, not a default. Cached text persists for the TTL, so consider whether user input and model output should be stored at all, and for how long. Finally, a cache is not an idempotency lock: two simultaneous misses can still trigger two calls. If that matters for your workflow, add a short-lived lock around the call.

Because the core cache adapter is documented as private API, plugin-facing behaviour may change between versions. Keep your own application-level cache independent of it so a core change cannot silently disable your protection.

Next steps

Start by measuring: add a counter before your AI call and record how often the same input repeats. Then set a TTL per feature rather than one global value, include a prompt version in every key, and decide explicitly what is never allowed into the cache. If you have a working key scheme, or you hit an edge case with transients and a persistent object cache, share your environment, the exact key you generate, and any error message with the Ai-Forum.org community so others can reproduce it.

FAQ

Does WordPress cache AI responses automatically?

The reference documents the mechanism — a PSR-16 cache can be attached via AiClient::setCache(), and core ships WP_AI_Client_Cache as an adapter — but it does not state a default. Verify the behaviour on your installed version, and remember that passing null disables caching.

Should I cache failed AI requests?

No. The published pattern stores the response only after a valid result and never after an error, so a temporary outage does not become a cached outage for the whole TTL.

Why do I still get an old answer after editing my prompt?

The prompt version is probably not part of the cache key. Include a version field and increment it whenever the instructions or output format change.

Do transients work the same on every WordPress host?

No. On sites without a persistent object cache they are stored in the options table and can be evicted before their TTL expires, so your code must behave correctly on a miss.

Can I use WP_AI_Client_Cache directly in my plugin?

The reference marks the class as private and states it is not intended for plugin or theme developers, only for core. Use AiClient::setCache() with your own PSR-16 implementation instead.

References

  1. https://developer.wordpress.org/reference/classes/wp_ai_client_cache/
  2. https://developer.wordpress.org/reference/classes/wordpress-aiclient-aiclient/setcache/
  3. https://lucarimediotti.com/cache-ai-wordpress-richieste-duplicate/

Related terms

PSR-16
A PHP standard interface for simple key-value caching (get, set, delete, TTL). WordPress's AI client cache adapter implements it.
Transient
A WordPress API for storing a value with an expiration time, read with get_transient() and written with set_transient().
Cache key
The identifier a cached value is stored under. It must change whenever any input that affects the answer changes.
TTL
Time to live — how long a cached value remains valid before it expires and the next request must call the API again.
Prompt version
A number or string included in the cache key that is incremented whenever the instructions or output format change, invalidating old entries.
Thundering herd
The situation where several requests miss the cache at the same moment and all call the API simultaneously; a cache alone does not prevent it.

Leave a Reply

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