Two Directions of AI

The PHP AI Client SDK: WordPress Calling AI vs AI Calling WordPress

⏱ 12 min

Every lesson so far has covered the “AI calling WordPress” direction, abilities, MCP, the Adapter. This lesson covers the opposite direction: your own WordPress code calling out to an AI model. That’s what the PHP AI Client SDK is for, and it has nothing to do with MCP or the Abilities API, it’s a separate, standalone concern.

What you'll learn in this lesson
The core entry point
wp_ai_client_prompt(), shipped in WordPress core 7.0.
Which providers it supports
Anthropic (Claude), OpenAI (GPT), and Google (Gemini), via official provider plugins.
Provider preference and fallback
How to request a model without hardcoding a single provider.
Where the standalone package fits
For using the same SDK outside a full WordPress environment.
Prerequisites

WordPress 7.0 or later for the core entry point shown here. If your local environment (from Lesson 2) is still on 6.9, run wp-env run cli wp core update before this lesson’s code examples.

Step 1: The core entry point, wp_ai_client_prompt()

WordPress 7.0 ships a core function for prompting an AI model, returning a builder object you chain configuration onto:

// File: my-plugin.php
$response = wp_ai_client_prompt( 'Summarize this text in one sentence: ' . $post_content )
	->generate_text();

echo esc_html( $response );
An older, now-superseded name

Earlier documentation and some community tutorials reference AI_Client::prompt() from a transitional wrapper package. That package is deprecated in favor of the core wp_ai_client_prompt() function shown here, if you see AI_Client::prompt() in another tutorial, treat it as outdated.

Step 2: Which AI providers it actually supports

WordPress 7.0 ships three official provider plugins, each connecting the AI Client to one LLM vendor:

Official PHP AI Client SDK providers, WordPress 7.0
Provider pluginVendor
AI Provider for AnthropicClaude
AI Provider for OpenAIGPT
AI Provider for GoogleGemini

At least one provider plugin must be installed and configured with a valid API key before wp_ai_client_prompt() has anything to actually call. Which provider gets used for a given request depends on what you specify, covered next.

Step 3: Requesting a preferred model, with fallback

Rather than hardcoding one vendor, you can express a preference order, and the SDK uses the first available, configured provider from your list:

// File: my-plugin.php
$response = wp_ai_client_prompt( $prompt_text )
	->using_model_preference( 'claude-sonnet-4-5', 'gemini-3-pro-preview', 'gpt-5.1' )
	->generate_text();

This is the mechanism Course 5 uses to build provider-agnostic features, if a site owner has only configured a Gemini API key, the same code still works, it just falls through the preference list to whichever provider is actually set up.

Step 4: Embeddings, the other generation type

Beyond generating text, the SDK also supports generating embeddings, numeric vector representations of text, used for semantic search and similarity comparisons rather than for producing readable output. Embeddings live on a separate class, not on wp_ai_client_prompt():

// File: my-plugin.php
use WordPress\AiClient\AiClient;

$values = AiClient::input( $post_content )->generateEmbedding()->getValues();

Course 5 covers embeddings in full depth, and Course 10, Build a RAG Knowledge Base in WordPress, builds a complete production retrieval pipeline on top of them.

Step 5: Using the SDK outside WordPress core

If you need the same AI-calling logic in a context that isn’t a full WordPress request, a build script, a separate microservice, a CLI tool, the SDK also ships as a standalone Composer package, independent of WordPress core:

composer require wordpress/php-ai-client
use WordPress\AiClient\AiClient;

$response = AiClient::prompt( 'Summarize this text in one sentence.' )
	->generateText();

The method names differ slightly by convention (generateText() in the standalone package versus generate_text() in the WordPress-integrated core function), because the standalone package follows general PHP naming conventions rather than WordPress’s.

Streaming isn't confirmed yet

Some AI SDKs advertise token-by-token streaming responses. As of this writing, streaming isn’t documented as a shipped feature of either the core function or the standalone package, don’t build a feature around it without re-checking the current SDK documentation first.

Test it: confirm a provider is reachable

With at least one provider plugin installed and an API key configured, run:

wp-env run cli wp eval 'echo wp_ai_client_prompt( "Say hello in five words or fewer." )->generate_text();'

You should see a short, real response from whichever provider you configured.

Recap

The PHP AI Client SDK is the “WordPress calling AI” half of the stack, unrelated to MCP or the Abilities API. wp_ai_client_prompt() is the current, core-shipped entry point (not the older AI_Client::prompt()), it supports Claude, GPT, and Gemini through official provider plugins, model preference lists for provider-agnostic code, and embeddings alongside plain text generation.

Resources & further reading

← Controlling Visibility: Ability Categories and the meta.mcp.public Flag Course Recap: Your Mental Model of the WordPress AI Ecosystem →