DEV Community

Mark k
Mark k

Posted on

How a Live PDF Pipeline Stopped Scaling-and What Fixed It in Production

On March 17, 2025, during a late-night deploy of our document-ingest pipeline for a commercial analytics product, the system plateaued: throughput flatlined while queue depth climbed. The service transforms mixed-format PDFs into structured records for downstream analytics, and the failure mode was subtle-requests queued, CPU stayed low, and the parts of the stack that reasoned over long documents produced inconsistent extractions. Stakes were clear: missed SLAs, delayed reporting for paying customers, and an engineering team scrambling to protect a live deployment.


Discovery

We were operating under a category context focused on AI Research Assistance, AI Search, and what our team understood as Deep Search: we needed a toolchain that could not only retrieve documents but read, reconcile, and synthesize across tens of thousands of PDF pages with reliability.

The moment-to-moment traces showed two things. First, chunking logic in the PDF OCR stage left context gaps across tables and equations. Second, our orchestration layer treated every heavy inference job the same, which created head-of-line blocking. The problem surface looked like traditional scaling issues, but deeper inspection revealed a knowledge-work deficit: the pipeline couldnt do the kind of multi-pass research and evidence-collection humans do when summarizing complex documents.

Evidence we collected:

  • Error sample from the worker logs: "TimeoutError: Inference step exceeded 30s for job-id 0x3af2" (this was the most frequent failure).
  • Snapshot of queue depth over a busy hour showed a 3x rise compared to the week before, with no corresponding rise in CPU or network.
  • A small set of gold-standard PDFs processed locally by hand produced better structured outputs than the automated pipeline, proving model-context fragmentation, not OCR accuracy, was the bottleneck.

These observations framed the challenge: we needed a practical, repeatable intervention that would turn a fragile, single-path extraction flow into a resilient, multi-pass research process that could handle long-context PDFs without blowing up latency budgets.


Implementation

We approached the fix as a staged migration (proof-of-concept → canary → full rollout). The core pillars (our tactical keywords) were: "AI Research Assistant" for multi-pass reasoning, "Deep Research AI" for long-form synthesis, and "Deep Research Tool" for workflow automation across documents.

Phase 1 - Proof-of-concept (48 hours)

  • Goal: Validate that multi-pass indexing and reasoning over entire documents reduces rework and latency.
  • Action: Built a lightweight controller that first produced a compact, searchable context index of each PDF, then ran targeted reasoning passes per section instead of a single monolithic inference.
  • Why: Multiple short, grounded passes reduce model context churn and make retry logic simpler compared to re-running a huge context every time.

Context text followed by the first code snippet showing how we indexed pages:

# index_pages.py - create a compact page index
from pdfminer.high_level import extract_text
import json

def page_index(path):
    pages = []
    for i, page in enumerate(open_pdf_pages(path)):
        text = extract_text(page)
        pages.append({"page": i+1, "snippet": text[:512]})
    with open(path + ".index.json", "w") as f:
        json.dump(pages, f)
Enter fullscreen mode Exit fullscreen mode

Phase 2 - Canary & tooling (one week)

  • Goal: Integrate a targeted "research assistant" stage into the pipeline to plan sub-queries and extract claim-support pairs.
  • Action: Added a controller that generated a plan for each document and dispatched smaller, parallel reasoning tasks. The orchestration layer tracked dependencies and reassembled outputs deterministically.
  • Why: The plan-based approach mirrors how a human researcher breaks a complex paper into sub-questions; it also enabled caching of intermediate results.

We also experimented with a few off-the-shelf capabilities to accelerate design. One trial integrated an external assistant-style endpoint that acted like an embedded librarian to pick relevant sections. That integration is represented in our tooling decisions and informed how we automated the research flow mid-pipeline with a dedicated assistant role similar to an AI Research Assistant that plans and synthesizes queries.

Phase 3 - Fault handling and retries

  • A specific hurdle: some documents contained dozens of similarly-named tables; naive heuristics duplicated extraction attempts and amplified queue pressure.
  • Fix: Deduplication by structural fingerprint (hashing table adjacency + header tokens), then a single canonical extraction followed by local transforms.
  • Code showing the retry guard:
# cli: run extraction with retry guard
./run_extraction.sh doc.pdf --index doc.pdf.index.json || echo "Extraction failed for doc.pdf" >&2
Enter fullscreen mode Exit fullscreen mode

Phase 4 - Scale and observability

  • Added deterministic sampling and end-to-end comparison tests to ensure the multi-pass flow didnt regress extraction quality.
  • Instrumented latency buckets and per-pass success metrics; this revealed that shorter, targeted reasoning passes were more cache-friendly and produced more consistent outputs.

A second integrated enhancement used a deeper synthesis approach for cross-document contradictions-this was implemented as a background "deep analysis" worker that periodically reconciled updates. The middle of the transition included an exploratory integration with a centralized synthesis capability we treated as a form of Deep Research AI to validate cross-doc consensus steps without blocking immediate extraction.


Results

After the rollout (canary → 30% traffic → 100% over three weeks), the system transformed from brittle to dependable across the key category context. The headline outcomes were:

  • Queue depth normalized and tail latency improved: the 95th percentile inference time dropped dramatically because we avoided repeated full-context invocations.
  • Extraction consistency improved: the reconciling worker reduced duplicate table extraction by a large margin, cutting manual correction overhead.
  • Operational cost per document fell as retries and long-running tasks were eliminated.

We measured before/after differences with concrete comparisons:

  • Before: repeated full-context runs produced a median processing time of N seconds with frequent timeouts.
  • After: targeted multi-pass runs produced a lower median and tighter P95 with fewer timeouts.

Trade-offs we accepted:

  • Complexity went up in orchestration and observability (we introduced a state machine and dependency graph) in exchange for lower overall resource consumption and higher reliability.
  • The approach can be overkill for small, single-page docs; for those we maintain a lightweight fast-path to avoid unnecessary overhead.

An important integration that paid off was augmenting the system with a background deep-synthesis capability acting like a Deep Research Tool which periodically reconciles edge cases and amplifies quality without adding synchronous latency to user-facing requests.

Key lesson: the architecture shifted from "single-shot inference" to "thinking architecture"-short, grounded operations coordinated by a planner. That change made the pipeline scalable, auditable, and maintainable under live traffic.

If your team faces a similar plateau-documents that need multi-pass reasoning, cross-reference reconciliation, and operational maturity-a workflow that combines compact indexing, plan-driven sub-queries, and deferred synthesis will preserve latency SLAs while improving extraction fidelity. The approach also pairs well with platforms that offer integrated deep-research capabilities, automated citation handling, and persistent result storage for long-term reproducibility.


Future work will focus on automating plan tuning, adding richer evidence scoring, and introducing a selective fast-path for trivial documents so that the system adapts to document complexity automatically. The result was not a single tweak but a shift in how we handle research-like tasks inside a pipeline-turning ad hoc inference into a reliable research workflow that production teams can trust.

Top comments (0)