Agentic AI is transforming supply chain automation by enabling systems to act with autonomy—identifying needs, negotiating with vendors, placing orders, and tracking fulfillment, all with minimal human intervention. This deep-dive tutorial will guide you through building a practical agentic AI workflow for procurement and fulfillment, leveraging open-source tools and APIs.
As we covered in our complete guide to AI workflow automation in 2026 supply chains, autonomous procurement is a critical blueprint for the next generation of resilient, efficient operations. Here, we’ll show you how to orchestrate it step by step.
Prerequisites
- Python 3.10+ (tested with 3.11)
- LangChain (v0.1.0 or later) for agentic workflow orchestration
- OpenAI API Key (or Azure OpenAI, or local LLM with compatible API)
- FastAPI (v0.95+), for exposing endpoints
- Basic understanding of REST APIs and JSON
- Familiarity with procurement and fulfillment processes
- Optional: Access to a mock supplier API (or Postman Mock Server)
1. Set Up Your Development Environment
-
Create and activate a virtual environment:
python3 -m venv agentic-supplychain-env source agentic-supplychain-env/bin/activate
-
Install required Python packages:
pip install langchain openai fastapi uvicorn requests
-
Set up your OpenAI API key:
export OPENAI_API_KEY=sk-...
(Replace with your actual API key. Consider usingpython-dotenvfor local development.)
2. Define the Agentic Workflow
The agentic AI workflow for procurement and fulfillment typically involves:
- Monitoring inventory and demand signals
- Identifying procurement needs
- Fetching vendor quotes and evaluating options
- Negotiating (if supported by vendor APIs)
- Placing orders autonomously
- Tracking order fulfillment
We'll implement a simplified, testable version of this using LangChain’s AgentExecutor and basic tool integrations.
3. Build the Core Agent with LangChain
-
Initialize your project:
mkdir agentic-procurement cd agentic-procurement touch agent.py
-
Define tools for the agent:
We'll simulate vendor APIs and inventory checks with simple Python functions. In production, replace these with real API calls.
from langchain.agents import initialize_agent, Tool from langchain.llms import OpenAI import requests def check_inventory(product_id): # In reality, call your inventory DB/API inventory = { "A123": 10, "B456": 2, # Low stock triggers procurement } return inventory.get(product_id, 0) def get_vendor_quote(product_id, quantity): # Replace with actual vendor API request if product_id == "B456": return {"vendor": "AcmeSupplies", "price_per_unit": 15.0, "available_qty": 100} return {"vendor": "Unknown", "price_per_unit": 0.0, "available_qty": 0} def place_order(vendor, product_id, quantity): # Replace with POST to vendor's order endpoint return f"Order placed: {quantity} x {product_id} from {vendor}" tools = [ Tool( name="CheckInventory", func=lambda input: str(check_inventory(input)), description="Checks inventory for a given product_id" ), Tool( name="GetVendorQuote", func=lambda input: str(get_vendor_quote(*input.split(','))), description="Gets vendor quote for product_id and quantity (input: 'B456,10')" ), Tool( name="PlaceOrder", func=lambda input: place_order(*input.split(',')), description="Places order with vendor for product_id and quantity (input: 'AcmeSupplies,B456,10')" ), ] -
Initialize the agent:
llm = OpenAI(temperature=0, model_name="gpt-3.5-turbo") agent = initialize_agent( tools, llm, agent="zero-shot-react-description", verbose=True ) -
Define the agent’s workflow prompt and test it:
def run_procurement_workflow(product_id, reorder_threshold=5, reorder_qty=10): prompt = ( f"Check inventory for {product_id}. " f"If inventory is below {reorder_threshold}, " f"get a vendor quote for {product_id} and {reorder_qty}. " f"If the price is acceptable (under $20/unit), " f"place an order for {reorder_qty} units." ) result = agent.run(prompt) print(result) if __name__ == "__main__": run_procurement_workflow("B456")Description of expected output:
The agent will check inventory, realize B456 is below threshold, fetch a vendor quote, and (since $15/unit is under $20), place an order. The output will include the agent’s reasoning and final action.
4. Expose as an API with FastAPI
-
Create a new API entrypoint:
touch api.py
-
Implement the FastAPI server:
from fastapi import FastAPI from agent import run_procurement_workflow app = FastAPI() @app.post("/procure") def procure(product_id: str, reorder_threshold: int = 5, reorder_qty: int = 10): result = run_procurement_workflow(product_id, reorder_threshold, reorder_qty) return {"result": result} -
Run the API server:
uvicorn api:app --reload
Screenshot description: The terminal should display
Uvicorn running on http://127.0.0.1:8000. -
Test your endpoint with
curlor Postman:curl -X POST "http://127.0.0.1:8000/procure?product_id=B456"
Screenshot description: The response will show the agentic workflow output, including inventory check, quote retrieval, and order confirmation.
5. Simulate Realistic Vendor APIs (Optional)
-
Set up a Postman Mock Server or simple Flask app to simulate a supplier API.
pip install flask
-
Example Flask mock supplier:
from flask import Flask, request, jsonify app = Flask(__name__) @app.route('/quote', methods=['GET']) def quote(): product_id = request.args.get('product_id') qty = int(request.args.get('qty', 0)) if product_id == "B456": return jsonify({"vendor": "AcmeSupplies", "price_per_unit": 15.0, "available_qty": 100}) return jsonify({"vendor": "Unknown", "price_per_unit": 0.0, "available_qty": 0}) @app.route('/order', methods=['POST']) def order(): data = request.json return jsonify({"status": "confirmed", "order_id": "ORD123", "details": data}) if __name__ == "__main__": app.run(port=5001) -
Update your agent tools to call the real (mocked) supplier API endpoints using
requests.
For a deeper dive into vendor automation, see Automating Vendor Management Workflows in Supply Chains: 2026’s Top AI Strategies.
6. Orchestrate Fulfillment Tracking
-
Add a fulfillment tracking tool:
def track_fulfillment(order_id): # Simulate fulfillment status; in production, poll vendor API return {"order_id": order_id, "status": "shipped", "eta": "2026-05-15"} tools.append( Tool( name="TrackFulfillment", func=lambda input: str(track_fulfillment(input)), description="Tracks fulfillment for a given order_id" ) ) -
Update your workflow to include fulfillment tracking:
def run_procurement_workflow(product_id, reorder_threshold=5, reorder_qty=10): prompt = ( f"Check inventory for {product_id}. " f"If inventory is below {reorder_threshold}, " f"get a vendor quote for {product_id} and {reorder_qty}. " f"If the price is acceptable (under $20/unit), " f"place an order for {reorder_qty} units. " f"Then track fulfillment for the order." ) result = agent.run(prompt) print(result) -
Re-test your endpoint:
curl -X POST "http://127.0.0.1:8000/procure?product_id=B456"
Screenshot description: The output should now include fulfillment status with an ETA.
For advanced orchestration and integration of IoT signals into fulfillment, see Integrating IoT Devices with AI Workflow Automation in Supply Chains: Secure Strategies for 2026.
Common Issues & Troubleshooting
-
OpenAI API errors: Ensure your API key is correct and you have sufficient quota. Check
OPENAI_API_KEYenvironment variable. - Agent not calling tools: Make sure your tool descriptions are clear and match the prompt’s intent. Review agent logs for reasoning steps.
- FastAPI endpoint not responding: Confirm the server is running and you’re using the correct port (default: 8000).
- Vendor API timeouts: When using real or mock APIs, ensure network connectivity and correct URLs.
- LLM hallucination or wrong actions: Refine prompt instructions and tool descriptions. Consider more structured tool input/output.
For more on avoiding pitfalls, see Top Mistakes to Avoid When Using Agentic AI for Workflow Automation.
Next Steps
- Integrate real supplier and ERP APIs for live data and transactions.
- Add authentication, logging, and audit trails for compliance and security.
- Explore agentic negotiation and multi-vendor selection logic.
- Evaluate and compare agentic AI workflow tools—see Top Agentic AI Workflow Tools for 2026: A Hands-On Comparison.
- Learn more about securing your AI workflows in Zero-Trust Approaches to Securing AI Workflow Automation in Supply Chains.
Agentic AI will continue to redefine supply chain automation. For a full landscape view and future blueprints, revisit our Pillar: AI Workflow Automation in 2026 Supply Chains—Blueprints, Risks, and Industry Leaders.