AI Tools Compared

To use AI coding tools in HIPAA-regulated environments, deploy self-hosted models (Ollama, CodeLlama) that keep PHI-containing code on-premises, or use cloud AI tools with signed Business Associate Agreements (BAAs) and strict input sanitization that strips patient data before sending code for completion. Never paste raw database queries, test fixtures, or configuration files containing PHI into cloud-based AI assistants. For teams that must use cloud tools, implement pre-send filters that detect and redact potential PHI patterns before code leaves your environment.

Understanding the Risk Model

AI coding tools typically send code to external servers for processing. In healthcare contexts, this creates potential PHI exposure risks. The core challenge is ensuring that no patient data, even in code form, leaves your controlled environment without proper safeguards.

Before adopting any AI coding tool in a healthcare project, evaluate whether it offers HIPAA-compliant tiers or self-hosted options. Many major providers now offer enterprise agreements with Business Associate Agreements (BAA) that establish legal accountability for data handling.

Practical Best Practices

1. Sanitize Input Before AI Processing

Never paste actual PHI into AI prompts. Even if the tool claims security, defense in depth matters. Create preprocessing scripts that detect and redact sensitive patterns:

import re

def sanitize_code_for_ai(code: str) -> str:
    """Remove potential PHI patterns before sending to AI tools."""
    # Redact Social Security Numbers
    code = re.sub(r'\b\d{3}-\d{2}-\d{4}\b', '[SSN_REDACTED]', code)
    # Redact email addresses
    code = re.sub(r'\b[\w.-]+@[\w.-]+\.\w+\b', '[EMAIL_REDACTED]', code)
    # Redact phone numbers
    code = re.sub(r'\b\d{3}[-.]?\d{3}[-.]?\d{4}\b', '[PHONE_REDACTED]', code)
    # Redact dates that might represent DOB
    code = re.sub(r'\bDOB[:\s]+\d{1,2}[/-]\d{1,2}[/-]\d{2,4}\b', 'DOB: [DATE_REDACTED]', code, flags=re.IGNORECASE)
    return code

This approach adds a safety layer between your code and external AI services.

2. Use Local or Self-Hosted Models for PHI-Adjacent Code

For code modules that directly handle PHI data structures, consider running local AI models. Tools like Ollama or local instances of code completion models keep all processing on premises:

# Example: Running a local code completion model
ollama serve
ollama run codellama:latest

This eliminates external data transmission entirely for sensitive modules.

3. Implement Prompt Isolation

Separate your AI interactions into distinct contexts:

Use different AI tool configurations or even separate accounts for these contexts to prevent cross-contamination of prompts.

4. Review AI-Generated Code for Compliance

AI tools can introduce security vulnerabilities or non-compliant patterns. Always review generated code for:

# Example: Adding audit logging to AI-generated database access
class HIPAACompliantPatientRepository:
    def __init__(self, db_connection, audit_logger):
        self.db = db_connection
        self.audit = audit_logger

    def get_patient(self, patient_id: str, requesting_user: str) -> Patient:
        # Always log access to PHI
        self.audit.log_access(
            user=requesting_user,
            resource=f"patient:{patient_id}",
            action="read",
            timestamp=datetime.utcnow()
        )
        return self.db.query("SELECT * FROM patients WHERE id = ?", patient_id)

5. Configure IDE Extensions Carefully

When using AI code extensions in your editor:

Most modern AI coding extensions now support local-only modes that don’t transmit code anywhere.

6. Maintain Clear Documentation

Document your AI tool usage policy in your codebase:

# AI Tool Usage Guidelines

## Allowed Tools (Healthcare Projects)
- Local models: Ollama, LM Studio
- Cloud tools: Only with BAA and encryption

## Prohibited
- External AI for PHI-related code
- Pasting real patient identifiers in prompts
- Using AI to generate test data with real PHI

## Required
- Input sanitization for all AI prompts
- Code review for all AI-generated code
- Audit trail for AI-assisted code changes

7. Implement Pre-Commit Checks

Add automated checks to your development workflow:

# .pre-commit-config.yaml example
repos:
  - repo: local
    hooks:
      - id: sanitize-ai-input
        name: Sanitize AI prompts
        entry: python scripts/sanitize_for_ai.py
        language: system
        pass_files: true

This ensures that code leaving your local environment has been screened.

Security Patterns for Healthcare AI Usage

When designing your healthcare codebase architecture, consider these patterns:

The Air Gap Pattern: Keep PHI-processing code in a separate module that never interfaces with AI tools directly. AI assists only the non-PHI surrounding infrastructure.

The Gateway Pattern: Route all AI requests through a sanitization service that strips potential PHI before reaching external services.

The Validation Pattern: Implement automated checks that verify AI-generated code doesn’t contain hardcoded credentials, improper logging, or missing encryption.

Tools with HIPAA BAA Availability

Not all AI coding tools offer Business Associate Agreements. Before deploying any tool in a healthcare setting, confirm BAA status with the vendor:

Tool BAA Available Notes
GitHub Copilot Yes (Enterprise) Requires GitHub Enterprise Cloud
Cursor No (as of 2026) Self-hosted only for HIPAA use
Claude API (Anthropic) Yes Enterprise tier
OpenAI API (ChatGPT) Yes Business/Enterprise tier
Amazon CodeWhisperer Yes AWS HIPAA eligible services
Ollama (local) N/A No data leaves your infrastructure

A BAA does not make a tool automatically compliant — it establishes legal accountability. You still need input sanitization, access controls, and audit logging in place regardless of BAA status.

Implementing HIPAA-Compliant Audit Logs

HIPAA’s Security Rule requires audit controls (45 CFR § 164.312(b)). Any AI-assisted code that accesses PHI must be auditable. This is the pattern that AI tools most often generate incorrectly — they produce functional code without the audit trail:

import logging
import json
from datetime import datetime, timezone
from functools import wraps
from typing import Callable

# Structured audit logger — write to immutable log storage in production
audit_logger = logging.getLogger("hipaa.audit")

def audit_phi_access(action: str):
    """Decorator for functions that read or modify PHI."""
    def decorator(func: Callable):
        @wraps(func)
        def wrapper(*args, **kwargs):
            # Extract user context from request context (adjust for your framework)
            from flask import g  # or request.user for Django, etc.
            user_id = getattr(g, 'user_id', 'unknown')

            audit_event = {
                "timestamp": datetime.now(timezone.utc).isoformat(),
                "user_id": user_id,
                "action": action,
                "function": func.__name__,
                "status": "initiated"
            }

            try:
                result = func(*args, **kwargs)
                audit_event["status"] = "success"
                return result
            except Exception as e:
                audit_event["status"] = "error"
                audit_event["error"] = str(e)
                raise
            finally:
                audit_logger.info(json.dumps(audit_event))
        return wrapper
    return decorator

# Usage — add this to any AI-generated data access function
@audit_phi_access("read_patient_record")
def get_patient(patient_id: str) -> dict:
    return db.query("SELECT * FROM patients WHERE id = %s", patient_id)

When reviewing AI-generated code, check every data access function for this pattern. AI tools consistently generate working queries without audit decorators unless you explicitly prompt for them.

Encryption at Rest: What AI Gets Right and Wrong

AI tools handle encryption at the application layer inconsistently. For HIPAA, encryption at rest is an addressable implementation specification (§ 164.312(a)(2)(iv)).

What Claude and ChatGPT get right: they know to use AES-256 and will generate key management scaffolding when prompted.

What they miss: they rarely implement field-level encryption for specific PHI columns, defaulting to full-disk encryption assumptions. For databases storing a mix of PHI and non-PHI, field-level encryption is more appropriate:

from cryptography.fernet import Fernet
import os

# Load encryption key from secrets manager — never hardcode
KEY = os.environ['PHI_ENCRYPTION_KEY'].encode()
cipher = Fernet(KEY)

def encrypt_phi_field(plaintext: str) -> str:
    """Encrypt a PHI field before writing to database."""
    return cipher.encrypt(plaintext.encode()).decode()

def decrypt_phi_field(ciphertext: str) -> str:
    """Decrypt a PHI field after reading from database."""
    return cipher.decrypt(ciphertext.encode()).decode()

# Use in your model — AI-generated code rarely includes this pattern
class Patient(Base):
    __tablename__ = "patients"
    id = Column(Integer, primary_key=True)
    _ssn_encrypted = Column("ssn", String)

    @property
    def ssn(self) -> str:
        return decrypt_phi_field(self._ssn_encrypted)

    @ssn.setter
    def ssn(self, value: str):
        self._ssn_encrypted = encrypt_phi_field(value)

Prompt AI tools explicitly: “Add field-level encryption for SSN, DOB, and email columns using a key loaded from environment variables.”

FAQ

Can I use GitHub Copilot for healthcare software? Yes, with GitHub Enterprise Cloud (which includes a BAA). Disable Copilot for files in directories containing PHI data models — configure this in .github/copilot settings. Use Copilot freely for infrastructure, test scaffolding, and non-PHI business logic.

Is Cursor HIPAA compliant? As of early 2026, Cursor does not offer a BAA. For strict HIPAA compliance, use Cursor in “local mode” with Ollama as the backend, or use it only for non-PHI code sections with the awareness that code context is sent to Cursor’s servers.

Does using AI to generate test data violate HIPAA? Not if done correctly. Never use real patient data as test data. Use AI tools like Claude to generate synthetic patient datasets that are statistically realistic but entirely fictional — this is actually a HIPAA-safe use case for cloud AI tools, since no real PHI is involved.

What logging format satisfies HIPAA audit requirements? HIPAA requires audit logs to capture who accessed what and when, but does not specify a format. Use structured JSON logs with timestamp, user identity, resource accessed, and action taken. Send these to an append-only log store (CloudWatch Logs with object lock, Splunk, etc.) that prevents modification.

Built by theluckystrike — More at zovo.one