The problem

Shopping chatbots often make up products, prices or stock, and letting an AI move money without asking is risky. Merchants also cannot see why the AI recommended something.

The solution

An agent that can only answer from verified tool results, must ask the shopper before cart or checkout actions, and records every step so the merchant can review the AI's decisions on a dashboard.

How it works

Architecture & flow

  1. 1

    Shopper chat UI, merchant dashboard and external AI agents call a FastAPI backend (Vercel serverless)

  2. 2

    /api/chat runs the agent loop: Gemini decides which tool to call, the backend runs it and returns the result

  3. 3

    Money-moving tools pause the loop until the shopper confirms through /api/chat/confirm

  4. 4

    Conversation state, products, orders and the ai_actions audit log live in PostgreSQL (Supabase)

  5. 5

    Checkout creates a Razorpay order with server-side prices, verifies the HMAC signature and locks stock rows with FOR UPDATE

Problems I hit

Challenges and how I solved them

Challenge 1

The LLM could invent products or prices.

How I fixed it

Gave it no built-in catalog knowledge, so every product fact has to come from a tool call against the database.

Challenge 2

Free-tier Gemini keys hit 429 quota errors during demos.

How I fixed it

Built a thread-safe key pool with round-robin rotation and cooldowns. History is stored in Postgres, so switching keys loses no context.

Challenge 3

Two shoppers buying the last item at the same time could oversell stock.

How I fixed it

Stock is decremented inside a transaction with SELECT ... FOR UPDATE row locking.

Challenge 4

Raw API errors and tracebacks were leaking into the chat.

How I fixed it

Added an error sanitizer that shows friendly messages to users and keeps full tracebacks in server logs.

Results

  • Working agent that searches, compares, adds to cart and checks out through Razorpay test mode
  • Merchant dashboard showing an AI Decision Trace for every recommendation
  • Deployed live on Vercel

What I learned

  • Designing tool schemas and a bounded agent loop
  • Human-in-the-loop confirmation for risky actions
  • Payment verification, webhooks and transactional stock updates
  • Handling LLM rate limits in production

Project Overview

ShopMind AI is an agentic e-commerce assistant built for the Razorpay hackathon (AI growth and agentic commerce track). Instead of a prompt-only chatbot, it runs a bounded agent loop: the model has no product knowledge of its own and must call tools to search, compare and check stock in a live PostgreSQL catalog.

Key features

  • Agent loop with tool calling (search_products, compare_products, get_stock), capped at 10 tool calls per turn.
  • Human-in-the-loop: add_to_cart and initiate_checkout pause and wait for the shopper to confirm.
  • Full AI audit trail: every tool input, output and approval is stored in PostgreSQL.
  • Razorpay checkout with server-side pricing, row-level stock locking and signed webhooks.
  • Merchant dashboard with an AI Decision Trace, revenue and upsell metrics.
  • Campaign agent that looks at abandoned carts and drafts recovery nudges.
  • Machine-readable /api/agent-catalog endpoint so other AI agents can discover and buy products.
  • Gemini key pool with round-robin rotation and automatic failover on 429 quota errors.

What's next

  • Add evaluation tests for agent tool choices
  • Support more product categories and personalised recommendations
Related

More ai workflow projects