There are two very important rules in tech trading:
1) a monopoly tech costs twice as much to buy. Where "monopoly" is defined as "the buyer knows of only one civ that knows the tech".
2) as more civs (that the buyer has contact with) knows a tech, the price to buy or to research the tech goes down.
These rules apply to both the players and to the AI.
In your example, after you sold the tech to the first civ, the tech will de-value by more than 50% when you sell to the second one. That's 50% from losing its monopoly status (rule 1), plus a little more from having a second civ knowing that tech (rule 2).
if I trade with B before trade A, the value is higher.
There may be a couple of explaination for this.
First, [edit: as vmxa pointed out

x-post] B may be researching the tech. That will lower their purchase price by however many beakers that they have already invested into the tech. If you sell to B first, then you're selling it at monopoly price. The doubling in price more than covers any researched discount that B gets. But if you sell to A first, then by the time you get to B, you're selling it at normal 3rd civ price, and B doesn't have to pay full 3rd civ price buy the tech.
A second explaination is as ainwood said, the AI may not have enough money to pay for it. You trade adviser will tell you if this is the case. If 30gpt is "okay" and 31gpt is "close", then the AI still has more money. But if 31gpt is "impossible", then it means the AI has no more money to offer.