Mario Ortega
Return to Case Studies
Enterprise AI Case StudyLive in Production

Production-Grade Document Intelligence & Extraction Engine

An enterprise-grade multi-agent document processing pipeline built to ingest, classify, and extract structured data from heterogeneous documents at massive scale. Combining OCR pipelines, tiered model routing (SLMs & LLMs), DSPy prompt optimization, and PostgreSQL pgvector hybrid RAG—delivered as containerized microservices across Azure and GCP.

LangGraphLangChainDSPyOllama (SLMs)Vision OCRPostgreSQL (pgvector)Hybrid RAGLangfuse ObservabilityFastAPI MicroservicesDocker / Azure / GCP
Cost Optimization
-78.4%

Reduced token consumption through SLM routing and DSPy compiled signatures.

Processing Latency
< 1.4s

Extraction latency achieved with async parallel nodes and fast OCR pre-filters.

Extraction Accuracy
99.2%

Deterministic JSON schema enforcement with Pydantic and self-critique loops.

System ArchitectureEnd-to-End Multi-Agent Pipeline

Multi-Stage Document Ingestion, Routing & Storage Pipeline

The platform processes heterogeneous document formats (Identity Cards, Passports, Financial Records, Legal Certifications, Invoices) through a 5-stage decoupled agentic mesh. Incoming payloads are ingested via secure REST APIs, classified by fast visual-textual models, routed dynamically, normalized via DSPy-compiled prompts, and vectorized into PostgreSQL.

Architecture Topology (High-Resolution Diagram)
Click to Enlarge Diagram
Document Intelligence Multi-Agent Architecture Topology Diagram
Stage 1

Ingestion & OCR

Accepts PDFs, scans, and multi-page images. Applies layout analysis, deskewing, and fast OCR text layer extraction.

IDs / Forms / Certs
Stage 2

Classifier & Router

LangGraph routing agent classifies document typology and confidence score to dispatch tasks to appropriate model tiers.

Taxonomy Tree
Stage 3

Tiered Extraction

Lightweight SLMs (Ollama) handle standard formats. Complex semi-structured docs escalate to DSPy-compiled frontier LLMs.

DSPy + Pydantic JSON
Stage 4

Storage & Hybrid RAG

Persists structured tables to relational PostgreSQL schema while vectorizing full text and FAQ semantic chunks in pgvector.

pgvector + Hybrid RAG
Stage 5

Enterprise APIs

Exposes low-latency OpenAPI endpoints consumed by CRMs, ERPs, client portals, and automated robotic process workflows.

REST / Webhooks
Technical ArchitectureView Mermaid Graph Specification
Toggle Code
graph LR
  subgraph Ingestion ["Stage 1: Multi-Doc Ingestion"]
    A[Raw Documents: PDF / Image / Scan] --> B[OCR & Visual Layout Engine]
  end

  subgraph Classification ["Stage 2: Agentic Routing"]
    B --> C[Document Classification Agent]
    C -->|High Structure: ID / Invoices| D[Tier 1: On-Prem SLM (Ollama)]
    C -->|Unstructured: Complex Certs / Text| E[Tier 2: Frontier LLM + DSPy]
  end

  subgraph Optimization ["Stage 3: Optimization & Guardrails"]
    D & E --> F[DSPy Teleprompter Validation]
    F --> G[Langfuse Observability & Token Tracker]
    G --> H{Pydantic Schema Gate}
    H -->|Fail Assertion| C
    H -->|Pass Assertion| I[Normalized Output]
  end

  subgraph Persistence ["Stage 4: Storage & RAG"]
    I --> J[(PostgreSQL Relational DB)]
    I --> K[(pgvector Embedding Store)]
    K --> L[Semantic Search & FAQ RAG Engine]
  end

  subgraph Distribution ["Stage 5: Agents as a Service"]
    J & L --> M[FastAPI Gateway / Docker Container]
    M --> N[Enterprise ERP / CRM / Upstream APIs]
  end
Executive Context & Challenge

Scaling AI Processing Without Escalating Cloud Spend

When building prototype document extractors, frontier models deliver impressive zero-shot results. However, when moving from a proof-of-concept into enterprise production—processing hundreds of thousands of multi-page documents monthly—uncontrolled LLM calls create unsustainable cloud bills and unpredictable latency.

As the technical lead, the challenge was to eliminate three classic enterprise failure modes:

  • Runaway Token Spend: Bloated document contexts and raw OCR outputs driving up inference costs unnecessarily.
  • Latency Bottlenecks: Forcing simple deterministic forms through slow cloud reasoning models instead of nimble on-prem SLMs.
  • Schema Drift & Risk: Hallucinations in edge-case documents causing downstream failures in core ERP/CRM databases.
Governance & ObservabilityProduction Telemetry
Token Optimization-62.5%
Context Pruning & DSPy Compilation
Local Model Offload71.3%
Specialized Lightweight Models (SLMs)
Pipeline Reliability99.92%
Schema Gate Pass Rate
Distributed tracing monitors token spend, per-node latency, and data schema integrity in real-time across all microservices.
Optimization Strategy

Algorithmic Prompt Optimization via DSPy

Instead of manually editing fragile prompts for dozens of document templates, the system leverages DSPy to programmatically compile and optimize master prompts against synthetic and curated ground-truth datasets.

01 • Schema Enforcement

Strict Typed Signatures

Replaced fragile textual system prompts with declarative typed signatures (`dspy.Signature`). The system deterministically extracts only required fields without conversational bloat.

Efficiency: -45% Token Usage
02 • Systematic Optimization

Automated Prompt Search

DSPy automated few-shot compilation finds the mathematically optimal demonstration examples that maximize accuracy across messy, edge-case document layouts.

Accuracy: 94.1% → 99.2%
03 • Cost-Aware Routing

Tiered Model Architecture

Clean, standardized documents execute instantly on lightweight local models, reserving expensive cloud reasoning models strictly for complex or low-confidence inputs.

Performance: 3.8x Faster Delivery
Data & Retrieval Layer

PostgreSQL, pgvector & Multi-Index Hybrid RAG

Processed document intelligence serves dual purposes: structured transactional queries and semantic unstructured retrieval. The data layer bridges both paradigms within a single unified PostgreSQL engine.

Relational Entity Database

Strict relational tables store validated demographic fields, certificate serials, tax IDs, and validation timestamps with ACID guarantees, foreign keys, and indexes for instant SQL lookup.

PostgreSQL 16 • Prisma ORM • Relational Constraints

pgvector & FAQ Hybrid RAG

Full document clauses and dynamic FAQ indices are chunked, embedded, and stored with HNSW indexes in pgvector. Combines BM25 lexical search with vector cosine similarity for pinpoint question-answering.

pgvector HNSW • Reciprocal Rank Fusion (RRF)
Cloud Infrastructure & Integration

Containerized Cloud Microservices & Enterprise APIs

The entire agentic system is packaged into modular, containerized Docker microservices exposed as secure REST and async webhook APIs. Designed for multi-cloud deployments across Azure Container Apps and Google Cloud Platform (GCP Cloud Run / GKE).

Container Runtime
Docker / OCI Images

Lightweight multi-stage builds with CPU/GPU runtime support.

Cloud Backbone
Azure & GCP Cloud Run

Autoscaling from 0 to N instances based on message queue depth.

Integration Interface
FastAPI / OpenAPI 3.1

Seamless ingestion hooks for enterprise CRMs, ERPs & portals.