Building a Pharmacy Expiry Management Automation with n8n: AI Decision-Making + Human Approval

n8n workflow uses AI to recommend drug expiry actions while keeping pharmacists in the approval loop.
Built on the n8n platform, this workflow triggers daily at 6 PM to pull inventory, sales logs, branch stock, and supplier return policies, then feeds the unified context to an LLM for analysis. The AI agent recommends inter-store transfers, supplier returns for credit, safety-gated discount promotions, and reorder halts — balancing financial control, operational efficiency, and patient safety in one framework. Crucially, no action executes automatically: every recommendation requires pharmacist approval, making this a reusable "AI analysis + human accountability" paradigm for regulated industries.
Managing drug expiry dates has always been a thorny challenge in pharmacy operations. Mishandled near-expiry medications can mean inventory losses at best and patient safety risks at worst. One developer built a "cascading drug expiry automation system" on the n8n workflow platform, connecting daily inventory checks, sales analysis, stock transfer decisions, and supplier returns into a single pipeline — while preserving pharmacist approval at critical steps. This solution demonstrates a textbook implementation of "AI automation + Human-in-the-loop" for a specialized business domain.
Fully Automated Data Collection Triggered Daily
The workflow's logic is straightforward: it fires automatically every day at 6 PM, pulling data from multiple sources simultaneously. The system fetches the day's inventory list, sales log, branch stock levels across all store locations, and supplier return and credit policies.
The real value here is consolidating data that would otherwise require manual, item-by-item reconciliation into a single pipeline. Pharmacies typically operate multiple branch locations with independent inventory and sales records — aggregating them manually is both time-consuming and error-prone. A scheduled trigger lets the system complete this foundational work at a fixed time each day, providing a complete data foundation for downstream intelligent decision-making.

A Code Node Assembles Context for AI Decision-Making
Once data collection is complete, all information flows through a dedicated Code Node for unified processing. This step essentially transforms heterogeneous data from inventory, sales, store locations, and suppliers into structured context that the AI can reason over.

This fully assembled context is then fed into an AI large language model (LLM) node — the core of the entire workflow, where decisions are made. Unlike simple notification-style automations, the AI agent here doesn't just alert the pharmacist that a batch is expiring soon. It analyzes sales data and generates a complete set of actionable recommendations. This leap from "alerting" to "decision support" is precisely where AI creates genuine value in business automation.
n8n is an open-source, low-code workflow automation platform that lets users connect services and data sources through a visual node interface, while also supporting JavaScript or Python directly inside Code Nodes for complex data transformation. Compared to purely no-code tools like Zapier or Make, n8n's key advantage is self-hosting — data never has to pass through third-party servers, which is especially important for healthcare scenarios involving patient information or sensitive inventory data. Its LLM nodes natively support major model APIs including OpenAI and Anthropic, allowing structured context to be passed directly as a prompt and the model's natural-language output to be parsed into data structures consumable by downstream nodes — enabling seamless "data pipeline + AI reasoning" integration.
AI-Generated Options for Handling Near-Expiry Stock
Based on the developer's demonstration, the AI agent analyzes current sales data and proposes several handling paths:
- Inter-store transfers: Move near-expiry stock to higher-volume branch locations, prioritizing consumption where turnover is fastest.
- Return for supplier credit: Based on supplier policies, return certain batches in exchange for credit.
- Safety-gated discount promotions: Apply discounts to specific batches — but with a hard constraint: discounts are only recommended when safety has been confirmed, preventing inventory clearance from compromising medication safety.
- Halt reordering: Stop replenishment orders for certain medications entirely, cutting off the source of expiry buildup.

What makes this decision logic elegant is that it folds three competing objectives — financial loss control, operational efficiency, and medication safety — into a single judgment framework. The "discount only when safe" constraint in particular illustrates how non-negotiable business rules are in vertical-domain automation. AI recommendations must be bounded by hard industry compliance requirements.

Human-in-the-Loop: AI Doesn't Make the Final Call
The most important design decision in this system is this: no action is ever executed fully automatically. Every recommendation the AI generates requires pharmacist approval before anything actually happens.
The developer emphasized this point explicitly in the demo — "human-in-the-loop" is a required step in this workflow, not an optional one. The reasoning is clear: pharmaceutical management carries compliance and safety responsibilities. AI can efficiently handle data analysis and solution generation, but the final decision must rest with a qualified pharmacist. This sidesteps the liability and risk issues that full automation would introduce, while still capturing the efficiency gains AI provides.
This "AI recommendation + human approval" pattern actually offers a reusable automation paradigm for many highly regulated, high-stakes industries: let AI handle the tedious analytical work, and return decision-making authority — along with accountability — to the people who are qualified to bear it.
"Human-in-the-loop" is an important design paradigm in machine learning and automation systems, referring to the practice of incorporating human judgment at critical points in an automated workflow rather than letting the system run fully autonomously. Its core distinction from full automation lies in accountability: when system outputs involve high-risk decisions, human involvement both compensates for AI limitations (such as hallucinations or data anomalies) and satisfies legal and compliance requirements. In healthcare specifically, pharmaceutical regulations in many countries explicitly require that prescription dispensing, inventory changes, and similar operations be performed under the responsibility of a licensed pharmacist — making human-in-the-loop not just a design preference but a hard compliance requirement. As AI capabilities improve, lower-risk steps can gradually shift toward full automation, but for decisions that directly affect patient safety, retaining a human approval node remains the most robust engineering choice at this stage.
Closing Thoughts: A Reference Model for Vertical-Domain Automation
This n8n-based drug expiry automation solution, while demonstrated with limited detail, clearly sketches a practical workflow skeleton: scheduled trigger → multi-source data collection → Code Node aggregation → AI decision-making → human approval. It showcases the real-world potential of combining low-code automation platforms with LLMs, and it reminds us that in high-stakes domains like healthcare and finance, keeping a human approval step isn't a technical compromise — it's a necessary design choice.
For teams looking to introduce AI automation into their own operations, this workflow's structure is worth emulating: start with automation to solve data aggregation and initial analysis, use AI to generate structured recommendations, and let domain experts serve as the final quality gate.
Related articles

@ai-sdk/workflow-harness 1.0.146 Release Notes
@ai-sdk/workflow-harness 1.0.146 is a patch-level update that syncs the @ai-sdk/harness dependency to 1.0.146. Auto-published via GitHub Actions with a verified signature.

Googlebook In-Depth Review: Can Google's New Laptop Replace the Chromebook?
In-depth Googlebook review: Google Book OS on Android 17, Pixel ecosystem integration, Gemini semantic search, and how it stacks up against Chromebook and MacBook Air on price and apps.

Python in Practice: Build an MCP Server from Scratch to Give LLMs Real-Time Web Data
Step-by-step guide to building an MCP server with Python and FastMCP, connecting it to Claude Code and Claude Desktop, and enabling LLMs to answer questions with live web data.