Builder Case Study · AI-Assisted Development

RefundFlow

Customer-support refund automation SaaS — users manage refund requests, AI agent (LangChain/Ollama) auto-decides based on policy, human-in-loop for edge cases.

Stale (2 months) Next.js 14 Supabase FastAPI LangChain Ollama
9 Sessions
5 Key Decisions
AI Augmented Workflow
Full-Stack SaaS
AI-augmented workflow (Zapier/Retool)

The Refund Processing Bottleneck

Customer support refund processing is repetitive, policy-driven, and error-prone. Each refund request requires checking order history, applying refund policy, calculating amounts, and processing the refund. For e-commerce businesses processing hundreds of refunds daily, the overhead is significant.

The core challenge: build a full-stack SaaS where an AI agent handles the common cases autonomously, escalating edge cases to human operators. The system must integrate order lookup, policy enforcement, amount calculation, and refund execution — all while maintaining audit trails and supporting multi-tenant isolation.

Industry Parallels: AI-augmented workflow automation (Zapier/Retool)

Three-Tier AI-Augmented System

The system decomposes into three tiers: a Next.js frontend for refund request management, a Supabase backend for auth, data, and real-time updates, and a Python FastAPI agent service running LangChain with a local Ollama LLM for autonomous decision-making.

from langchain.agents import Tool, AgentExecutor
from supabase import create_client

tools = [
    Tool(name="lookup_order",
         func=lambda id: create_client(url, key)
             .table("orders").select("*").eq("id", id).execute()),
    Tool(name="refund_policy",
         func=lambda: open("policy.txt").read()),
]
agent = AgentExecutor(agent=llm_agent, tools=tools, verbose=True)
result = agent.invoke({"input": "Refund order #ORD-4921 for $59.99"})
  1. Next.js Frontend — App Router, TypeScript, Tailwind CSS. Customer-facing refund request portal and operator review dashboard.
  2. Supabase Backend — Postgres for order/refund data, Auth for multi-tenant login, Realtime for live dashboard updates and agent decision notifications.
  3. FastAPI Agent Service — LangChain with Ollama-powered LLM. Receives refund requests, looks up order data, applies policy via few-shot prompts, and either auto-approves/denies or escalates to a human operator.

System Architecture

USER NEXT.JS 14 APP App Router · TypeScript · Tailwind FRONTEND SUPABASE Postgres (orders, refunds, users) Auth · Row Level Security · Realtime BACKEND FASTAPI AGENT SERVICE LangChain · Few-shot Policy Prompting AI LAYER OLLAMA LLM Local Inference · No Cloud API DECISION AUTO-APPROVED Refund processed automatically ESCALATE Human-in-loop review frontend/ backend/ ai/

Refund Request Decision Pipeline

REFUND DECISION PIPELINE USER Refund Request Web Form 1 NEXT.JS UI + API Route Form Validation 2 SUPABASE Auth + DB Request Store 3 FASTAPI Policy Engine Business Logic 4 OLLAMA LLM Analysis Policy Check Risk Score 5 DECISION LLM + Policy APPROVE Auto-approve ESCALATE Human review DENY Auto-deny N Step Flow Decision Approve Deny

Frontend ↔ Backend ↔ AI Decision Engine

SYSTEM ARCHITECTURE Solid = sync call · Dashed = async/event FRONTEND Next.js Pages React · SSR Form Components Validation Dashboard Analytics · History Auth (Supabase) BACKEND FastAPI Server Python · Uvicorn Policy Engine Business Rules Supabase Client Postgres · Auth AI LAYER Ollama Local LLM LangChain Prompt Pipeline Risk Scoring ML Classifier DATA Postgres Supabase Cache Redis Queue Celery HTTP SSE sync sync SQL

Languages & Frameworks

Frontend
Next.js 14 TypeScript Tailwind CSS
Backend
Supabase (Postgres) Supabase Auth Supabase Realtime
AI / Agent
Python FastAPI LangChain Ollama (Local LLM)
Infrastructure
Docker Compose Supabase Self-Hosted

Key Decisions

ADR-001
Supabase Over Custom Backend
Decision: Supabase (self-hosted)

Auth, database, and real-time out of the box. Self-hostable with Docker for data sovereignty. Row Level Security provides multi-tenant isolation at the database level, critical for a SaaS processing refund data across different businesses.

ADR-002
Local Ollama Over Cloud LLM
Decision: Local Ollama inference

Refund data contains PII (customer names, payment details). Local inference keeps all data on-premise. No per-request API costs at scale — important for high-volume refund processing where each request consumes LLM tokens.

ADR-003
FastAPI Agent Over Next.js API Routes
Decision: Separate Python FastAPI service

LangChain works better in Python with the broader PyPI ecosystem. Async Python is natural for LLM calls with 5-30 second latency. Separates the agent concerns from the frontend — each can scale independently and be deployed on different infrastructure.

How It Was Built

Session 1
Full-stack SaaS with Supabase
221 messages

Building the initial app scaffold: Next.js app router setup, Supabase schema design for orders/refunds/users, auth integration, and the agent workflow skeleton with LangChain policy prompting.

Session 2
Browser Tool Usage Issues
151 messages

Debugging Supabase queries and agent responses. Resolved cross-origin issues, real-time subscription setup, and LangChain tool calling integration with the browser environment.

Session 3
Testing with Supabase Docker
111 messages

Setting up local Supabase instance via Docker Compose for development. Testing auth flows, Row Level Security policies, and end-to-end refund request processing against the local environment.

Session 4
Browser Extension Testing
76 messages

Testing the refund flow end-to-end through the browser. Validating the human-in-loop dashboard, agent decision notifications in real-time, and multi-tenant isolation for different merchant accounts.

Session 5
Running with Supabase Docker
18 messages

Final integration testing of the complete stack running against the self-hosted Supabase instance. Verifying that the AI agent correctly auto-decides standard refunds and escalates edge cases to the operator dashboard.

Delivered Assets

Nine dedicated sessions produced a complete AI-augmented refund automation SaaS. The system features auth, refund request management, an AI agent using LangChain/Ollama for autonomous decision-making, and a human-in-loop dashboard for edge case handling. The architecture aligns with the broader industry trend toward AI-augmented workflow automation, similar to Zapier and Retool.

9
Sessions Dedicated
5
Key Decisions
Full-Stack
SaaS Platform
AI
Augmented Workflow
Auth
Multi-Tenant Login
Agent
Auto-Decision Engine
Dashboard
Human-in-Loop
Policy
Few-Shot Prompting

For Enterprises

Want to Automate Your Refund Processing?

This project demonstrates how AI-augmented workflows can eliminate repetitive refund processing overhead. Let's discuss how the same patterns apply to your customer support operations.

Schedule a Strategy Consultation
or

For Founders

Building AI-Augmented SaaS?

The patterns here — local LLM inference for PII safety, LangChain policy agents, human-in-loop escalation, multi-tenant Supabase — are directly applicable to your AI workflow automation products.

Discuss Your Startup's AI Vision