---
name: kimaai_chatbot_before_request
type: action
since: 1.3.5
tier: free
feature: chatbot
area: chatbot
summary: Fires before the chatbot sends a request to the AI provider.
parameters:
  - name: ai_payload
    type: array
    description: The request to the provider, after kimaai_chatbot_ai_payload.
  - name: payload
    type: array
    description: The normalised request payload, with prompt, postId, postSection, lang, sessionId and chatbot_settings, and assistantSurface in a search answer.
  - name: chatbot_settings
    type: array
    description: The chatbot settings.
surfaces: [chatbot, search]
---

# kimaai_chatbot_before_request

## When it fires

Right after `kimaai_chatbot_ai_payload`, once the request is prepared and before it runs, in a chatbot request or a search answer. It fires for answers that stream and for those that do not, and also for a quick action whose callback answers without the provider. KimaAI's answer sources (Pro) start their list for the request here.

The action cannot change the request; use `kimaai_chatbot_ai_payload` for that. Pair it with `kimaai_chatbot_after_response` and `kimaai_chatbot_error` to time or count the requests.

## Example

```php
add_action(
  'kimaai_chatbot_before_request',
  function (array $ai_payload, array $payload): void {
    error_log(sprintf('KimaAI asks %s (%s)', $ai_payload['aiModel'], $payload['assistantSurface'] ?? 'chatbot'));
  },
  10,
  2
);
```

## Related

- `kimaai_chatbot_ai_payload`: changes the request before this action.
- `kimaai_chatbot_after_response`: fires after the answer.
- `kimaai_chatbot_error`: fires when the provider fails.
