Sometimes I lose blood vials and sometimes I don't
Blood Vials are not an unlimited resource. You can carry up to 20 at the same time and any vials you then pick up are sent into your storage, which you can access in the Hunter's Dream inside the house on the right side. The maximum amount of vials you can have in storage are 600. When you die, your blood vials automatically get filled up back to 20 with the remaining ones in your storage. If those are all used up, you need to pick up new ones by defeating enemies, finding them in the environment, or buying them from the merchant in the Hunter's Dream. Enemies in the earlier areas of Bloodborne drop blood vials quite frequently, so they can be farmed fairly quickly there.
See also Where is the best place to farm Blood Vials in Bloodborne?