AWS Builder Center
How We Built MedVerify: A Serverless Multi-Agent AI System on AWS to Detect Counterfeit Medicines

How We Built MedVerify: A Serverless Multi-Agent AI System on AWS to Detect Counterfeit Medicines

Learn how MedVerify combines AWS Amplify, Lambda Web Adapter, and an autonomous multi-agent hierarchy to inspect packaging anomalies, query CDSCO recall data, and protect patients from fake medicines.

MedVerify: Hunting Counterfeit Medicine

Built by Team Error Masters — because the worst errors are the ones inside a pill bottle.



The Problem

Your medicines aren't working after three days. You look closer at the strip: the typography looks soft, the foil feels off, the batch number sits slightly crooked.
The WHO estimates 1 in 10 medical products in developing economies are substandard or falsified. In India, the CDSCO publishes monthly alerts on spurious batches — buried in PDFs no patient or pharmacist will ever open.
MedVerify fixes the gap: a 100% serverless, multi-agent AI platform on AWS, powered by Google Gemini and Groq, that turns a photo of a blister pack into a regulatory-grade verdict in seconds.



Architecture at a Glance

Service
Job
AWS Amplify Hosting
React 19 SPA, global CloudFront distribution
API Gateway (HTTP v2)
Low-latency ingress, CORS
Lambda + Web Adapter
FastAPI backend, Python 3.12, zero container overhead
DynamoDB
5 tables — batches, reports, sessions, manufacturers
S3
Blister-pack photos, CDSCO gazettes, RAG documents
Bedrock Knowledge Base
Vector search over regulatory monographs



A Three-Tier Agent Hierarchy

One model can't efficiently handle both a sub-second batch lookup and a 20-step forensic investigation — so queries get routed by complexity:
Query type
Routed to
Speed
Batch lookup / exact match
Skill Agent
< 1 second
Packaging photo
Reactive Agent
Multimodal OCR + cross-check
Adverse reaction / ambiguous case
Deep Agent
Multi-step forensic plan
The Orchestrator (Groq  llama-3.3-70b-versatile ) classifies every query first. The Skill Agent (Strands framework,  SKILL.md -driven) fires parallel DynamoDB queries for violation ratings and NSQ flags. The Reactive Agent hands photos to Gemini 2.5 Flash to extract batch codes and holographic markers.
For high-risk cases, the Deep Agent uses LangChain's  TodoListMiddleware  to build an investigation plan rather than guessing:
  1. Decompose symptoms into tasks
  1. Query DynamoDB for recall notices
  1. Query Bedrock KB for regulatory advisories
  1. Synthesize a harm-reduction verdict
Users can open any case and read exactly why the agent reached its conclusion.



Follow One Query Through the Swarm

(illustrative example)
Here's what actually happens behind the scenes when a worried patient uploads a photo late at night:
11:02:04 PM — Photo lands in S3. The Orchestrator reads the request: an image, plus the text "my fever isn't going down after these." It's not a clean batch lookup — this needs vision and clinical judgement. Routed to the Reactive Agent, with a flag to escalate if anything looks off.
11:02:05 PM — Reactive Agent calls Gemini 2.5 Flash on the image. Extracts: brand name, batch code, manufacturer, and a mismatch — the holographic marker's angle doesn't match the manufacturer's known spec.
11:02:06 PM — Mismatch detected, auto-escalated to the Deep Agent.
11:02:07–11:02:11 PM — Deep Agent builds its task list: checks the batch code against DynamoDB recall records (clean — no official recall yet, but that's not proof of safety), queries Bedrock KB for similar holographic-marker complaints in the region (finds two prior sanitized case reports, promoted from earlier verified feedback), cross-references the manufacturer's NSQ history (one prior flag, six months ago).
11:02:12 PM — Verdict returned: "No official recall exists for this batch. However, packaging anomalies consistent with two prior verified reports were detected, and this manufacturer has one prior NSQ flag. Recommend not administering further doses and consulting a pharmacist or physician."
Total time: 8 seconds. No hotline, no PDF, no guessing.



FastAPI on Lambda, Zero AWS Code

We chose AWS Lambda Web Adapter over Mangum-style ASGI wrappers.  main.py  stays standard FastAPI — identical code runs locally, in Docker, or on Lambda. Setup is two env vars and one layer:
Layers: arn:aws:lambda:us-east-1:753240598075:layer:LambdaAdapterLayerX86:28
AWS_LAMBDA_EXEC_WRAPPER = /opt/bootstrap
PORT = 8080
#!/bin/sh
exec python3 -m uvicorn src.main:app --host 0.0.0.0 --port "${PORT:-8080}"
The adapter proxies API Gateway requests to  localhost:8080  inside the Lambda microVM. The framework never knows it's serverless — no vendor lock-in, no rewrite.



Learning Without Hallucinating

Memory is gated by human verification, not self-generated: when a licensed pharmacist or patient marks a case helpful, a background job strips PII, writes a sanitized case monograph, and lets Bedrock KB vectorize it into permanent retrieval memory.
def promote_feedback_sessions_to_kb(limit: int = 20):
eligible = sessions_table.scan(
FilterExpression=Attr("feedback.helpful").eq(True) & Attr("promoted_to_kb").ne(True)
)["Items"]
for session in eligible:
sanitized_narrative = sanitize_and_format(session)
s3.put_object(
Bucket=settings.S3_KB_DOCUMENTS_BUCKET,
Key=f"case-history/{session['session_id']}.txt",
Body=sanitized_narrative
)
Every confirmed counterfeit becomes context for the next investigation — which is exactly how the query above found two prior matching reports.



Try It

Building multi-agent workflows or wrestling with Lambda Web Adapter? Drop a comment below.
— Team Error Masters
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article