Architecture
No framework, no magic: a FastAPI pipeline you can read end to end, deterministic guardrails, pgvector retrieval, and an n8n escalation path.
The pipeline
SVG diagramuser_messageEvery message enters the same pipeline. No trusted users, no magic words.
injection_filterDeterministic pattern screen (EN+FR): instruction overrides, prompt extraction, persona changes, fake authority. Fires → hard refusal, retrieval never runs.
scope_filterOff-topic intents (poems, code, weather) and competitor comparisons → polite decline.
retrieval (pgvector)Top-k chunks from Postgres + pgvector (OpenAI embeddings in live mode; deterministic TF-IDF in demo mode, in-memory).
grounding_gateTop similarity AND term coverage must clear the thresholds. Fires → honest “I don't have that in our docs” + escalation card.
grounded_generationAnswer composed only from retrieved chunks (extractive in demo mode; raw Anthropic/OpenAI SDK in live mode, system-prompt constrained). Citations attached.
confidence_gateBelow the confidence threshold the answer is withheld and replaced by an honest fallback + escalation card.
sentiment_router / human_handoffAnger cues → empathy prefix + grounded answer + handoff offer. Explicit human request → immediate escalation.
escalation → n8n → ticket + SlackIdempotent POST to the n8n webhook with 1s → 2s → 4s retry backoff: priority routing, Slack notification, ticket in the ops log.
The system prompt, in full
Transparency as a feature: the exact instructions the agent runs under. A guardrail that only works when hidden isn't a guardrail.
Escalation workflow (n8n)
When the agent escalates, the backend POSTs to an n8n webhook. The workflow enriches the ticket, posts to Slack, and files it in the support inbox. Import the JSON into any n8n instance.
Download workflow JSONStack & decisions
- No LangChain, no orchestration framework — the pipeline is ~400 lines of FastAPI you can read in one sitting. That is the point: I build this myself, so I can debug, extend, and audit it myself.
- Guardrails are deterministic code, not prompt wishes. Regex screens, similarity thresholds, and coverage checks cannot be sweet-talked — and each one reports exactly what it did, per request.
- Demo mode is fully deterministic: a TF-IDF lexical retriever + extractive answers, zero API keys, byte-identical results on any machine. That's what makes the public eval suite reproducible.
- Live mode swaps in OpenAI embeddings + pgvector and an Anthropic/OpenAI LLM under a strict grounding system prompt — same guardrails, same pipeline, one env var.
Support cost estimator
Compare human-only support ($6.50/ticket benchmark) against Bulwark hybrid deflection (68% automated at $0.15/1M input · $0.60/1M output tokens).
Security & compliance notes
Prompt-injection patterns and out-of-scope requests are blocked in Python before retrieval or LLM inference ever runs.
Middleware enforces 30 req/min per IP (HTTP 429 JSON) and POST /api/escalate honors Idempotency-Key headers to prevent duplicate tickets.
Every production prompt is fingerprinted with a SHA-256 hash and tracked in version history so behavioral regressions are traceable.
Demo runs on 100% fictional CloudDesk data. In production, swap in Postgres + pgvector inside your VPC and enable zero-data-retention API agreements.
Deployment instructions
Run locally in deterministic demo mode with zero API keys, or deploy the full stack (FastAPI + Next.js + Postgres/pgvector + n8n) via Docker Compose.
# 1. Local quickstart (demo mode, no API key required) cd backend && pip install -r requirements.txt uvicorn app.main:app --reload cd frontend && npm install && npm run dev # 2. Full production stack with pgvector + n8n cp .env.example .env # Set DEMO_MODE=false, OPENAI_API_KEY / ANTHROPIC_API_KEY, # DATABASE_URL, and N8N_WEBHOOK_URL docker compose --profile n8n up -d --build
Verify dependency readiness anytime at GET /api/health.