Shopper AI assistant
The storefront ships an optional chat assistant for shoppers. It answers product questions, tracks orders and shipments, explains returns and support tickets, and helps with the cart. It can also prepare changes (add to cart, apply a coupon, cancel an order, start a return, open a ticket), but nothing changes until the shopper presses Confirm.
It runs on Claude (Claude Haiku 5.5 by default) through the Anthropic API. The agent loop lives in the engine
(@magenxcommerce/engine/assistant); the theme’s app/api/assistant/** routes only call its handlers, so every shop runs
one upgradeable copy. See the Engine API: Assistant reference.
What it can do
Section titled “What it can do”| Area | Read (runs straight away) | Change (needs the shopper’s Confirm) | Who |
|---|---|---|---|
| Catalog | search products, product detail with variants and stock, current deals and free-gift offers | - | everyone |
| Cart | show the cart | add a product, change a quantity or remove a line, apply a coupon the shopper gave | everyone |
| Orders | recent orders, one order with shipments and tracking numbers | cancel an order (with one of the store’s cancellation reasons), reorder | signed-in shoppers |
| Guest orders | look up an order with order number, checkout e-mail and billing last name | - | guests |
| Returns | list returns, one return, returnable items and return options | create a return | signed-in shoppers |
| Guest returns | returnable items and return options for an order found by number, e-mail and last name | create a return (updates go to the checkout e-mail) | guests |
| Help desk | list tickets, one ticket | open a ticket (also the hand-off to a human), reply to a ticket | signed-in shoppers |
| Guest help desk | - | open a ticket with name and e-mail (replies by e-mail) | guests |
It cannot place orders, take payments, or change addresses or account details: it points the shopper to checkout or the account pages for those.
It follows the store’s Magento settings
Section titled “It follows the store’s Magento settings”What the assistant offers is read from Magento at runtime, per store view, with the same public settings the storefront uses, and refreshed every 5 minutes:
| Magento setting | Effect |
|---|---|
| Help desk enabled / guest tickets | Guests get create_guest_ticket only when guest tickets are on (the contact page shows the same ticket form under the same setting). Otherwise guests are sent to the contact page. |
| Help desk customer account | Signed-in shoppers’ ticket tools (list_my_tickets, get_ticket, create_ticket, reply_ticket). |
| Order cancellation (Sales) | cancel_order, with the store’s own cancellation reasons. Off: the assistant offers a ticket or the contact page instead. |
A feature switched off after the assistant proposed it can no longer be confirmed. If Magento can’t be asked, the assistant uses the last known settings, or offers none of these features.
-
Turn it on in
apps/theme/magenx.config.ts:export const assistant: AssistantSettings = {enabled: true,model: "claude-haiku-5-5",effort: "low",persona: { name: "Shop assistant", tone: "friendly and concise" },policies: {shipping: "Delivery usually takes 5 - 7 working days.",returns: "Items can be returned within 30 days.",},pages: ["/deals (current offers)", "/brands"],disabledTools: [],};policiesare short texts the assistant may quote.instructionsis free text for anything shop-specific the assistant should know (opening hours, a phone number, extra policies, things to say or avoid); it is cut at 2,000 characters, and the assistant’s own rules take priority over it.pageslists storefront pages the assistant may link to besides the built-in ones (contact, guest order, cart, checkout, account pages): paths without the locale prefix, each with a short note; up to 20 are used.disabledToolswithholds tools by name; an unknown name stops the server from starting rather than being ignored.limitsoverrides individual budgets (see below). Features such as guest tickets or order cancellation need no entry here: they follow Magento (above). -
Set two server-only environment variables at runtime:
Variable Purpose ANTHROPIC_API_KEYClaude API key. ASSISTANT_SEAL_KEY32 or more random characters. Encrypts the conversation the browser holds; rotating it ends open chats. Without either one,
/api/assistantreturns 404 and the widget says the assistant is unavailable.ASSISTANT_DISABLED=1turns it off without a rebuild. -
Rate-limit
/api/assistantand/api/assistant/confirmin nginx (limit_req). -
Running more than one storefront instance? Set
REDIS_URL(orASSISTANT_REDIS_URLto use a different store) to a Redis or Valkey the instances share. The guard that stops a confirmed action from running twice, and the built-in rate limits, then hold across all instances. Without it they are per process, which is only correct for one instance. Keys live undersf:assistant:, so an ACL user limited to~sf:*is enough. If the configured store is unreachable, confirming is refused (503) rather than risking a second run; the rate limits fall back to per process. -
With Turnstile enforcement on (
TURNSTILE_ENABLE=1), a guest solves one challenge (actionassistant) before their first message. Signed-in shoppers are not challenged; their token is checked with Magento instead.
Security model
Section titled “Security model”- The assistant can only do what the shopper could do by clicking. Tools run the storefront’s own GraphQL
operations with the shopper’s customer token, read from the same
httpOnlycookie the GraphQL proxy uses. Magento scopes every result to that customer, so another customer’s order simply isn’t found. No admin or integration token exists anywhere in the assistant. - Identity never comes from the model. No tool has a customer, cart or store field; they come from the request. A guest’s cart id comes from the storefront’s own cart state.
- Changes are two-step. When the model calls a change tool, the server only checks it against live data (does the cart line exist, is the reason one of the store’s, are these items returnable) and writes a plain-language summary. The exact, validated change is stored in the conversation. Only the confirm route executes it, and only when the conversation belongs to the same shopper, the proposal is still pending and under 5 minutes old, it has not already run on this server process, and the tool is still enabled. At most two proposals per message.
- The conversation is sealed. It is kept in the shopper’s browser as one AES-256-GCM blob, bound to the shopper (a new sign-in starts a new conversation). The browser can neither read nor alter it, so forged history or a changed proposal is rejected.
- Text from other people is data. Product descriptions, reviews and ticket messages are reduced to plain text and cut short before the model reads them, and the system prompt says never to follow instructions inside them. The widget renders answers as text and links only to pages on the shop’s own site.
- Guest order lookups are limited to 3 per conversation and 10 per hour per client (guest returns count against the same limits), and a wrong guess reads as “not found” without saying which field was wrong.
- Guest tickets and guest returns make the store send e-mail to an address the visitor typed. Besides the human check and the Confirm click, they are limited to 5 per hour per client, and a guest ticket to one per conversation.
Budgets
Section titled “Budgets”| Limit | Default | What happens |
|---|---|---|
maxSteps | 8 | Model calls per shopper message. |
maxToolCalls | 12 | Tool calls per shopper message. |
turnTimeoutMs | 45000 | Wall clock per shopper message. |
maxConversationTokens | 400000 | Tokens one conversation may spend. |
maxMessageChars | 2000 | Length of one shopper message. |
messagesPerMinute | 10 | Per client, per process. |
maxHistoryMessages | 10 | Shopper messages in the history; above this the conversation is summarised (see below). |
A message that hits a limit gets a short explanation, and the conversation keeps its history from before that message.
Long conversations are summarised, not trimmed. Claude keeps its reasoning valid only while the earlier
conversation is unchanged, so the assistant never cuts old messages off. When a conversation grows past
maxHistoryMessages, one extra model call writes a short summary (order numbers, products, open requests, the shopper’s
language) and the conversation continues from that summary. A deploy that changes the system prompt, a policy text or the
tool list doesn’t break open chats either: the API drops the reasoning that no longer matches and carries on.
Requests are built for prompt caching: the tool list and system prompt are byte-identical on every call, and each step of a message reads the earlier steps from cache. Tool results carry only the fields an answer needs. A typical message costs two or three model calls, most of their input served from cache.
Debugging
Section titled “Debugging”- Conversation reference. The widget shows the first 8 characters of the conversation id. Tickets the assistant opens carry it too.
- Traces. Every model call, tool call, compaction and confirmed action is an OpenTelemetry span
(
assistant.model,assistant.tool.<name>,assistant.compaction,assistant.action.<name>,assistant.turn) taggedassistant.conversation_id, with token usage, cache reads, stop reasons, error codes and dropped reasoning blocks. Spans never contain message text or tool results, and personal fields in tool inputs are masked. See Observability. - When the model call fails. The shopper sees “The assistant is unavailable right now” (or “busy” when the API is
rate-limited or overloaded). The real reason is on the
assistant.modelspan (error.status,error.type,error.message,error.request_id) and in one server log line:[assistant] model call failed: <status> <type> <message> (conversation <id>). A 401 or 403 adds a hint to checkANTHROPIC_API_KEY. - Debug drawer (development only). With
NODE_ENV=developmentthe route also streams debug entries and the widget shows a drawer under the chat: each model step with tokens and cache reads, each tool call with its masked input and result size, compactions and confirmed actions. Production builds contain neither.
The repository has a live eval suite for the assistant: about 30 scenarios run against the real model with a fake in-memory store, each three times, with deterministic checks (which tools ran, what was proposed, what Magento received, what the answer says). Safety scenarios (another customer’s order, injected instructions in a product description, revealing the system prompt, pressure to break the rules) must pass every time; the others on at least two of three runs.
ANTHROPIC_API_KEY=… npm run eval:assistantEVAL_ONLY=<id,id> runs a subset and EVAL_REPEATS changes the repeat count. A full run costs a few cents to a few tens
of cents. Run it after changing the prompt, the tools or the model.
Customising the widget
Section titled “Customising the widget”Shoppers reach the assistant two ways: the floating Ask AI button, and an Ask AI button inside the search field that hands over what they typed and sends it straight away (after a guest’s human check, when one is needed).
The widget is components/assistant/assistant-launcher.tsx (the floating button, loaded with every page),
assistant-panel.tsx (the chat, loaded on first use) and ask-ai-button.tsx (the search field button). Shadow any of
them through the theme’s src/custom/ override layer. Its texts are in the Assistant namespace of
src/i18n/messages/<locale>.json.
Any client component can open the assistant, optionally with a first message:
import { openAssistant } from "@/components/assistant/assistant-events";
openAssistant(); // just open itopenAssistant("Do you ship to Spain?"); // open it and send this