kimaai_chatbot_after_response Hook #
The kimaai_chatbot_after_response action fires after a successful AI response has been received and (optionally) mutated by filters, but before the REST API returns the final JSON to the frontend.
Use it for analytics, telemetry, logging, cost tracking, and measuring performance (paired with kimaai_chatbot_before_request).
When it fires #
In the request flow:
- The plugin builds
$ai_payload - Fires
kimaai_chatbot_before_request - Calls the AI provider via
AiService::run() - Applies
kimaai_chatbot_response_payloadfilter - Fires
kimaai_chatbot_after_response - Returns the response to the client (
200)
The action is triggered here:
do_action('kimaai_chatbot_after_response', $response, $ai_payload, $payload, $chatbot_settings);
Hook signature #
/**
* Action: fired after a successful AI response (for analytics/telemetry).
*
* @param array $response AI response payload.
* @param array $ai_payload The provider payload that was sent.
* @param array $payload Normalized payload.
* @param array $chatbot_settings Chatbot settings.
*/
do_action('kimaai_chatbot_after_response', $response, $ai_payload, $payload, $chatbot_settings);
Parameters #
1) $response (array) #
The final response payload that will be returned to the frontend (status 200).
This is after the kimaai_chatbot_response_payload filter has run, so it reflects any modifications made there.
2) $ai_payload (array) #
The exact provider request payload that was sent to the AI service (prompt, model, tool settings, etc.).
3) $payload (array) #
Normalized chatbot request context (prompt/postId/sessionId/selectedTools, etc.).
4) $chatbot_settings (array) #
The chatbot settings used for this request.
Basic usage: log successful responses #
add_action('kimaai_chatbot_after_response', function ($response, $ai_payload, $payload) {
error_log(sprintf(
'[KimaAI Chatbot] Response OK: provider=%s model=%s session=%s postId=%d',
(string) ($ai_payload['aiProvider'] ?? ''),
(string) ($ai_payload['aiModel'] ?? ''),
(string) ($payload['sessionId'] ?? ''),
(int) ($payload['postId'] ?? 0),
));
}, 10, 4);
Example: measure latency (paired with kimaai_chatbot_before_request) #
Before request: store a start time.
add_action('kimaai_chatbot_before_request', function ($ai_payload, $payload) {
$session_id = (string) ($payload['sessionId'] ?? '');
if ($session_id !== '') {
set_transient('kimaai_ai_start_' . md5($session_id), microtime(true), 5 * MINUTE_IN_SECONDS);
}
}, 10, 3);
After response: compute elapsed time.
add_action('kimaai_chatbot_after_response', function ($response, $ai_payload, $payload) {
$session_id = (string) ($payload['sessionId'] ?? '');
$key = 'kimaai_ai_start_' . md5($session_id);
$start = (float) get_transient($key);
if ($start > 0) {
delete_transient($key);
$ms = (microtime(true) - $start) * 1000;
error_log(sprintf(
'[KimaAI Chatbot] AI latency: %.0fms provider=%s model=%s',
$ms,
(string) ($ai_payload['aiProvider'] ?? ''),
(string) ($ai_payload['aiModel'] ?? '')
));
}
}, 10, 4);
Example: track usage / cost (if your $response includes token info) #
If your AI service response includes token usage (structure depends on your AiService implementation), you can capture it here.
add_action('kimaai_chatbot_after_response', function ($response, $ai_payload, $payload) {
// Example keys only — adjust to match your AiService response structure.
$usage = $response['usage'] ?? null;
if (!is_array($usage)) {
return;
}
$prompt_tokens = (int) ($usage['prompt_tokens'] ?? 0);
$completion_tokens = (int) ($usage['completion_tokens'] ?? 0);
error_log(sprintf(
'[KimaAI Chatbot] Tokens: prompt=%d completion=%d session=%s',
$prompt_tokens,
$completion_tokens,
(string) ($payload['sessionId'] ?? '')
));
}, 10, 4);
Example: analytics without storing user prompts #
add_action('kimaai_chatbot_after_response', function ($response, $ai_payload, $payload) {
$prompt = (string) ($ai_payload['prompt'] ?? '');
$prompt_hash = $prompt !== '' ? hash('sha256', $prompt) : '';
$record = [
'time' => time(),
'provider' => (string) ($ai_payload['aiProvider'] ?? ''),
'model' => (string) ($ai_payload['aiModel'] ?? ''),
'postId' => (int) ($ai_payload['postId'] ?? 0),
'sessionId' => (string) ($payload['sessionId'] ?? ''),
'tools_count' => !empty($ai_payload['tools']) ? count($ai_payload['tools']) : 0,
'prompt_hash' => $prompt_hash,
];
error_log('[KimaAI Chatbot Analytics] ' . wp_json_encode($record));
}, 10, 4);
Notes and best practices #
- This action runs only on the successful path (no exception thrown).
- Don’t
echooutput—this is a REST request. - Avoid logging full prompts and raw identifiers unless needed; prefer hashing/anonymizing.
- If you need to modify the response payload, use:
kimaai_chatbot_response_payload(filter)
Related hooks #
kimaai_chatbot_before_request— great for starting timers / tracing IDskimaai_chatbot_response_payload(filter) — modify the outgoing response bodykimaai_chatbot_error— error reporting when AI execution throwskimaai_chatbot_gate/kimaai_chatbot_blocked— early blocking & diagnostics