Skip to main content
AI & Engineering · 6 min

From Demo to Production: Structuring Deterministic Workflows with LangGraph to Stop Hallucinations

Unconstrained AI agent loops fail in production. Here is how to architect 5-node deterministic StateGraphs with shared memory, local vector search, and typed Pydantic models.

Pablo Fesa
Pablo Fesa Software Architect & Tech Lead

Almost any developer can assemble an AI agent demo over a weekend using a standard ReAct loop: you supply the model with tools, give it a system prompt, and let it decide what to call on every iteration.

In an internal slide presentation, it looks magical. But the moment you connect that open loop to a mission-critical workflow with real financial impact (such as automotive claims appraisal, insurance underwriting, or legal quotes), the system fractures:

  • The agent enters recursive loops calling redundant tools.
  • It overwrites verified parameters with speculative guesses.
  • It fabricates arguments not supported by the downstream API.
  • Token consumption climbs unpredictably with zero correlation to output quality.

This article details the architecture we engineered for PeritIA, transforming computer vision and automotive appraisal into a deterministic, auditable pipeline backed by comprehensive automated test coverage.

Why Unconstrained Agent Loops Fail in Production

The core vulnerability is relinquishing control of the execution flow (control flow) to a probabilistic model.

In conventional software, application state and business logic dictate transitions. In an open-ended agent, transitions depend on the statistical probability that the model picks the correct tool at the exact right moment. If that probability is 95% per step, after six consecutive decisions total pipeline reliability drops below 74%.

For production enterprise systems, the equation must be inverted: deterministic code governs state transitions, while AI models resolve strictly bounded cognitive tasks within individual nodes.

The 5-Node StateGraph Architecture

Rather than an unconstrained loop, we model the workflow as a directed acyclic state graph with shared, typed memory using LangGraph:

[Vehicle Damage Photographs]
            │
            ▼
 ┌─────────────────────┐
 │ 1. Identification   │ ──► Validates make, model, photo quality
 └──────────┬──────────┘
            │
            ▼
 ┌─────────────────────┐
 │ 2. Damage Detection │ ──► Multimodal bodywork damage classification
 └──────────┬──────────┘
            │
            ▼
 ┌─────────────────────┐
 │ 3. Parts Mapping    │ ──► Local dense retrieval (pgvector + fastembed)
 └──────────┬──────────┘
            │
            ▼
 ┌─────────────────────┐
 │ 4. Cost Dependencies│ ──► Deterministic parts graph query in Neo4j
 └──────────┬──────────┘
            │
            ▼
  < Cost > 70% Actual Cash Value? >
        │               │
      [Yes]            [No]
        │               │
        ▼               ▼
 [Total Loss Case]  [Itemized Repair Estimate]

Node 1: Quality and Identification Gate

Verifies whether uploaded photographs contain sufficient resolution and match the registered vehicle. If an image is blurry or irrelevant, the pipeline halts immediately without spending tokens on downstream processing.

Node 2: Multimodal Damage Detection

Applies vision models to classify affected components and severity ratings, constrained to an authoritative ontology of homologated parts.

Node 3: Local Dense Vector Retrieval

Instead of shipping voluminous parts catalogs to external APIs, detected parts are matched against a localized pgvector store using fastembed. Zero API token overhead for parts indexing.

Node 4: Component Relationships and Labor Times in Neo4j

Replacing a modern headlamp often requires removing the front bumper. Rather than asking a model to recall repair procedures from prompt weights, the system queries a graph database (Neo4j) containing official repair steps and standardized labor times.

Node 5: Conditional Resolution Branch

The terminal node evaluates total projected repair costs against the vehicle’s market valuation. If repair costs cross the 70% threshold, it triggers a total-loss determination; otherwise, it compiles an itemized repair estimate.

Testing Agentic Pipelines for Production Confidence

The primary objection to deploying autonomous systems in enterprise pipelines is unpredictable behavior.

We enforce a three-layer verification strategy:

  1. Immutable Pydantic schemas: Every node enforces rigid input and output schemas. If a model generates an unexpected attribute, the boundary raises a validation exception before state mutation occurs.
  2. Deterministic mock unit tests: 142 pytest cases isolate each graph node, mocking model responses to verify state transitions, threshold boundaries, and arithmetic accuracy.
  3. Regression evaluation via LangSmith: Running against a gold-standard dataset of 50 pre-labeled historical claims ensures prompt modifications or model version bumps never degrade evaluation accuracy below baseline.

Engineering Takeaway

The most impactful enterprise AI is not the one that composes poetry or wanders freely through unconstrained tool loops. It is the one that executes discrete cognitive tasks inside a deterministic harness governed by rigorous software engineering.

By designing agents as finite state machines with explicit typed contracts, you transform fragile prototypes into dependable operational systems that save real hours every single day.

Topics: #LangGraph #Python #Hybrid RAG #pgvector #pytest
Continue Reading

Related Articles

Looking to architect resilient systems without budget overruns?

Book a 15-minute diagnostic session with Pablo to audit your bottlenecks and map out a clear technical path.