Dev.to AI 🤖 Ai 👁 0 📖 2 min read

Add an AI Cart Assistant to Shopify That Only Answers From Your Policies

Add an AI Cart Assistant to Shopify That Only Answers From Your Policies A keyword bot breaks when shoppers phrase things differently. An LLM handles "will this reach me by Friday?" fine, but it can also make things up.

Add an AI Cart Assistant to Shopify That Only Answers From Your Policies

A keyword bot breaks when shoppers phrase things differently. An LLM handles "will this reach me by Friday?" fine, but it can also make things up. The fix is to give it your policies and the live cart, and tell it to stick to them.

  1. Send the cart from the storefront

The widget reads /cart.js and posts it with the question to your server.

javascript
async function ask(question) {
const cart = await (await fetch('/cart.js')).json();
const res = await fetch('https://your-server.com/api/cart-assistant', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ question, cart }),
});
return (await res.json()).reply;
}

  1. Ground the model on the server javascript import Anthropic from '@anthropic-ai/sdk'; const client = new Anthropic(); // reads ANTHROPIC_API_KEY from env

const POLICIES =
Delivery: 3-5 business days.
COD: available under ₹5,000.
Returns: within 7 days of delivery.
Free shipping above ₹999.
;

app.post('/api/cart-assistant', async (req, res) => {
const { question, cart } = req.body;
const items = cart.items.map(i => ${i.quantity} x ${i.product_title}).join(', ');

const msg = await client.messages.create({
model: 'claude-sonnet-5-5',
max_tokens: 300,
system: You are a shop assistant on the cart page. Answer in two sentences or fewer.
Use ONLY these policies and the cart below. If the answer isn't there, say you're not sure and offer a human.
Policies: ${POLICIES}
Cart: ${items || 'empty'}; total ₹${cart.total_price / 100}.
,
messages: [{ role: 'user', content: question }],
});

res.json({ reply: msg.content[0].text });
});

The API key stays on the server. Never put it in theme code.

Tips
Keep policies in one place and update them whenever offers change.
Rate-limit the endpoint so it can't be abused.
Log questions where the bot says "not sure" and review them weekly.
Don't send customer personal details to the model unless you need to.
Wrap-up

AI cart automation here is just two inputs, your policies and the live cart, plus a strict instruction. Start with this, then add reminders and handoff as you learn what shoppers ask.

📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.