What This Agent Does

The Order Risk Agent is the second agent in the pipeline. It receives the list of flagged suppliers from Agent 1 and traces the cascade: which materials come from those suppliers, which products use those materials, which open customer orders are for those products.

This agent does not use an LLM. The cascade analysis is deterministic — a set of structured database queries over four tables. The LLM was deliberately excluded to avoid token costs on logic that does not require reasoning.


The Cascade Chain

Flagged Supplier
  → Raw Materials (supplysafe_raw_materials WHERE supplier_id = flagged)
  → Inventory Check (supplysafe_inventory WHERE material_id IN affected_materials)
  → Bill of Materials (supplysafe_bill_of_materials WHERE material_id IN critical_materials)
  → Products (supplysafe_products WHERE product_id IN bom_products)
  → Order Items (supplysafe_order_items WHERE product_id IN affected_products)
  → Orders (supplysafe_orders WHERE id IN affected_order_items AND status NOT IN delivered, cancelled)

This chain runs in Python using Supabase REST API queries. The full traversal from flagged supplier to affected orders completes in under 2 seconds.


Step-by-Step Execution

Step 1 — Read flagged suppliers from state

The agent reads suppliers_flagged from the LangGraph state object written by Agent 1.

Step 2 — Find affected materials

For each flagged supplier, the agent queries supplysafe_raw_materials to find all materials sourced from that supplier.

Step 3 — Check inventory levels

For each affected material, the agent queries supplysafe_inventory and checks quantity_on_hand against reorder_point and safety_stock_level. Materials below safety stock are flagged as critical.

Step 4 — Trace through BOM

The agent queries supplysafe_bill_of_materials to find all products that contain the affected materials. This is a many-to-many relationship — one material can appear in multiple products.

Step 5 — Find open orders

The agent queries supplysafe_order_items for all line items containing affected products, then retrieves the parent orders from supplysafe_orders where status is processing, in_production, ready, or delayed.

Step 6 — Write to state