DocAgent is a production-grade, autonomous document processing agent designed to streamline accounts payable and enterprise invoice operations. It extracts structured data from multi-modal documents (PDF, Scanned Images, TXT), validates compliance against 9 deterministic business rules, performs statistical fraud & anomaly screening, detects SHA-256 duplicates, and manages human-in-the-loop approval workflows with full database persistence and an immutable audit trail.
π View Interactive Mermaid Architecture Source
graph TB
classDef clientStyle fill:#EBF5FB,stroke:#2980B9,stroke-width:2px,color:#1B4F72;
classDef gatewayStyle fill:#E8F8F5,stroke:#16A085,stroke-width:2px,color:#0E6251;
classDef agentStyle fill:#FEF9E7,stroke:#F39C12,stroke-width:2px,color:#7D6608;
classDef toolStyle fill:#FDEDEC,stroke:#E74C3C,stroke-width:2px,color:#78281F;
classDef dataStyle fill:#F4ECF7,stroke:#8E44AD,stroke-width:2px,color:#512E5F;
subgraph L1["π₯οΈ PRESENTATION LAYER"]
UI["Streamlit Web UI<br/>(Live Ingestion, Review Queue, Analytics)"]:::clientStyle
API_CLIENT["External REST Clients / ERP Systems"]:::clientStyle
end
subgraph L2["β‘ API GATEWAY & CONTROLLER"]
FASTAPI["FastAPI Application<br/>(File Parsing, Async Endpoints, Auth & CORS)"]:::gatewayStyle
end
subgraph L3["π§ AGENTIC ORCHESTRATION ENGINE (LangGraph)"]
GRAPH["StateGraph Workflow Runner<br/>β’ Self-Correction Extraction Loop<br/>β’ Deterministic Business Rule Engine (9 Rules)<br/>β’ Composite Risk Scoring & Threshold Routing<br/>β’ Human-in-the-Loop Checkpoint Pausing"]:::agentStyle
end
subgraph L4["π οΈ AI & INTELLIGENCE SERVICES"]
direction LR
LLM_PRIMARY["β¨ Gemini Multimodal<br/>(Vision & Structured JSON)"]:::toolStyle
LLM_FALLBACK["π GitHub Models / GPT-4o<br/>(Automatic Fallback)"]:::toolStyle
ANOMALY_ENGINE["π Statistical & Forensic Engine<br/>(Z-Score Outliers, Round Numbers, Structuring)"]:::toolStyle
end
subgraph L5["ποΈ PERSISTENCE & CACHE LAYER"]
direction LR
DB[("π PostgreSQL / SQLite<br/>(Invoices, Line Items, Audit Logs, Validations)")]:::dataStyle
CACHE[("β‘ Redis / In-Memory Cache<br/>(SHA-256 Deduplication Registry)")]:::dataStyle
end
UI -->|Upload Document / Human Approval| FASTAPI
API_CLIENT -->|POST /process & POST /resume| FASTAPI
FASTAPI -->|Initialize & Run State| GRAPH
GRAPH <-->|Extract Structured Data| LLM_PRIMARY
LLM_PRIMARY -.->|On Failure / Failover| LLM_FALLBACK
GRAPH <-->|Detect Fraud & Pattern Outliers| ANOMALY_ENGINE
GRAPH <-->|Deduplication Check| CACHE
GRAPH <-->|Query Spend History & Persist Records| DB
FASTAPI -->|Stream Analytics & Status| UI
Raw Invoice (PDF/Image/TXT)
β
βββΊ 1. Multi-Modal Extraction Node (Gemini 2.5/3.6 Flash + GitHub Models fallback with auto-retry)
β
βββΊ 2. Compliance Validation Node (9 Business Rules: Approved Vendors, Tax Math, Spends, Dates)
β
βββΊ 3. SHA-256 Deduplication Node (Redis cache + In-memory fallback prevents duplicate payouts)
β
βββΊ 4. Forensic Anomaly Detection Node (Z-Scores vs historical DB records, Round Numbers, Structuring)
β
βββΊ 5. Risk Scoring & Decision Routing Node (Calibrated 0.0 - 1.0 composite risk score)
β βββ Risk < 0.25 & Amount β€ 100k βββΊ Auto-Approve
β βββ Risk β₯ 0.70 or Tampering βββΊ Auto-Reject
β βββ Risk β₯ 0.25 or Amount > 100k βββΊ Flag for Human Review (Manager / Director)
β
βββΊ 6. Human-in-the-Loop Interrupt Node (Pauses graph execution; resumes on manager decision)
β
βββΊ 7. Database Persistence & Audit Trail (Full state saved to PostgreSQL/SQLite via SQLAlchemy 2.0)
| Rule Name | Severity | Description |
|---|---|---|
max_amount |
ERROR |
Invoice amount must not exceed departmental budget limit (βΉ500,000 / $500,000). |
approved_vendor |
ERROR |
Vendor must be in the approved master vendor registry (24+ verified enterprise vendors). |
date_validity |
ERROR |
Invoice date must be a valid ISO format and not post-dated in the future. |
visual_text_consistency |
ERROR |
Embedded visual scan amount must match digital line items (tampering alert). |
line_item_math |
WARNING |
Individual line items (qty Γ rate) must sum to subtotal within Β±1% tolerance. |
total_math |
WARNING |
Declared Subtotal + Tax Amount must match Total Amount within Β±1% tolerance. |
vendor_id_present |
WARNING |
Vendor must supply a valid tax registration identifier (GSTIN, VAT ID, EIN). |
currency_consistency |
WARNING |
Currency must be a recognized ISO 4217 standard currency code (INR, USD, EUR, GBP). |
payment_terms_check |
INFO |
Invoice must explicitly declare payment terms (e.g., Net 30, Due on Receipt). |
DocAgent includes a dedicated benchmarking harness evaluated against a curated dataset of 20 realistic enterprise invoices across 5 distinct test categories (Clean, Compliance Failures, Anomaly Triggers, Multi-Tier Routing, and Edge Cases):
# Run the evaluation benchmark suite
python eval/run_eval.py --mode offline| Benchmark Metric | Result | Target | Status |
|---|---|---|---|
| Rule Compliance Accuracy | 100.0% (180/180 checks) |
β₯ 95.0% |
β PASSED |
| Anomaly Detection Precision | 1.00 |
β₯ 0.90 |
β PASSED |
| Anomaly Detection Recall | 1.00 |
β₯ 0.90 |
β PASSED |
| Anomaly Detection F1 Score | 1.00 |
β₯ 0.90 |
β PASSED |
| Decision Routing Accuracy | 100.0% (20/20 invoices) |
β₯ 95.0% |
β PASSED |
| Approval Level Routing | 100.0% (20/20 invoices) |
β₯ 95.0% |
β PASSED |
Full evaluation reports are auto-generated to eval/eval_report.md and eval/eval_report.json.
Upload PDF, Image, or TXT invoices to initiate multi-stage extraction, compliance verification, and routing.
Live visual feedback with extracted table breakdown, composite risk gauge, and compliance rule results.
Transparent observability into every step executed by the LangGraph state machine.
| Decision Area | Technology / Pattern | Engineering Rationale |
|---|---|---|
| Agent Framework | LangGraph (StateGraph) | Deterministic state machine, checkpointing/resume capabilities, conditional routing, and granular step observability. |
| Multi-Modal LLMs | Google Gemini + GitHub Models (GPT-4o) | High-speed multi-modal vision with automatic failover for high availability and zero vendor lock-in. |
| Persistence Layer | SQLAlchemy 2.0 ORM | Enterprise relational data layer with auto-fallback to SQLite when PostgreSQL is offline for zero-friction local development. |
| Duplicate Prevention | Redis + SHA-256 Fingerprinting | O(1) idempotent hash lookup over key invoice attributes (vendor:number:total) with configurable TTL. |
| Human-in-the-Loop | LangGraph Interrupts | Pauses workflow execution for manager/director sign-off without dropping state; resumes via /resume/{thread_id}. |
| Backend API | FastAPI | Async I/O, automatic OpenAPI Swagger documentation, dependency injection, and Pydantic validation. |
| Frontend UI | Streamlit | Rapid, reactive UI rendering with interactive audit logs, pending review queues, and live analytics. |
- Python 3.12+
- Gemini API Key (or GitHub Personal Access Token for GitHub Models)
- Docker & Docker Compose (optional, for containerized execution)
-
Clone the repository:
git clone https://github.com/ayushcodes27/doc-agent.git cd doc-agent -
Configure environment variables:
cp .env.example .env # Edit .env and supply your GEMINI_API_KEY (or GITHUB_TOKEN) -
Launch the entire stack:
docker-compose up --build
-
Access the services:
- Streamlit UI: http://localhost:8501
- FastAPI Docs (Swagger): http://localhost:8000/docs
- PostgreSQL:
localhost:5432 - Redis:
localhost:6379
-
Create and activate a virtual environment:
python -m venv .venv # Windows (PowerShell): .venv\Scripts\Activate.ps1 # macOS/Linux: source .venv/bin/activate
-
Install dependencies:
pip install -r requirements.txt
-
Configure environment:
cp .env.example .env # Ensure your GEMINI_API_KEY / GITHUB_TOKEN are set in .env -
Start the FastAPI backend server:
uvicorn api.main:app --host 127.0.0.1 --port 8000 --reload
-
In a separate terminal, launch the Streamlit frontend:
streamlit run ui/app.py
The test suite covers data models, agent nodes, validation rules, anomaly detection, deduplication, database persistence, and the complete LangGraph workflow.
# Run all 42 unit and integration tests
pytest -v
# Run evaluation benchmark suite
python eval/run_eval.py --mode offlinedoc-agent/
βββ agent/ # LangGraph StateGraph workflow, nodes, & human-in-the-loop logic
βββ api/ # FastAPI REST service (/process, /resume, /invoices, /analytics)
βββ db/ # SQLAlchemy 2.0 persistence layer, ORM models, & repository CRUD
βββ eval/ # 20-invoice benchmark dataset & evaluation runner (run_eval.py)
βββ tools/ # Multi-modal extractors, 9-rule validator, anomaly & dedup engines
βββ ui/ # Streamlit frontend (Document ingestion, review queue, analytics)
βββ tests/ # Pytest unit & integration test suite (42 tests)
βββ config.py # Environment configuration & LLM provider settings
βββ docker-compose.yml # Container orchestration (API, UI, PostgreSQL, Redis)
Distributed under the MIT License. See LICENSE for more information.



