Healthcare · Pharmacy Benefits
Digitizing complex pharmacy contracts with generative AI.
Fortune 10 healthcare enterprise
An end-to-end GenAI pipeline that reads dense pricing contracts — tables, legal language, and billing rules — and turns them into audit-ready structured data, unlocking roughly $64M in annual savings.
Healthcare · Pharmacy Benefits
Challenge
What was the challenge?
Pharmacy pricing contracts are some of the hardest documents in the industry to work with. A single contract can run dozens of pages of legal language, and the numbers that actually drive billing live inside pricing tables that arrive in roughly 20 different layouts: borderless tables, two-column and seven-column variants, transposed tables, and tables that break across multiple pages. Alongside the tables, the business terms that govern payment — brand and generic drug definitions, rebate guarantees, rebate reimbursement, offshore restrictions, 340B provisions, biosimilars, exclusion clauses — are buried in paragraphs of prose that only trained analysts could reliably interpret.
Every one of those elements had to be digitized, standardized into the five canonical pricing structures (term of contract, network guarantees, rebate guarantees, term fees, and additional fees), and audited against the client pricing management database owned by another team. Done by hand, this was slow, expensive, and error-prone — and in pharmacy pricing, an extraction error isn't a typo, it's a billing discrepancy.
Approach
How did we approach it?
We treated the contract as three separate extraction problems — tables, language, and billing logic — and built a dedicated GenAI pipeline for each, tied together by one non-negotiable principle: the model is never trusted blindly. Every LLM output is validated against the source document with deterministic checks, and anything that fails validation is pushed back through a feedback prompt rather than passed downstream.
Where a format was consistent enough for plain code, we deliberately used plain code — deterministic Python handles the well-behaved table formats, and GPT is reserved for the long tail that traditional parsing can't touch. That kept costs down, speed up, and made the system's behavior predictable where predictability was possible.
For hallucination control we layered defenses: few-shot prompts built from curated samples, binary classification passes to confirm each candidate extraction, regex validation proving that every value in the output actually exists in the input, and majority voting across up to three feedback rounds before an answer is accepted.
Delivered
What did we deliver?
- Automated table digitization that converts ~20 different pricing-table formats into one standardized, audit-ready structure covering all five pricing categories.
- Language extraction that finds and classifies critical contract definitions — brand and generic drugs, rebates, rebate reimbursement, offshore restrictions, 340B, biosimilars — plus global and default exclusion clauses pulled from rebate and guarantee language.
- Automatic conversion of contract language into executable billing logic: brand and generic SQL generated directly from the drug definitions, ready to run on the claims adjudication platform.
- Table repair intelligence: tables that OCR split across pages are merged, and tables OCR wrongly fused are split back apart, before any transformation begins.
- An audit workflow that validates every extracted component against the client pricing management database maintained by the pricing team.
- APIs standardized and secured to enterprise-grade consistency, security, and performance standards, including a bridge API for pricing-database access during integration.
Mechanism
How does it work under the hood?
- 01Contracts are OCR'd; headers and footers are stripped; the remaining paragraphs are embedded and stored in MongoDB so every downstream step can retrieve by meaning, not keyword.
- 02Language extraction: for each definition type, the system retrieves the 5–6 most similar paragraphs, builds a prompt from curated sample paragraphs plus the retrieved candidates, and has GPT classify each candidate in or out (a deliberate 0/1 framing that suppresses hallucination). A feedback-loop prompt then re-checks the extraction, repeated up to three times with majority voting.
- 03Table extraction: detected tables are classified as pricing or non-pricing (keyword rules first, GPT-on-structure second), then repaired — split tables merged, fused tables separated — before transformation.
- 04Table transformation: consistent formats go through deterministic Python. Everything else goes through few-shot GPT — the two most similar reference examples are retrieved from a format library, the table is passed as JSON, and the output is regex-validated against the input so no fabricated value can survive. Failures trigger a feedback prompt; oversized tables are split around the context window and recombined in code afterwards.
- 05Billing logic: brand and generic definitions are first matched against a library of previously approved SQL — above a 90% similarity threshold, the approved logic is reused verbatim. New definitions get GPT-generated SQL from few-shot examples, hardened through the same feedback loop.
- 06Every output lands in the audit workflow, where extracted values are reconciled against the client pricing management database before anything reaches production billing.
Outcome
What was the outcome?
The system went from research problem to production capability, contributing to roughly $64M in annual cost savings by removing manual digitization from the contract-to-billing pipeline. The work earned an internal engineering excellence award for its contribution.
Just as important as the savings: the standardized, validated output made downstream auditing dramatically simpler, because every table, definition, and billing rule now arrives in one predictable shape with proof it came from the source document.
Learnings
Key learnings
LLMs earn trust through validation, not confidence — regex-proving every output value exists in the source document is what made GenAI safe for billing data.
Use deterministic code wherever the format allows and save the model for the long tail; the hybrid is cheaper, faster, and more predictable than AI-everywhere.
Feedback-loop prompts with majority voting turn a good extraction rate into a production-grade one — single-pass LLM answers are not enough for regulated data.
Related stories
Healthcare · Specialty Pharmacy
Preventing misdelivered medicines with NLP
An NLP system that extracts delivery addresses from call transcripts at ~95% accuracy and validates them against member records — saving roughly $2M a year and protecting specialty medicine deliveries.
Healthcare · Pharmacy Network
Automating document intake for 700+ pharmacies
A hybrid LLM + machine-learning system that classifies and extracts data from roughly 1.6 million faxed pharmacy forms a month at 94% accuracy, feeding clean structured data straight into order creation.
Work with us
