π Introduction
When building enterprise Agentic AI Workflows, developers almost universally fall into the same trap: using Large Language Models (LLMs) like GPT-4o, Claude 3.5 Sonnet, or Llama 3 to make operational micro-decisions:
- Should this order be processed via standard fulfillment, local delivery, or flagged for fraud?
- Is this high-value return eligible for auto-approval?
- Which regional warehouse should handle this specific SKU combination?
While LLMs excel at creative writing and open-ended text generation, using an autoregressive model for discrete conditional logic introduces severe engineering bottlenecks: 500msβ2500ms latency per node, excessive token costs, schema parsing failures, and uncalibrated confidence metrics.
In this zero-to-hero guide, we will build a complete, production-ready E-Commerce Order Routing & Fraud Mitigation Engine from scratch using Python, LangGraph, and Jevβthe breakthrough System-1 decision model by TypeSafe AI.
β‘ System-1 vs. System-2 AI Paradigm
Named after Daniel Kahnemanβs cognitive framework (Thinking, Fast and Slow):
- System 2 (Generative LLMs): Slow, deliberate, sequential reasoning (Chain-of-Thought, token generation).
- System 1 (Jev Engine): Fast, parallel, intuitive, structured decision-making in a single pass without token generation.
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Incoming Order Payload β
β (Cart Items, Delivery Address, Customer History) β
ββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Jev (System-1 Decision Node) β
β Evaluates Fulfillment Route, VIP Eligibility, and β
β Fraud Risk Score in parallel (70ms - 150ms) β
βββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββ
β
ββββββββββββββββββΌβββββββββββββββββ
βΌ βΌ βΌ
ββββββββββββββββββββ ββββββββββββββββββββ ββββββββββββββββββββ
β High Risk / Fraudβ β Backorder / VIP β β Standard Route β
β (Security Hold) β β (Human Review) β β (Auto-Dispatch) β
ββββββββββββββββββββ ββββββββββββββββββββ ββββββββββββββββββββ
Comparative Breakdown
| Metric / Feature | Generative LLMs (System 2) | Jev by TypeSafe AI (System 1) |
|---|---|---|
| Primary Output | Unstructured Text / JSON String | Pure Typed Decisions & Probabilities |
| Execution | Autoregressive (token-by-token) | Non-autoregressive (parallel evaluation) |
| Latency | 800ms β 3000ms | 70ms β 200ms |
| Cost per 1M Decisions | ~$15.00 β $30.00 | ~$0.042 |
| Schema Integrity | Requires Pydantic / JSON Repair | Guaranteed by design |
| Confidence Calibration | Uncalibrated (hallucinates certainty) | Calibrated ($RLCD$ Probabilities) |
π οΈ The Three Jev Decision Primitives
Jev evaluates three core primitive types simultaneously in a single API execution:
-
Noul(Boolean / Binary Logic): Evaluates True/False conditions (e.g., Is this user eligible for VIP express dispatch?). -
Choice(Categorical Selection): Selects the single best option from up to 255 pre-defined buckets (e.g., Which fulfillment channel should handle this order?). -
Score(Ordinal / Numeric Scale): Rates inputs against a defined scale with custom rubric guidance (e.g., Evaluate fraud risk level from 1 to 5).
ποΈ Architecture: What We Are Building
We will build an automated Smart Retail Order Orchestrator using LangGraph:
-
Jev Evaluation Node:
- Evaluates
Fulfillment Channel(Choice). - Assesses
VIP Express Eligible(Noul). - Assesses
Fraud Risk Score(Score).
- Evaluates
-
Conditional Router Edge:
- If
Fraud Risk Score$\ge 4 \rightarrow$ Fraud Investigation Node. - If
Confidence Score$< 0.75 \rightarrow$ Manual Operations Review Node. - Otherwise $\rightarrow$ Automated Warehouse Routing Node.
- If
π Step-by-Step Implementation
Prerequisites & Setup
Install the required dependencies:
pip install typesafe-ai langgraph langchain-core pydantic python-dotenv
Configure your environment variables (.env):
TYPESAFE_API_KEY="your_typesafe_jev_api_key"
Step 1: Define Shared State Schema
Create main.py and set up the shared state object using Python's TypedDict:
import os
from typing import TypedDict, Optional
from typesafe import TypeSafeClient
# Initialize TypeSafe Jev Client
client = TypeSafeClient(api_key=os.getenv("TYPESAFE_API_KEY"))
# Define LangGraph State Schema
class OrderState(TypedDict):
order_id: str
order_details: str
fulfillment_channel: Optional[str]
is_vip_express: Optional[bool]
fraud_risk_score: Optional[int]
confidence_score: Optional[float]
execution_status: Optional[str]
dispatch_notes: Optional[str]
Step 2: Build the Jev Decision Node
The Jev node evaluates three distinct conditions in a single pass without token-by-token text generation:
def jev_order_evaluator(state: OrderState) -> OrderState:
"""Uses Jev System-1 to classify fulfillment route, VIP status, and fraud risk in one call."""
order_context = state["order_details"]
# Query Jev Engine
response = client.jev.evaluate(
state=order_context,
questions={
# Primitive 1: Choice
"fulfillment_channel": {
"type": "choice",
"instructions": "Determine the optimal inventory fulfillment channel for this order.",
"criteria": {
"local_store": "Order items are available locally and address is within same ZIP code area.",
"regional_hub": "Standard physical goods requiring regional distribution center shipment.",
"digital_delivery": "E-books, software keys, gift cards, or purely digital entitlements.",
"freight_special": "Heavy bulk items over 70 lbs requiring freight handling."
}
},
# Primitive 2: Noul (Boolean)
"is_vip_express": {
"type": "noul",
"instructions": "Determine if this order qualifies for priority same-day packaging.",
"criteria": {
"true": "Order explicitly specifies express shipping, subscription tier VIP, or perishable goods.",
"false": "Standard delivery speeds or non-urgent items."
}
},
# Primitive 3: Score
"fraud_risk": {
"type": "score",
"instructions": "Evaluate potential transaction risk or policy abuse indicator.",
"legend": {
"1": "standard verified purchase pattern",
"3": "minor inconsistency in shipping/billing or high order quantity",
"5": "flagged stolen card pattern, disposable email, or extreme payment discrepancy"
}
}
}
)
# Unpack typed responses
channel_res = response.answers["fulfillment_channel"]
vip_res = response.answers["is_vip_express"]
fraud_res = response.answers["fraud_risk"]
return {
**state,
"fulfillment_channel": channel_res.selected_option,
"is_vip_express": vip_res.value,
"fraud_risk_score": fraud_res.score,
"confidence_score": min(channel_res.confidence, vip_res.confidence)
}
Step 3: Define Execution Nodes & Router Logic
from langgraph.graph import StateGraph, END
def fraud_hold_node(state: OrderState) -> OrderState:
return {
**state,
"execution_status": "FLAGGED_FOR_AUDIT",
"dispatch_notes": f"π¨ High Risk Score ({state['fraud_risk_score']}/5). Payment placed on administrative hold."
}
def manual_review_node(state: OrderState) -> OrderState:
return {
**state,
"execution_status": "MANUAL_OPS_QUEUE",
"dispatch_notes": f"β οΈ Low confidence classification ({state['confidence_score']:.2f}). Forwarded to fulfillment manager."
}
def warehouse_dispatch_node(state: OrderState) -> OrderState:
channel = state["fulfillment_channel"].upper()
priority = "EXPRESS" if state["is_vip_express"] else "STANDARD"
return {
**state,
"execution_status": "PROCESSED",
"dispatch_notes": f"β
Order routed to [{channel}] with [{priority}] fulfillment priority."
}
def route_order(state: OrderState) -> str:
# 1. Fraud Risk Check
if state["fraud_risk_score"] and state["fraud_risk_score"] >= 4:
return "fraud_hold"
# 2. Confidence Threshold Check (Calibrated Probability)
if state["confidence_score"] and state["confidence_score"] < 0.75:
return "manual_review"
# 3. Standard Path
return "warehouse_dispatch"
Step 4: Assemble & Compile the LangGraph Pipeline
workflow = StateGraph(OrderState)
# Add Graph Nodes
workflow.add_node("jev_evaluator", jev_order_evaluator)
workflow.add_node("fraud_hold", fraud_hold_node)
workflow.add_node("manual_review", manual_review_node)
workflow.add_node("warehouse_dispatch", warehouse_dispatch_node)
# Define Entry Point
workflow.set_entry_point("jev_evaluator")
# Add Conditional Routing
workflow.add_conditional_edges(
"jev_evaluator",
route_order,
{
"fraud_hold": "fraud_hold",
"manual_review": "manual_review",
"warehouse_dispatch": "warehouse_dispatch"
}
)
# Attach Terminal Edges
workflow.add_edge("fraud_hold", END)
workflow.add_edge("manual_review", END)
workflow.add_edge("warehouse_dispatch", END)
# Compile Graph
app = workflow.compile()
Step 5: Test Execution Runs
if __name__ == "__main__":
sample_order = {
"order_id": "ORD-88392",
"order_details": "Customer ordered 1x Pro Gaming Laptop ($1,800). Shipping address is local store pickup (ZIP 90210). Priority status set to VIP Same-Day.",
"fulfillment_channel": None,
"is_vip_express": None,
"fraud_risk_score": None,
"confidence_score": None,
"execution_status": None,
"dispatch_notes": None
}
result = app.invoke(sample_order)
print("\n--- ORDER PIPELINE RESULTS ---")
print(f"Fulfillment Channel : {result['fulfillment_channel']}")
print(f"VIP Express Eligible: {result['is_vip_express']}")
print(f"Fraud Risk Rating : {result['fraud_risk_score']}/5")
print(f"Engine Confidence : {result['confidence_score']:.2f}")
print(f"Status : {result['execution_status']}")
print(f"Notes : {result['dispatch_notes']}")
π Performance & Cost Comparison
| Metric | Generative LLM Node (GPT-4o) | Jev System-1 Engine |
|---|---|---|
| Average Latency | ~1400 ms | ~90 ms (~15x Faster) |
| Cost per 1M Executions | ~$18.00 | ~$0.042 (~400x Cheaper) |
| Schema Reliability | Susceptible to JSON parsing errors | 100% Guaranteed Native Types |
| Confidence Score | Heuristic / Often Overconfident | Calibrated Probabilistic Score |
π― Key Takeaways
- Decouple Logic from Text Generation: Reserve LLMs like GPT-4o or Claude for synthesizing text, writing emails, or summarizing documents. Use System-1 decision engines like Jev for routing and classification nodes.
-
Deterministic Outputs: Jev provides zero-parsing overhead by producing calibrated native types (
bool,int,enum) directly. - Calibrated Fallbacks: By pairing Jev's calibrated confidence scores with LangGraph conditional edges, you can implement robust human-in-the-loop triggers whenever uncertainty passes your set threshold.
π Related Resources & Further Reading
If you found this guide valuable, drop a reaction β€οΈ, bookmark it π, and leave a comment below!
Top comments (0)