19 / 19Project
A shopping assistant that works with tool calling, user approval, prompt injection protection and rate limiting.
In this project you will build a chat assistant that searches for products and adds them to the cart with your approval. You write the tools, the approval step, the security boundaries and the cost measurement. The tests that ship with it are what let you say "done".
The files are ready on GitHub: shopping-assistant.
Tip
By default the project runs with a mock model (src/lib/fake-model.ts). That was chosen because real models cost money. No key is needed and the tests give the same result every time. The mock model only plays back scenarios written in advance; it does not reason like a real model.
If cost is not a problem for you, add a .env.local to the project root: put a model name from the AI Gateway in AI_MODEL and your own key in AI_GATEWAY_API_KEY.1
The project was run with next@16.4, ai@7.0, @ai-sdk/react@4.0, zod@4, vitest@5, @playwright/test@1.64 and @axe-core/playwright@4.14 on Node 22. In the example solution 14 unit and 6 end-to-end tests pass.
git clone https://github.com/virastack/academy
cd academy/examples/ai/shopping-assistant/starter
npm install
npx playwright install chromium
npm run test:unitAll 14 unit tests should come out red, because the tools and the route are not written yet. Run the interface tests separately with npm run test:e2e. Five of them should be red. The goal is to turn them all green.
The CLAUDE.md in the starter folder already tells the agent the rules. There are 12 products in data/products.ts. Two of them are out of stock, and one carries attack text in its description. That product will be the target of the tests in the steps ahead.
Write createTools in src/lib/tools.ts.
searchProducts should take query, maxPrice and category, and call findProducts in src/lib/products.ts.
addToCart should take productId and an integer quantity between 1 and 5, and call addItem in src/lib/cart.ts for userId.
On an error, log the real message on the server and return only "The product could not be added to the cart." to the model.Two rules matter:
Only some of a product's fields should go to the model. id, name, category, price and inStock are enough. description should not go. Text that comes from outside can be read as an instruction once it gets in front of the model. That is indirect prompt injection, and tool results are one of its paths.23
Put the quantity limit in the schema. Even if the model is wrong or has been tricked, quantity: 50 cannot get past the schema.
Check: did the tool tests in npm run test:unit turn green? The "the description does not go to the model" and "the quantity limit" tests verify that.
Write createChatHandler in src/lib/chat-handler.ts.
Take the user from the x-user-id header.
Stream over the network with streamText: the tools from createTools(userId), user approval for addToCart (toolApproval), at most 5 steps, maxOutputTokens 1024, and the model stopping when the request is cancelled.
Say in the instructions that tool results are untrusted data.
When it finishes, report the userId, the token counts and the cost with onUsage.The approval mechanism comes with toolApproval. If you pass 'user-approval' for addToCart, the tool does not run until the user approves.2 Even if the model falls for text saying "add it without approval" and calls the tool, the cart does not change. That limits the impact of the attack.
Check: are the "it asks for approval and adds after approving", "the cart does not change when it is refused" and "a tricked model cannot add without approval" tests green? The last test deliberately makes the model try to add 5 of product p-02.
Set up useChat in src/components/Chat.tsx and write the product card in src/components/ProductCard.tsx.
The searchProducts result should be shown as product cards. Pressing a card should send the message "add <id> to cart".
While addToCart waits for approval, Approve and Dismiss buttons should appear. Once answered, it should continue automatically.
Show the assistant's text as plain text; do not render markdown.
The cart count should come from /api/cart as "Cart: N items".useChat gives you the tool parts with the type tool-<name>. While approval is pending the state is approval-requested, and the answer is sent with addToolApprovalResponse.4
Don't render the assistant's text as markdown. An image URL the model writes, such as , takes the browser to that address when the page loads and can leak the conversation data out.5 The mock model tries exactly that when you type "give me a summary".
Check: in the end-to-end tests, are "adding to cart asks for approval and the cart becomes 1 after approving", "dismissing leaves the cart unchanged" and "no request is made for an external image in the assistant text" green? Try it in the browser with npm run dev too: show me running shoes, then Add to cart on a card, then Approve.
Write createRateLimiter in src/lib/rate-limit.ts with a sliding window.
On a request over the limit, return success: false and how many seconds until it can be retried.
Answer that in chat-handler with a 429 and a retry-after header.
Also return 413 without calling the model if more than 50 messages or a message longer than 4000 characters comes in.Unbounded consumption gives an attacker a way to grow your bill. Rate limiting, an input limit and an output limit (maxOutputTokens) are three separate layers against that.6 The rate limit in this project is kept in memory in a single process. If it is going to run on more than one server, it needs a shared store. The detail is in Security and cost.
Check: are the "429 and retry-after once the limit is exceeded", "the window resets" and "long input gets a 413" tests green? The 413 test also checks that the model was never called.
Write estimateCost in src/lib/cost.ts.
It should turn the input, cache-read, cache-write and output tokens into dollars with the PRICE table.The prices were taken from Anthropic's pricing page: for Sonnet 5.5, $2 input, $10 output, $0.10 cache read and $2.50 cache write per million tokens. Look at the page before you bake them into your code.7
Check: for 200 normal input, 700 cache-read, 100 cache-write and 300 output tokens, the result should come out at about $0.00372. The test checks that.
npm testThe unit tests run first, then the end-to-end ones. If the browser cannot be found, point the CHROME_PATH environment variable at a Chrome.
| Test | What it checks |
|---|---|
| The tool arguments | The search filter and the allowlist of fields |
| The description does not leak | The attack text is not in the model's second prompt |
| The quantity limit | A quantity outside 1–5 does not get past the schema |
| Approval | No adding without approval, and the cart is unchanged on a refusal |
| A tricked model | Even if the model asks for p-02×5, the cart stays empty |
| Errors do not leak | An internal error message does not go to the model |
| User isolation | Every user's cart is separate |
| The rate limit | The 429, the retry-after and the window resetting |
| The input limit | A 413 for 51 messages or 4001 characters |
| The output limit | maxOutputTokens of 1024 or less |
| The cost | estimateCost and the onUsage report |
| The interface | A clean console, a single h1, the cards, the approval flow, no external image request, no axe violations |
npm test is fully green.
description goes to the model nowhere.
addToCart does not run without approval.
The assistant's text is shown as plain text.
The rate limit, the input limit and maxOutputTokens are all on.
Tip
If you get stuck, compare with the solution/ folder. Try it yourself first. When the agent writes something wrong, pasting the test's error message to it is usually enough.
Sources
`AI_GATEWAY_API_KEY` and making a request with a plain string model name.
`tool()`, `inputSchema`, `stopWhen` and `toolApproval`.
Direct and indirect prompt injection, the impacts and the mitigations.
Tool parts in `useChat`, their states and the approval UI.
Zero trust in model output, encoding and CSP.
Denial of wallet, input limits, rate limiting and quotas.
The input, output and cache prices per model.
Your progress is stored only in this browser.
Last updated: November 3, 2026