2/26/2025
DocRouter.AI
Smart Doc Router
AI-powered document processing
that connects directly to your ERP
DocRouter.AI
617.216.8509
Consulting Background
Engineering Background
PhD in Mathematics, MIT
Andrei Radulescu-Banu
DocRouter.AI
Smart Document Router
DocRouter.AI
Define Schema & Prompt
DocRouter.AI
Upload File -> Get Extraction
DocRouter.AI
Other ERP
Fax queue
Manual Workflows
DocRouter
Email queue
System of Record (e.g. ERP)
AI Workflows
Human Review
AI
Workflows
A lightweight DocRouter installation
DocRouter.AI
DocRouter Architecture
Next.js
Client
Next.js
Server
MongoDB
FastAPI
(python)
Python
workers
Python
workers
Python
workers
LiteLLM
OCR
OpenAI
Anthropic
Gemini…
DocRouter.AI
Tech Stack
👉 React, TailwindCSS, Next,js, NextAuth
👉 FastAPI, Pydantic
👉 AWS, MongoDB
👉 LiteLLM
👉 OpenAI, Anthropic, Gemini, Groq/DeepSeek
Text editor: Cursor
DocRouter.AI
React in Smart Doc Router
Component-Based Architecture
State Management & Hooks
Real-Time UI Updates
PDFLeftSidebar
PDFViewer
Layout
DocRouter.AI
PDFLeftSidebar
PDFViewer
Layout
XML-like tags are called JSX
Can be referenced elsewhere as
<Layout> … </Layout>
Contents is {children} prop
DocRouter.AI
JSX in React
Component Tags
HTML Tags
JSX gets transformed into regular JavaScript function calls during the build process. For example:
Use className instead of class b/c class is reserved in JavaScript
Tailwind utility classes
npm run build
DocRouter.AI
Tailwind vs CSS
DocRouter.AI
Essential Tailwind in the Doc Router
That's it! These cover 80% of the layout patterns in the codebase. Everything else builds on these basics.
Key Points:
DocRouter.AI
Design UI with Cursor
Just prompt Cursor explaining the problem, and ask for 3 options for solutions
Pick specific files that Cursor can focus on
I want ability to edit field description
DocRouter.AI
1st option
Description field not editable… More prompting needed to solve this.
DocRouter.AI
2nd option
Description field did not expand… More prompting needed to solve this.
DocRouter.AI
3rd option
Modal button is functional!
DocRouter.AI
Tech Stack - Next.js
👉 React, TailwindCSS, Next.js, NextAuth
👉 FastAPI, Pydantic
👉 AWS, MongoDB
👉 LiteLLM
👉 OpenAI, Anthropic, Gemini, Groq/DeepSeek
Text editor: Cursor
DocRouter.AI
DocRouter Architecture
Next.js
Client
Next.js
Server
MongoDB
FastAPI
(python)
Python
workers
Python
workers
Python
workers
LiteLLM
OCR
OpenAI
Anthropic
Gemini…
DocRouter.AI
Next.js Benefits Over Plain React
1. Server-Side Rendering (SSR) & Static Site Generation (SSG)
2. File-based Routing
3. API Routes
DocRouter.AI
DocRouter Project Structure
Next.js frontend
FastAPI backend
Why FastAPI as back end?
DocRouter.AI
Server vs. Client Component Example
Upload page (server)
DocumentUpload component (client)
DocumentUpload component (client)
Upload page (server)
DocRouter.AI
When to use Server vs. Client Components?
DocRouter.AI
Tech Stack - NextAuth
👉 React, TailwindCSS, Next.js, NextAuth
👉 FastAPI, Pydantic
👉 AWS, MongoDB
👉 LiteLLM
👉 OpenAI, Anthropic, Gemini, Groq/DeepSeek
Text editor: Cursor
DocRouter.AI
NextAuth.js
DocRouter.AI
Flow of Events: NextAuth.js Login
User logs in
Next.js server checks Mongo for creds, creates JWT and session. NextAuth callbacks are called.
Next.js
Client
Next.js
Server
MongoDB
FastAPI
(python)
Python
workers
Python
workers
Python
workers
LiteLLM
OCR
OpenAI
Anthropic
Gemini…
1
2
1
2
DocRouter.AI
Tech Stack - FastAPI
👉 React, TailwindCSS, Next.js, NextAuth
👉 FastAPI, Pydantic
👉 AWS, MongoDB
👉 LiteLLM
👉 OpenAI, Anthropic, Gemini, Groq/DeepSeek
Text editor: Cursor
DocRouter.AI
FastAPI
Next.js
Client
Next.js
Server
MongoDB
FastAPI
(python)
Python
workers
Python
workers
Python
workers
LiteLLM
OCR
OpenAI
Anthropic
Gemini…
Any UI function is available
as REST Api in FastAPI
DocRouter.AI
FastAPI
FastAPI is a Python web framework typically used for RestAPIs.
It auto-generates OpenAPI (Swagger) documentation.
Basic Structure
Starts web server for FastAPI
@app
decorator
async keyword
DocRouter.AI
async, await, coroutines
Async routines are called coroutines.
DocRouter.AI
await Is Syntactic Sugar For yield
The event loop:
This is why async code can handle many operations efficiently without using multiple threads - it's all running in a single thread, just intelligently switching between tasks whenever they're waiting for I/O.
Async/await ultimately relies on non-blocking system calls, primarily select(), poll(), or epoll() (on Linux) for network operations.
DocRouter.AI
async - How To Start The Event Loop
In FastAPI:
In workers:
Starts async event loop
Calls coroutines in parallel,
Returns when all have completed
DocRouter.AI
Workers
Next.js
Client
Next.js
Server
MongoDB
FastAPI
(python)
Python
workers
Python
workers
Python
workers
LiteLLM
OCR
OpenAI
Anthropic
Gemini…
worker_ocr
worker_llm
DocRouter.AI
FastAPI - The @app Decorator
Here's what it looks like without the decorator syntax:
DocRouter.AI
Tech Stack - Pydantic
👉 React, TailwindCSS, Next.js, NextAuth
👉 FastAPI, Pydantic
👉 AWS, MongoDB
👉 LiteLLM
👉 OpenAI, Anthropic, Gemini, Groq/DeepSeek
Text editor: Cursor
DocRouter.AI
FastAPI - @app handlers and Pydantic Schema
Pydantic schema for DocumentsUpload body
organization_id parsed automatically and passed in as arg
DocRouter.AI
Pydantic Schema Is Also Used For LLM Structured Output
DocRouter.AI
Tech Stack - AWS, MongoDB
👉 React, TailwindCSS, Next.js, NextAuth
👉 FastAPI, Pydantic
👉 AWS, MongoDB
👉 LiteLLM
👉 OpenAI, Anthropic, Gemini, Groq/DeepSeek
Text editor: Cursor
DocRouter.AI
Tech Stack - AWS
AWS
In a nutshell:
DocRouter.AI
Tech Stack - MongoDB
MongoDB
Compass UI Front-End
DocRouter.AI
Tech Stack - MongoDB
MongoDB
Example send_msg()
Example rcv_msg()
(Supports horizontal scaling of readers)
DocRouter.AI
Diagram Flow: Schema and Prompt config
User adds schema in the UI
Next.JS client calls FastAPI
FastAPI saves schema in Mongo
Same steps for adding prompts, except prompts have associated schema, LLM and tags.
Next.js
Client
Next.js
Server
MongoDB
FastAPI
(python)
Python
workers
Python
workers
Python
workers
LiteLLM
OCR
OpenAI
Anthropic
Gemini…
1
2
3
1
2
3
DocRouter.AI
Diagram Flow: Doc upload, LLM run
Tagged document is uploaded
Next.JS client calls FastAPI
Message posted to workers to run OCR
OCR is run, output saved in Mongo
Worker retrieves prompt, schema, OCR output and runs LLM
LLM output saved to Mongo then displayed in UI
Next.js
Client
Next.js
Server
MongoDB
FastAPI
(python)
Python
workers
Python
workers
Python
workers
LiteLLM
OCR
OpenAI
Anthropic
Gemini…
1
2
5
1
2
3
3
4
4
5
DocRouter.AI
Tech Stack - LiteLLM
👉 React, TailwindCSS, Next.js, NextAuth
👉 FastAPI, Pydantic
👉 AWS, MongoDB
👉 LiteLLM
👉 OpenAI, Anthropic, Gemini, Groq/DeepSeek
Text editor: Cursor
DocRouter.AI
LiteLLM
Using sync litellm API
Using async litellm API
DocRouter workers use async API, to handle docs in parallel
DocRouter.AI
LLM Model Choice (Feb ‘24)
LiteLLM supports over 100 models
DocRouter.AI
Example Cursor Prompts
I discovered one JavaScript API call was not in api.ts… Had to be fixed manually.
Prompt Cursor to make all FastAPI paths start with “/v0/…”
DocRouter.AI
Thank you!
DocRouter.AI
References
DocRouter.AI