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.
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:
- 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.
- Deterministic mock unit tests: 142 pytest cases isolate each graph node, mocking model responses to verify state transitions, threshold boundaries, and arithmetic accuracy.
- 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.
Related Articles
Laya on CPU, No GPU: What I Measured on Three Machines Before Putting It in Production
38ms was with a GPU. On CPU, 94 to 848ms depending on the machine. Real benchmarks, the thread trap, ONNX vs PyTorch, and how the deployment ended up on a VPS.
Laya: The Open-Source System-1 Decision Engine Outperforming Jev on Latency and Calibration
40ms on GPU, 94 to 848ms on CPU depending on the machine, multilingual, and zero per-token cost. How Laya brings calibrated probabilistic inference to production pipelines alongside LLMs.
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.