The cost model is like a time limited software license. If there is some new trick to extend the license, the drug companies would likely just charge more up front.
Exactly. The price is set by the willingness to pay (the demand curve), which accounts for the fraction of purchased pills that will actually be consumed.
It seems like this would be true if everything were working perfectly and there was a gradient of purchasing options across price/expiration. As it is consumers really don't have any choice wrt expiration date since as far as I'm aware the option to pay more for longer lasting drugs doesn't really exist.
It doesn't require a gradient. Suppose that, instead of expiry issues, half of all pills were inert duds for whatever manufacturing reason. (You look in the bottle and the duds are black.) This would make the pills half as valuable to consumers, and manufacturers would simply sell twice as many for half the price. It does not require that consumers have access to pills that have a higher or lower dud rate. And yes, it's possible to construct a model where there are frictional losses from duds (increased spending on pill bottles!), but to assert that you'd need to try to write down some numbers.
Pharma companies get the benefit of near-perfect inelasticity in demand, i.e., if you don't get your meds means you die or have extreme pain, you will likely be willing to pay inordinate prices.