Skip to main content
KadiCache
Browse 37 Docs Guides & Search

Built-In BYOK AI Agent: Setup, Multi-Provider Vault, 3-Viewport Lab & Custom CSS

Step-by-step illustrated guide to connecting OpenAI, Claude, Gemini, or OpenRouter keys inside WP Admin -> KadiCache -> AI Agent, inspecting live tool traces, verifying 3-viewport CSS drafts, and rolling back changes in 1 click.

apps/wordpress-plugin-pro/src/Ai/AiAgentController.phpapps/wordpress-plugin-pro/src/Ai/AiKeyVault.phpapps/wordpress-plugin-pro/src/Ai/AiProviderClient.phpapps/wordpress-plugin-pro/src/Ai/PreviewResponder.phpapps/wordpress-plugin-pro/src/CustomCss/CustomCssStore.php
WP Admin -> KadiCache -> AI Agent -> Chat Workspace
MODULE 36 · 2X RETINA
Clean connected chat workspace with in-chat provider/model switching across configured BYOK keys, 6 guided diagnostic workflows, and Vision screenshot paste.1000×1024 WebP
Quick Summary · 30-Second Operational SpecSource-Verified Guide
WP Admin Path
WP Admin -> KadiCache -> AI Agent (Chat · Custom CSS · API & Models)
Expected Outcome
Direct BYOK · Millisecond Tool Traces · 390/820/1440px Draft Verification
Key Marker / Engine
AiAgentController.php | AiKeyVault.php | AiProviderClient.php | CustomCssStore.php
  • Prerequisites: Active KadiCache Cloud Pro pairing (WP Admin -> KadiCache -> Cloud Connection) + your own API key from OpenAI, Anthropic Claude, Google Gemini, or OpenRouter.
  • Zero Proxy & Encrypted Vault: Keys are encrypted at rest with SecretBox (libsodium / AES-256-GCM) in AiKeyVault.php; all LLM traffic travels directly from WordPress to strictly allowlisted provider endpoints via wp_safe_remote_post.
  • Safe Tool Execution: Read-only diagnostics (get_status, diagnose_url, get_settings, get_rum_analytics, inspect_page_dom, run_pagespeed_audit, simulate_settings) run automatically with collapsible millisecond traces, while all writes pause for admin approval and create a 1-click rollback checkpoint.
  • 3-Viewport Custom CSS Workflow: Stage a non-live CSS draft (stage_draft), verify Mobile (390px), Tablet (820px), and Desktop (1440px) via signed URLs (PreviewResponder.php), approve publication (publish), or roll back instantly from Chat or the Custom CSS sub-tab.
KadiCache Pro AI Agent execution loop: Direct BYOK LLM call -> Read-Only Tool Trace or 3-Viewport Signed Draft Lab -> Explicit Admin Approval -> Atomic McpCheckpoints Rollback.Click to zoom

Prerequisites & Entitlement Check#

Before using AI Agent inside WordPress, confirm two requirements:

  1. Active KadiCache Cloud Pro Connection: Open WP Admin -> KadiCache -> Cloud Connection and ensure your site displays Active Link · Cloud Pro. AiKeyVault::entitlement() checks pairing plus a cached daily subscription verification (ENTITLEMENT_TTL = 86400s). Sites running KadiCache Free Core see a read-only preview tab explaining the workflow until Cloud Pro is connected.
  2. At Least One BYOK Provider API Key: Obtain an API key directly from OpenAI, Anthropic, Google AI Studio (Gemini), or OpenRouter. Because AI Agent is strictly BYOK, KadiCache never bills per token and never sees your prompts or keys.

Step 1: Configure Your Multi-Provider BYOK Key Vault (API & Models)#

Open WP Admin -> KadiCache -> AI Agent and click the API & Models sub-tab:

  1. Select a Provider Card: Choose OpenAI, Anthropic Claude, Google Gemini, or OpenRouter. You can configure multiple providers — saving a second key never overwrites keys already stored for other providers (encrypted_keys_by_provider).
  2. Paste Your API Key & Confirm Disclosure: Paste your key into the write-only API Key input (only the last 4 characters are kept as a visual hint such as ••••Ndqw) and check the direct-to-provider data disclosure checkbox.
  3. Click Test Key & Sync Models or Save & Open Chat: AiKeyVault::save() validates unmistakable cross-provider prefixes (sk-or- for OpenRouter, sk-ant- for Anthropic, AIza / AQ. for Gemini), encrypts the key via SecretBox, fetches the provider's live /models list (AiProviderClient::fetchLiveModels()), and marks the provider card ● Connected.
FIG 01Configuring encrypted BYOK keys per provider, live model sync, and Screenshot Vision Mode inside WP Admin -> KadiCache -> AI AgentZoom 2x

Step 2: Switch Active Providers & Run Guided Diagnostics in Chat#

Once at least one provider key is saved, the API setup notice disappears cleanly and the top bar displays a green ● Connected badge. Right below the header, the Active Agent strip lets you switch between your configured providers and their synced models without leaving Chat (POST /wp-json/kadicache-pro/v1/ai-settings with action: "switch_model").

FIG 02Connected status indicator, in-chat Active Agent provider and model dropdowns, 6 diagnostic quick-start cards, and target URL barZoom 2x

You can start in three ways:

  • Click One of the 6 Starter Cards: Launch targeted workflows for Fix Delay JS / Broken UI, Fix Layout Shift (CLS), Speed Up Mobile LCP, Why Is This Page MISS?, Fix Broken Images / CSS, or Full Site Speed Audit.
  • Set a Target URL & Describe the Symptom: Enter any same-site path (for example /shop/ or /checkout/) in the Inspecting bar and ask a specific question.
  • Paste a Visual Screenshot (Ctrl+V) or Click Screenshot: Attach a PNG, JPEG, or WebP screenshot (up to 1.2 MB after client compression). AiProviderClient::validateImage() verifies magic bytes strictly in request memory — screenshots are never saved to disk.

Step 3: Inspect Live Tool Execution Traces & Export Reports#

During a run, AI Agent executes one LLM step or one tool call per HTTP request (POST /wp-json/kadicache-pro/v1/ai-chat/step) so shared-host PHP timeouts never interrupt long analyses. You can cancel an in-flight turn immediately with the Stop button (AbortController).

When the assistant replies:

  1. Expand the Collapsible Tool Trace (▼ Used X tools · Y ms): Verify the exact read-only tools executed (such as get_status, diagnose_url, get_settings, get_rum_analytics, or inspect_page_dom) along with each tool's millisecond runtime.
  2. Click Copy Report: Copy the formatted markdown summary directly to your clipboard for client updates, QA tickets, or team documentation.
FIG 03Expanded 3-tool execution trace showing get_status, diagnose_url, and get_settings timings alongside formatted diagnostics and Copy ReportZoom 2x

Read-Only Auto-Executed Tools vs Approval-Gated Actions#

Execution ModeTools (AiAgentController.php)Safety Guarantee
Auto-Executed Read-Onlyget_status, diagnose_url, get_settings, get_preload_status, inspect_database, get_rum_analytics, get_approval_status, run_pagespeed_audit, simulate_settings, inspect_page_dom, plus read sub-actions (manage_cloudflare.get_state, manage_object_cache.get_state/probe, manage_script_manager.inspect_url/list_rules, manage_pro_cloud.status, manage_custom_css.list/stage_draft)Zero changes to live site options, cache, or frontend HTML
Non-Live 3-Viewport Labrun_viewport_lab (PreviewResponder::signedUrl())Renders Mobile (390px), Tablet (820px), and Desktop (1440px) against an isolated signed draft preview — never live visitors
Approval-Gated Writesupdate_settings, optimize_site, purge_cache, manage_script_manager writes, manage_cloudflare writes, manage_object_cache writes, manage_custom_css (publish / toggle / delete)Pauses for explicit admin click on a server-hashed approval card and creates an automatic McpCheckpoints rollback checkpoint (cp_...)

Step 4: Verify 3-Viewport Custom CSS Fixes & Use 1-Click Rollback#

When you ask AI Agent to fix a visual overflow, overlapping button, or layout shift with CSS:

  1. Draft Staging (stage_draft): AI Agent stages a sanitized, path-scoped CSS draft (for example scoped to / or /product/*, using @media queries for responsive breakpoints) without affecting live traffic.
  2. 3-Viewport Lab Verification (run_viewport_lab): The built-in viewport runner inspects Mobile (390px), Tablet (820px), and Desktop (1440px) and returns before/after overflow, overlap, CLS, and console telemetry to the agent.
  3. Approve & Publish (publish): Review the proposed CSS diff and click Approve to publish. Published rules inject cleanly after Cloud Remove Unused CSS (RUCSS), serve directly from Early Cache, and appear under the Custom CSS sub-tab.
  4. 1-Click Rollback: Click Rollback on the checkpoint card in Chat — or toggle/delete any rule anytime inside the Custom CSS sub-tab (CustomCssStore.php), which automatically purges affected cached pages.
FIG 04Managing allowlist-sanitized Custom CSS rules, Add Rule editor, Early Cache integration, and checkpoint rollbacksZoom 2x

Troubleshooting & Error Reference#

Frequently Asked Operational Questions

Why does the Active Agent dropdown in Chat only show some of the 4 providers?
By design, the in-chat Active Agent selector only lists providers that have a valid SecretBox-encrypted API key saved in your vault. To add another provider to the quick switcher, open the API & Models sub-tab, select that provider card, save its API key, and return to Chat.
What happens if I remove a saved API key for one provider while another provider is configured?
Removing a key deletes only that provider's entry in encrypted_keys_by_provider. AiKeyVault automatically falls back to your remaining configured provider without touching its stored key or model selection.
Does KadiCache AI Agent store my pasted screenshots on my WordPress server?
No. Screenshots are validated in memory via magic bytes (PNG, JPEG, or WebP up to 1.2 MB), forwarded directly in the current HTTPS turn to your selected AI provider, and immediately discarded — never written to wp-content/uploads or the database.
How do I reset the conversation or clear token history?
Click + New Chat in the top-right corner of the AI Agent header. This clears your current user_meta conversation thread cleanly while leaving your saved BYOK provider keys and published Custom CSS rules untouched.
Are Custom CSS rules kept if I purge my page cache?
Yes. Published rules live in WordPress via CustomCssStore and are automatically re-injected after Cloud RUCSS whenever pages rebuild into Early Cache. Purging cache refreshes the served HTML without deleting your rules.
Gergis — Lead Systems & Cache Architect
Principal WordPress & Cloud Edge Architect
E-E-A-T Source Verified

Creator of KadiCache’s Early PHP Drop-In, 7-Driver Object Cache Engine, Cloud Remove Unused CSS, and Zero-CLS Lazy Render pipeline.

apps/wordpress-plugin-pro/src/Ai/AiAgentController.phpapps/wordpress-plugin-pro/src/Ai/AiKeyVault.phpapps/wordpress-plugin-pro/src/Ai/AiProviderClient.phpapps/wordpress-plugin-pro/src/Ai/PreviewResponder.phpapps/wordpress-plugin-pro/src/CustomCss/CustomCssStore.php
Was this engineering guide helpful?