Sub topic 12
Data privacy and compliance constraints
Privacy and compliance rules can remove options before cost or quality comes into play. This chapter shows how to turn those rules into concrete requirements for choosing between a hosted API and a self-hosted open-weight model. It assumes you have read the earlier chapters on hosted APIs and open-weight models, and that you know what a token and a prompt are. It also assumes you can read basic Python.
Why privacy comes first
Every prompt you send to a hosted API leaves your infrastructure. The prompt may contain customer names, medical notes, contracts, or source code. Once you ask whether that is allowed, the answer can rule out whole categories of options.
Check privacy constraints early because they are binary. A model that is cheap and accurate is still unusable if your contract or regulator forbids sending it the data. Treat privacy as a filter that you apply before you compare cost and quality.
This chapter is not legal advice. Your legal, security, or compliance team decides what is permitted. Your job is to bring them precise questions and to build the application so that their answers can be enforced in code.
Classify your data first
Start with what the application actually sends to the model. List every field that can end up in a prompt, including text users paste in, retrieved documents, and tool outputs. Then label each one.
| Class | Examples | Typical consequence |
|---|---|---|
| Public | Published docs, marketing copy | Any option is usually fine |
| Internal | Meeting notes, internal wikis | Needs a vendor agreement and access controls |
| Personal data | Names, emails, account IDs | Privacy law applies, such as GDPR in the EU |
| Regulated | Health records, payment card data | Sector rules apply, such as HIPAA or PCI DSS |
| Secret | Credentials, unreleased source code | Often self-host only, or do not send at all |
Look at the highest class that can appear in any single request. One field of personal data in a prompt makes the whole request personal data.
What to ask a hosted provider
A hosted API is a processor of your data. These are the questions to put to a vendor, and the answers are usually in its terms, its data processing addendum, and its security documentation.
- Training use. Does the provider train models on your prompts and outputs? Many business plans say no by default, but consumer products often differ. Confirm the terms for the specific plan you use.
- Retention. How long are prompts and outputs stored, for example for abuse monitoring? Is a zero-retention option offered, and does it apply to your account type?
- Data residency. In which region is data processed and stored? Some rules or contracts require data to stay in a country or region.
- Subprocessors. Which other companies touch the data, such as cloud hosts? Are you notified when the list changes?
- Agreements. Will the provider sign a data processing agreement (DPA)? For US health data, will it sign a business associate agreement (BAA) under HIPAA?
- Certifications and audits. Does it hold reports such as SOC 2 or ISO 27001? These show controls exist. They do not by themselves make your use compliant.
- Access and logging. Who at the vendor can read your data, and under what conditions? Are your own request logs stored somewhere you must also protect?
Large cloud platforms often resell models inside your own cloud account. That can change the answers on residency, networking, and agreements, because the contract is with your cloud provider. Compare it as a separate option from the model maker's own API.
What changes when you self-host
With an open-weight model on your own hardware, prompts never leave your network. That removes the vendor from the data path, and with it the questions about training use, vendor retention, and subprocessors.
You do not escape the obligations, though. You take them on yourself.
- You secure the servers, the network, and access to the inference endpoint.
- You decide what the serving software logs. Many servers log prompts at debug levels, and those logs become a new store of sensitive data.
- You handle deletion requests, retention schedules, and audit evidence for your own systems.
- If you rent GPUs from a cloud, that provider is still a subprocessor for the machine, even though it never sees your prompts through an API.
Self-hosting changes who is responsible. It does not remove responsibility. A small team with no security staff can end up less protected than a well-audited vendor.
The model license also matters. Some licenses restrict certain uses or add conditions, as covered in the licensing chapter. Check it alongside the privacy review, because both can block a deployment.
A decision flow
The flow below turns the questions into a first-pass filter. The result is a shortlist, not a final choice.
Enforce the policy in code
A written policy is easy to bypass by accident. A developer adds a new feature that sends a customer record to the wrong endpoint, and nobody notices. Make the policy a check in the code path.
The example below picks an endpoint from the data class and refuses to proceed when no endpoint is allowed. It uses the same environment-variable approach as the portability chapter.
import os
# Which endpoint may receive each data class.
ALLOWED = {
"public": "hosted",
"internal": "hosted",
"personal": "local",
"regulated": "local",
"secret": None, # never sent to any model
}
ENDPOINTS = {
"hosted": os.environ.get("HOSTED_BASE_URL", "https://api.example.com/v1"),
"local": os.environ.get("LOCAL_BASE_URL", "http://localhost:11434/v1"),
}
def endpoint_for(data_class: str) -> str:
if data_class not in ALLOWED:
raise ValueError(f"unknown data class: {data_class!r}")
target = ALLOWED[data_class]
if target is None:
raise PermissionError(f"data class {data_class!r} may not be sent to a model")
return ENDPOINTS[target]
print(endpoint_for("public"))
print(endpoint_for("personal"))
try:
endpoint_for("secret")
except PermissionError as err:
print("blocked:", err)The table fails closed. An unknown class raises an error instead of defaulting to the hosted endpoint. Whoever tags a request with its data class still has to get that tag right, so the check complements a data review and does not replace one.
Redaction and minimization
Sometimes you can keep a hosted API by sending less. Data minimization means including only the fields the model needs. If the task is to summarize a support ticket, the customer's phone number is probably not needed.
Redaction replaces sensitive values with placeholders before the request, then restores them afterward. Pattern-based redaction catches well-formed values such as emails but misses names, addresses, and free-text details. Treat it as one layer of defense, and never as proof that a prompt is clean.
Common mistakes
- Reviewing only the main prompt. Retrieved documents, tool results, and conversation history also reach the model.
- Trusting plan names. Training and retention terms often differ between consumer, team, and enterprise plans of the same vendor.
- Logging prompts everywhere. Your own application logs, tracing tools, and error trackers can copy sensitive prompts to systems that were never reviewed.
- Forgetting evaluation data. Your test set from the evaluation chapter may contain real customer text. Handle it with the same rules, or build it from synthetic or scrubbed examples.
- Treating the decision as permanent. Terms, regions, and laws change. Record the review date and the assumptions, so the decision can be revisited.
How this feeds the next decision
Privacy and compliance produce hard requirements, such as "personal data must stay in the EU" or "no vendor retention." Carry those forward as pass or fail criteria. The hosted-versus-self-hosted chapter weighs them against cost, latency, and operating effort. A requirement that only one option meets settles the question, and you can skip the cost comparison.
Exercise
Your app summarizes customer support tickets that contain names and account numbers. A hosted provider offers a DPA, EU-region processing, and a zero-retention option, and its business plan states that it does not train on your data. Your legal team has not yet approved the vendor. List two things you should do before sending production tickets to it, and say what the data class changes about your options.
Show answer
First, get the legal team's approval and confirm in writing that the zero-retention and no-training terms apply to your specific plan and account, along with the EU region and the subprocessor list. Second, review your own logging, tracing, and evaluation data, so real tickets are not copied to unreviewed systems. Tickets with names and account numbers are personal data, so the hosted API is only allowed if the agreements, residency, and retention are acceptable. If any of those fails, the fallback is to self-host an open-weight model, where the prompts never leave your network but you take on the security and logging duties yourself.