Skip to content

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.

Magento GraphQLClaudeStorefront /api/assistantChat widgetMagento GraphQLClaudeStorefront /api/assistantChat widgetA change is only proposed: the widget shows a Confirm cardmessage + sealed conversationsystem prompt, tools, historytool call (e.g. get_order)same query the account pages run, with the shopper's own tokentool result (plain text, size-capped)streamed answer (Server-Sent Events)POST /api/assistant/confirm (shopper clicked Confirm)the stored, validated changeoutcome + updated conversation
AreaRead (runs straight away)Change (needs the shopper’s Confirm)Who
Catalogsearch products, product detail with variants and stock, current deals and free-gift offers-everyone
Cartshow the cartadd a product, change a quantity or remove a line, apply a coupon the shopper gaveeveryone
Ordersrecent orders, one order with shipments and tracking numberscancel an order (with one of the store’s cancellation reasons), reordersigned-in shoppers
Guest orderslook up an order with order number, checkout e-mail and billing last name-guests
Returnslist returns, one return, returnable items and return optionscreate a returnsigned-in shoppers
Guest returnsreturnable items and return options for an order found by number, e-mail and last namecreate a return (updates go to the checkout e-mail)guests
Help desklist tickets, one ticketopen a ticket (also the hand-off to a human), reply to a ticketsigned-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.

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 settingEffect
Help desk enabled / guest ticketsGuests 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 accountSigned-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.

  1. 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: [],
    };

    policies are short texts the assistant may quote. instructions is 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. pages lists 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. disabledTools withholds tools by name; an unknown name stops the server from starting rather than being ignored. limits overrides individual budgets (see below). Features such as guest tickets or order cancellation need no entry here: they follow Magento (above).

  2. Set two server-only environment variables at runtime:

    VariablePurpose
    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/assistant returns 404 and the widget says the assistant is unavailable. ASSISTANT_DISABLED=1 turns it off without a rebuild.

  3. Rate-limit /api/assistant and /api/assistant/confirm in nginx (limit_req).

  4. Running more than one storefront instance? Set REDIS_URL (or ASSISTANT_REDIS_URL to 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 under sf: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.

  5. With Turnstile enforcement on (TURNSTILE_ENABLE=1), a guest solves one challenge (action assistant) before their first message. Signed-in shoppers are not challenged; their token is checked with Magento instead.

  • 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 httpOnly cookie 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.
LimitDefaultWhat happens
maxSteps8Model calls per shopper message.
maxToolCalls12Tool calls per shopper message.
turnTimeoutMs45000Wall clock per shopper message.
maxConversationTokens400000Tokens one conversation may spend.
maxMessageChars2000Length of one shopper message.
messagesPerMinute10Per client, per process.
maxHistoryMessages10Shopper 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.

  • 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) tagged assistant.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.model span (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 check ANTHROPIC_API_KEY.
  • Debug drawer (development only). With NODE_ENV=development the 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.

Terminal window
ANTHROPIC_API_KEY=… npm run eval:assistant

EVAL_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.

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 it
openAssistant("Do you ship to Spain?"); // open it and send this