> ## Documentation Index
> Fetch the complete documentation index at: https://www.algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# AI Mode custom hooks

> Custom hooks that change the AI Mode button, chat, product cards, filters, and search box suggestions.

This is a **beta feature** according to [Algolia's Terms of Service ("Beta Services")](https://www.algolia.com/policies/terms/).

> [!NOTE]
>
> Autocomplete, InstantSearch, and Recommend in the Algolia app blocks now support version 5 of the Algolia API client.
> Most stores only need to switch the [search client version setting](https://www.algolia.com/doc/integration/shopify/getting-started/configuration.md#search-client-version) to **Version 5**.
> If you use custom storefront JavaScript, follow the [version 5 upgrade guide](https://www.algolia.com/doc/libraries/sdk/upgrade/javascript.md).
> Support for version 4 ends on August 14, 2026.

These hooks change [AI Mode](https://www.algolia.com/doc/integration/shopify/building-search-ui/ai-mode.md).
Register them on the `algolia.hooks.initialize` event, like the other [custom hooks](https://www.algolia.com/doc/integration/shopify/building-search-ui/frontend-custom-events).

## `beforeAutocompleteAiModeTemplate`

Template for the content of the **AI Mode** button in the Autocomplete menu.
The default content is a star symbol followed by the `aiModeTitle` label.

### Parameters

**defaultTemplate**

Default template.

**templateOptions** `object`

Template options.

**html** `function`

Tagged template function to build the template.

### Returns

**template**

Template to render inside the button.

### Examples

> [accordion] Change the button content
>
> ```js JavaScript icon=code theme={"system"}
> document.addEventListener("algolia.hooks.initialize", () => {
> 	algoliaShopify.hooks.registerHook(
> 		"beforeAutocompleteAiModeTemplate",
> 		(_defaultTemplate, { html }) => {
> 			return html`<span>Ask our assistant</span>`;
> 		},
> 	);
> });
> ```

## `beforeAiModeOptions`

Changes the options of the InstantSearch [`chat` widget](https://www.algolia.com/doc/api-reference/widgets/chat/js) before it renders.
Use it to change text, templates, and other widget options.

The app sets `container`, `agentId`, `id`, and `transport` after the hook runs and overrides any changes to them.

Hooks run after translations,
so text you set in a hook overrides the [translated labels](https://www.algolia.com/doc/integration/shopify/building-search-ui/ai-mode.md#translate-labels).

### Parameters

**options** `object`

Chat widget options, such as `templates.header.titleText`, `templates.header.clearLabelText`, `templates.prompt.textareaPlaceholderText`, `templates.prompt.disclaimerText`, `templates.loaderText`, `templates.item`, and `templates.messages.error`.
Other options you can set include `context`, `feedback`, `suggestions`, and `getSearchPageURL`.

**context** `object`

Extra information the app sends to the agent with every question, such as `language` and the product or collection properties from [Page awareness](https://www.algolia.com/doc/integration/shopify/building-search-ui/ai-mode.md#page-awareness).
If the store doesn't have a language and the app doesn't send any product or collection information,
`context` is `undefined`.
Check for `undefined` before reading it.

### Returns

**options** `object`

Return the modified options.
If the hook returns nothing,
AI Mode keeps the defaults and logs `beforeAiModeOptions must return the options. Using the defaults.` to the console.
If the hook throws an error, AI Mode also keeps the defaults.

### Examples

> [accordion] Change the chat title
>
> ```js JavaScript icon=code theme={"system"}
> document.addEventListener("algolia.hooks.initialize", () => {
> 	algoliaShopify.hooks.registerHook("beforeAiModeOptions", (options) => ({
> 		...options,
> 		templates: {
> 			...options.templates,
> 			header: {
> 				...options.templates.header,
> 				titleText: "Shop assistant",
> 			},
> 		},
> 	}));
> });
> ```

> [accordion] Send an extra field to the agent
>
> Add properties to `options.context` to send more information to the agent with every question.
> This example sends a single-line text product metafield on product pages.
> Add it to your theme's `theme.liquid` file.
>
> ```html HTML icon=code-xml expandable theme={"system"}
> <script>
>   document.addEventListener("algolia.hooks.initialize", function () {
>     window.algoliaShopify.hooks.registerHook("beforeAiModeOptions", function (options) {
>       {% if template.name == 'product' and product.metafields.custom.material != blank %}
>       return {
>         ...options,
>         context: {
>           ...options.context,
>           product_material: {{ product.metafields.custom.material.value | json }},
>         },
>       };
>       {% endif %}
>       return options;
>     });
>   });
> </script>
> ```
>
> Follow these rules:
>
> * Copy the existing context properties with `...options.context` before adding your own.
>   This preserves `language` and the app's product and collection properties.
> * Always return an `options` object.
> * Use a flat object with string values.
>   Each value must contain at least one non-whitespace character and be at most 1,024 UTF-8 bytes.
>
>   Use at most 32 properties.
>   Property names must contain 1 to 64 ASCII letters, numbers, periods, underscores, or hyphens.
>
>   Keep the total context within the 4,096-byte limit, including the properties the app sends.
>   On product pages, the app already sends up to about 2.6 KB.
>
>   The app doesn't automatically shorten or validate the properties you add.
>   If the context violates these rules,
>   Agent Studio rejects each question with an HTTP 422 error,
>   and the chat displays an error.
> * If you rename or remove the app's properties, update the agent's instructions to match.
>
> The agent doesn't know what a new field means until you tell it.
> Add a line about the field to the agent's instructions.
> For the `product_material` example:
>
> ```text Agent instructions icon=file-text theme={"system"}
> When the turn context includes product_material, use it when you answer questions about materials.
> ```
>
> For more on turn context, see [Per-turn context](https://www.algolia.com/doc/guides/algolia-ai/agent-studio/how-to/turn-context.md).

## `beforeAiModeProductTemplate`

Template for each product card in the chat carousel.

Include `${cart}` in your custom card template where you want the Add to cart button to appear.

Click tracking also works for custom cards.
If the answer is still loading, clicking a link in a custom card stops it.

### Parameters

**defaultTemplate**

Default template.

**templateOptions** `object`

Template options.

**html** `function`

Tagged template function to build the template.

**item** `object`

Product record, with attributes such as `title`, `handle`, `price`, and `image`.

**cart** `object | null`

The **Add to cart** control for the card.
Render it with `${cart}` where you want the button.
If **Add to cart in chat** is off, `cart` is `null`.

**distinct** `boolean`

Whether the store shows one result per product.

**itemLink** `string`

Product URL.

### Returns

**template**

Template to render.

### Examples

> [accordion] Customize product cards and keep the Add to cart button
>
> ```js JavaScript icon=code theme={"system"}
> document.addEventListener("algolia.hooks.initialize", () => {
> 	algoliaShopify.hooks.registerHook(
> 		"beforeAiModeProductTemplate",
> 		(_defaultTemplate, { html, item, cart }, _distinct, itemLink) => {
> 			return html`
> 				<div class="custom-card">
> 					<a href=${itemLink}>Custom ${item.title}</a>
> 					${cart}
> 				</div>
> 			`;
> 		},
> 	);
> });
> ```

## `beforeAiModeFiltersString`

Changes the stock filter (A filter is a condition that limits which records Algolia returns. Filters often use one or more facet-value pairs, such as brand:Apple AND color:red. You can also filter by numeric values, dates, tags, booleans, or geographic constraints.) that AI Mode sends with each question.
The `beforeAutocompleteFiltersString` hook doesn't apply to AI Mode.

AI Mode adds the returned string to the agent's own filter with `AND`.
The hook can't remove the agent's filter.
For examples of what AI Mode sends, see [Filter products](https://www.algolia.com/doc/integration/shopify/building-search-ui/ai-mode.md#filter-products).

### Parameters

**stockFilter** `string`

Filter that applies your store's out-of-stock setting.
Can be an empty string.

### Returns

**filters** `string`

Modified stock filter.
An empty string removes the stock filter, and the agent's filter still applies.
Use supported filter syntax, such as `A AND (B OR C)`.
Don't group `AND` conditions within an `OR` condition.
For the complete rules, see [Filters and boolean operators](https://www.algolia.com/doc/guides/managing-results/refine-results/filtering/in-depth/combining-boolean-operators.md).

### Examples

> [accordion] Add a price filter to the stock filter
>
> ```js JavaScript icon=code theme={"system"}
> document.addEventListener("algolia.hooks.initialize", () => {
> 	algoliaShopify.hooks.registerHook(
> 		"beforeAiModeFiltersString",
> 		(stockFilter) => {
> 			return [stockFilter, "price < 100"].filter(Boolean).join(" AND ");
> 		},
> 	);
> });
> ```

> [accordion] Remove the stock filter
>
> ```js JavaScript icon=code theme={"system"}
> document.addEventListener("algolia.hooks.initialize", () => {
> 	algoliaShopify.hooks.registerHook("beforeAiModeFiltersString", () => {
> 		return "";
> 	});
> });
> ```

## `beforeAutocompletePromptSuggestionsTemplate`

Template for each [search box suggestion](https://www.algolia.com/doc/integration/shopify/building-search-ui/ai-mode.md#set-up-search-box-suggestions) under the **AI Mode** header in the Autocomplete menu.
It works like [`beforeAutocompleteSuggestionsTemplate`](https://www.algolia.com/doc/integration/shopify/building-search-ui/autocomplete-hooks#beforeautocompletesuggestionstemplate).
Selecting a suggestion opens AI Mode and asks that question, so the item doesn't link anywhere.

### Parameters

**defaultTemplate**

Default template.

**templateOptions** `object`

Template options including `html`, `item`, and `components`.
The `item` has a `prompt` property containing the suggested question.

### Returns

**template**

Template to render.

### Examples

> [accordion] Customize search box suggestions
>
> ```js JavaScript icon=code theme={"system"}
> document.addEventListener("algolia.hooks.initialize", () => {
> 	algoliaShopify.hooks.registerHook(
> 		"beforeAutocompletePromptSuggestionsTemplate",
> 		(_defaultTemplate, { html, item, components }) => {
> 			return html`
> 				<div class="aa-AiModePromptSuggestion">
> 					<span class="aa-AiModePromptSuggestionText">
> 						${components.Highlight({ hit: item, attribute: "prompt" })}
> 					</span>
> 				</div>
> 			`;
> 		},
> 	);
> });
> ```

The default template uses these classes:

| Class | Element |
| - | - |
| `.aa-AiModePromptSuggestion` | The whole suggestion |
| `.aa-AiModePromptSuggestionIcon` | The icon |
| `.aa-AiModePromptSuggestionText` | The question |

### Other hooks for search box suggestions

These [Autocomplete hooks](https://www.algolia.com/doc/integration/shopify/building-search-ui/autocomplete-hooks.md) also apply to search box suggestions:

* Use [`beforeAutocompletePluginOptions`](https://www.algolia.com/doc/integration/shopify/building-search-ui/autocomplete-hooks#beforeautocompletepluginoptions)
  with `pluginName: "promptSuggestions"` to change the search client, index name (`indexName`), or search parameters, such as `hitsPerPage`.
* Use [`beforeAutocompleteConfigurationOptions`](https://www.algolia.com/doc/integration/shopify/building-search-ui/autocomplete-hooks#beforeautocompleteconfigurationoptions)
  to configure search box suggestions.
  For these suggestions, `context.sourceId` is `"promptSuggestionsPlugin"`.
* Use [`beforeAutocompletePluginsArray`](https://www.algolia.com/doc/integration/shopify/building-search-ui/autocomplete-hooks#beforeautocompletepluginsarray)
  to remove the suggestions plugin.
* When you customize the Autocomplete menu with [`beforeAutocompleteMainTemplate`](https://www.algolia.com/doc/integration/shopify/building-search-ui/autocomplete-hooks#beforeautocompletemaintemplate),
  include `elements.promptSuggestionsPlugin` in the returned template to display the search box suggestions.
