Federated Learning & Privacy-Preserving AI
Privacy-preserving AI techniques for engineers: federated learning, differential privacy, PII redaction, secure processing and the practical patterns for handling sensitive data with LLMs.
On this page
Handling sensitive data with AI raises real privacy obligations, and there is a spectrum of techniques from everyday practices to specialised methods. This tutorial covers both: the practical PII discipline every application team needs, and the advanced techniques — federated learning, differential privacy — for the specific situations that call for them.
Key Takeaways
- For most teams, privacy means practical discipline: redact PII, keep it out of prompts and logs, use compliant models and regions, minimise retention.
- Federated learning trains across distributed data without centralising it.
- Differential privacy gives a provable guarantee that individuals cannot be identified from output.
- The advanced techniques are for specific situations — not everyday application patterns.
Start with the practical
Before the specialised techniques, the everyday discipline that covers most application needs.
PII redaction
Remove or tokenise personal data before it reaches the model where the use case allows:
public String process(String input) {
// Replace personal data with tokens before it enters the prompt.
RedactionResult redacted = redactor.redact(input); // emails, cards, names, IDs
String response = chatClient.prompt().user(redacted.text()).call().content();
// Restore tokens in the response if needed, mapping back locally.
return redacted.restore(response);
}Combine a pattern-based pass with a proper PII-detection library for regulated content. See security in AI-powered Spring applications.
Keep PII out of prompts and logs
Use compliant models and regions
For regulated data, use local models so data never leaves your infrastructure, or in-region cloud models that meet your residency requirements. Match the processing location to the data's legal constraints.
Minimise retention
Keep sensitive data — including prompts, responses and any derived memory — only as long as needed, with a clear retention policy and deletion on request. An agent memory system that silently accumulates personal data forever is a privacy liability.
Federated learning
For the specific situation of training a model across sensitive data distributed on many devices or servers — without centralising it:
Each participant trains on its own data; only the model updates — not the data — are shared and aggregated. The classic use is learning from data on users' phones (predictive text, on-device features) while the raw data never leaves the device. It is a specialised technique, most relevant when you both train models and cannot centralise the training data.
Differential privacy
A mathematical guarantee: add calibrated noise so that the presence or absence of any single individual cannot be determined from the output, while aggregate patterns survive. It is used when you need to publish or derive insights from sensitive data with a provable privacy bound — analytics on sensitive datasets, training data protection.
The key idea for an engineer: differential privacy trades a measurable amount of accuracy for a measurable privacy guarantee, and the trade is tunable. It is powerful and specialised — you reach for it when you need a provable guarantee, not merely good practice.
Secure multi-party computation and confidential computing
Two more specialised tools worth knowing exist:
- Secure multi-party computation lets parties jointly compute over their combined data without any party seeing the others' data — for rare cases where multiple organisations must compute together on data none will share.
- Confidential computing processes data in hardware-encrypted enclaves, so even the infrastructure operator cannot see it — for the highest-sensitivity workloads.
Both are niche; most teams will never need them, but knowing they exist helps you recognise the situation that calls for one.
Matching technique to need
| Need | Technique |
|---|---|
| Everyday sensitive data in an LLM app | PII redaction, compliant models, minimise retention |
| Data cannot leave the device | Local models, or federated learning if training |
| Provable guarantee no individual is identifiable | Differential privacy |
| Multiple orgs compute without sharing data | Secure multi-party computation |
| Infrastructure operator must not see data | Confidential computing |
Next
Frequently Asked Questions
What is federated learning?
What is differential privacy?
What are the practical privacy techniques for LLM applications?
How do I handle PII when using an LLM?
Related tutorials
- AI Agents for the EnterpriseDeploy AI agents in the enterprise: integrating with SAP, Salesforce and ServiceNow, SSO and identity, audit trails, approval workflows and the governance enterprise agents require.
- AI Regulations & ComplianceWhat developers need to know about AI regulation: the EU AI Act risk tiers, GDPR for AI, ISO 42001 and the NIST AI RMF — and the engineering practices that keep AI systems compliant.
- Small Language Models (SLMs)When smaller models win: SLMs like Phi and Gemma, on-device and edge AI, model routing between small and large models, and the cost and latency case for not always reaching for the biggest model.
- Open Source vs Proprietary AI StrategyChoose an AI model strategy: open-weight vs proprietary hosted models, total cost of ownership, vendor lock-in risk, hybrid approaches and migration paths — a decision framework for Java teams.