Skip to content
by Unity Nodes
NEAR DevHub

Privacy by Design: Every AI App Says It Protects Your Data. Here's the Code

about 8:04 long

Nothing is loaded from YouTube until you press play, and then without tracking cookies.

About this video

Every AI app claims it protects your data. The claim lives or dies in the architecture, so this video is the code.

I walk through the Swift running on-device in my sample app: the lanes a request can be routed to, how the router picks one, why an audit record gets written before the route commits, and how the whole thing is logged so you can verify it later.

Four lanes, ranked. Local, meaning it never leaves the phone. A PHI lane for protected health information. A general private lane. An everyday lane. Every feature in the app inherits routing for free, which also means that if a cloud provider goes down or you blow through a quota, forcing the local path is always available. The most private option is the one that requires nobody else.

Two details I'd argue matter more than the routing itself.

The audit record is written before the route turns, not after. If something goes wrong you need to know where the data went, and a log written after the fact can't tell you that. Which raises the obvious problem: an audit layer that records where private data went is itself a target. So it has to be encrypted. A log that leaks is worse than no log.

The chain runs as a blockchain on the phone. No coins, no network, one honest job: recording what left the device, what stayed, and which lane it took. Then verify it. Once you have that, it stops being plumbing and becomes a feature. Users can open the chain, see the record count, and check for themselves where their information went.

And the ratchet. Once a conversation enters a more private lane it stays there. You can upgrade to something more secure, never downgrade. Restore an old conversation months later and it inherits its lane. In the demo, asking about my dad's medication amounts puts that thread in the protected health lane permanently. My own numbers might stay local and never leave the phone at all.

All of this runs on private inference through NEAR AI, using the TEEs I covered in an earlier video. If you're not building for health you may not need the PHI lane, and a TEE lane on its own could be enough. The pattern is what transfers.

Chapters 0:00 Where this fits in the series 0:31 The sample app and where triage begins 0:39 Every app claims it protects your data 1:07 The Swift code and the four lanes 1:38 Plan and commit: how the router resolves 2:17 Why you can always force the local path 2:56 The audit record is written before the route turns 3:35 The audit layer is itself an attack surface 4:11 The audit chain running on the phone 4:54 The ratchet: lanes upgrade, never downgrade 5:46 A real conversation locked to the health lane 6:19 Turning the audit trail into a user-facing feature 6:50 Private inference through NEAR AI 7:13 Adapting this if you're not building for health 7:38 Local is always the safest bet

Earlier in this series: triage and routing, plus the TEE explainer.

Subscribe and follow along. More implementation videos coming.

#BuildInPublic #SwiftUI #iOSDev #PrivacyByDesign #AIEngineering #NEARAI #HealthTech

Transcript

1,521 words

Captions as YouTube publishes them, usually machine-generated, so product and people names can be misheard.

  1. 0:01

    Welcome to the next episode in my series on building with AI. We're focusing on privacy and how to build with private inference in different ways. If you haven't seen the first few episodes around triage and routing, I would suggest that you check those out. It's a great introduction to understanding the problem with having to take in different information and process it in different ways when you're dealing with AI. Today I'm going to walk you through actual

  2. 0:24

    routing decisions and what the code looks like in trying to make these decisions and both how simple it is, but what actually goes into the thought process of having to actually do that and before you even hand it off to the AI. Let's get started. This sample project, we're building an app that's going to actually live on your phone. So all the triage is going to begin from there. What you see now is that every AI app makes the same claim that they care

  3. 0:48

    about your data and they protect your data. But how do they actually really do that and are they actually doing that? And it really comes down to the architecture and decision behind the code. So let's walk through a little bit of what that looks like in order to do this in a proper way to triage this type of private information. All right, let's look at the code. So, what you have here

  4. 1:07

    is some Swift code. And this is going to run on an iOS based device. And I want to just start with the really simple concept of what are the different lanes that we're going to actually route to. So, for this example app, we are doing a couple different things. One of them is actually local, so we're not actually leaving the device at all. We have a PHI, which is a private health information. Then, we have a just generalized private lane. And then we have what's called an everyday lane.

  5. 1:38

    Really simple. And we're kind of ranking things accordingly from there. Now that we know the different lanes that we're going to have, now we need to figure out how are we going to get there? How do we resolve to those different lanes? So in this bunch of code, we have two different setups here. So we're going to have first what we would call a plan and then we're going to have what we're calling a commit to that plan. And the

  6. 2:01

    purpose here is to actually run triage and determine which level of floor or uh lane we're going to go on. Every feature that's built into this app is going to have that and it inherits it for free. So basically the reason why we want to do this is that if for some reason there's an issue with any of the cloudbased stuff or there's an issue with the cloud quota um we can always force the local path. We can always go up. The most private is going to be

  7. 2:32

    local. So we can do that first. But here we need to make sure that we are running that triage determining the floor. We are making sure that like what is actually the bottom floor for whatever we're going to do now, whatever is going to be asked of the AI system and then go from there.

  8. 2:56

    You'll notice that an audit record is actually written before that route is turned. And the reason why we do that is because we want to make sure that if something were to be incorrect for any reason, we need to be able to go back and look why and know where all the stuff went. So we don't actually want to do the routing until that. So we need to make sure that we go from there. Once we have then determined

  9. 3:27

    which path we're going to go, then we can go ahead and do that. One thing I think is important to recognize when you're doing something like this and we're having that audit of where data is going to be private if we are exposing things through the audit layer. So the audit layer has to be encrypted. We have to ensure that at the bare minimum data is not being leaked through that audit

  10. 3:51

    chain. So that autochain is great. If we something goes wrong, but if it becomes a point of attack, then that's not helpful either. So we need to make sure that we're doing that safely as well. All this goes into the same concept of routing. It's just one part of that and making sure that we're logging correctly, safely, and privately. Let's talk about the actual audit chain here and how we actually are logging things.

  11. 4:15

    We need to be able to know where the actual logs are and that they are true and that they are not being manipulated in any way. Essentially, what we're doing here in this code, if we scroll down to this section here, we're actually auditing. We're inserting a record into essentially what would be a blockchain that is running on the phone.

  12. 4:37

    Uh there's no coins on here or anything like that. And it's just one honest job of saying you know what content actually left my phone which content has stayed local and which lane did it actually go for that level of trust and privacy. Now we talk about architecture of this app in the design process here of how do we make sure we're we're maintaining privacy all the way through. So a part

  13. 5:03

    of that comes from the conversation itself and we have to have a ratchet in there that is actually saying you know if something is going to a particular lane that we are going to always stay in that lane again we can upgrade and so we can go to a more secure lane but we cannot go backwards and once something is in that conversation that now has a that is triggering that particular

  14. 5:32

    um more private lane then we need to make sure that we are staying in there. So even if we come back to it later so if we restore that conversation it's an old conversation we're going to go back to the conversation that lane needs to be inherited. So you can see here that this conversation is using protected health lane because asking health related questions. that's actually

  15. 5:54

    having private information, which would be, you know, dad's medication amounts and what they're taking. So, we want to make sure that's all going through that protected health lane. Uh, it would maybe be local if this was the main user. So, this was like, let's say, my stuff, maybe that would actually be local to my phone. We wouldn't have to leave that at all. But all that would then go into the audit trail. We would

  16. 6:19

    then be able to go back and do. And so you can actually build that into your app once you build out an audit trail of your routing that be can become a feature and users might really appreciate being able to see you know like let's verify the chain activity at this 44 records in there you know where do these different go um things like that you know like what's on here you could build out a whole feature set and this is just an example of like what you could start to do and have these logs that verify and show

  17. 6:50

    privacy All this is done by using systems like Near AI that allow you to connect to private inferences that are going to give you that privacy and protection using a TE which we discussed in another video how those work. I think it's worth checking out how that goes. And then from there building in other pieces that you need. If you're not building a health app, then maybe you don't need, you know, that extra maybe private health lane. Maybe just the normal TE by

  18. 7:22

    itself is just enough. It's going to determine on what you're building, but you start to really have that flexibility once you understand what needs to be routed where, and what kind of privacy you want to afford your end user, how you're going to do that, including, if you noticed from the beginning of the video, keeping things local. keeping things local is going to be the safest way of doing that because the information already lives on the device. There's nothing that needs to be sent out and so that's the safest bet.

  19. 7:49

    Uh just from a data security and privacy standpoint, that's always an option. Thanks for watching this episode in the series of building with AI and building with privacy in mind. Uh we'll have more about how to actually implement more of this type of stuff all the way through.

  20. 8:04

    If you found this helpful, please subscribe and follow along because there'll be a lot more videos like this coming. Thanks.