How to decide when is a3/h3 needed? a3/h3 are frequently used to prevent the bishops from pinning the knights.

I always have a hard time deciding whether to play them or not. Playing them loses moves and tempo while not playing can also sometimes become annoying (for example, if the knight is pinned after the bishop has already moved OR worse, if the bishop cannot come back to remove the pin).

Can the experts here help the rest of us with some thumb rules and/or examples?
Let's stick to one color, white here, and everything we'll say will generally hold true for black as well. 

Rule of thumb: On the one hand, you generally don't want to move your king-side pawns (such as h3) when you've castled short, unless you have to, and we will expand on what "have to" entails here. The immediate exception to that is when you decide to fianchetto your king-side bishop, e.g., in the Catalan where then you play g3. On the other hand, your queen-side pawns (assuming you've castled short) are to be used more actively to either gain space or prevent an spatial expansion by your opponent.  (the rule is naturally to be taken with caution, chess rarely lends itself to these kinds of generalizations)

The more common cases are when a bishop is pinning your knight on g4 and h3 is an immediate way to try and challenge the bishop, asking it where it wants to go. But there are cases where g4 is not being exploited for a pin but rather as an intermediate square to re-route a knight (to e5 for instance). In either case, if the pin is problematic to deal with, which may be so if you've already made a commitment with your light square bishop and it cannot return to e2, or if the knight re-routing would be an improvement for your  opponent, then you really want to play h3 in a prophylactic way to prevent all of that. Here's an example in the Benko gambit:

 [Title "Benko gambit declined - preventive example"]
 [fen ""]

 1.d4 Nf6 2.c4 c5 3.d5 b5 4.Nf3 g6 5.Qc2 Bg7 6.e4 d6 7.h3 {Stopping any future knight re-routes to e5 via g4, and specially securing e3 for the c1 bishop. Bg4 is altogether less of a concern here in comparison}


And below you find two prophylactic examples of a3 and h3 where they are purely strategic and intentional:

 [title "a) Nimzo-Indian b)London system - (deliberate examples)"]
 [fen ""]

 1.d4 Nf6 (1...d5 2.Nf3 Nf6 3.Bf4 e6 4.e3 Be7 5.Nbd2 O-O 6.h3 {this is largely to secure the h2 square so that we can preserve our dark square bishop for instance against Nh5.}) 2.c4 e6 3.Nc3 Bb4 4.Qc2 O-O 5.a3 {white by choice, not forced, plays a3 to resolve the pressure on c3 in order to finish development more deliberately, with choices such as Bg5 becoming easier to make} Bxc3+ 6.Qxc3 b6 7.Bg5     


Then there are reactive cases where tactically one may not have a choice but to challenge a piece on g4 or b4 respectively, in which case although there's no doubt your opponent would be forcing your hand into playing h3 (a3), but you will have to include that move and accept a small set-back. Extreme cases are when a mate is threatened, e.g., a queen on h5 and knight on g4.

Why moving the castled-side pawns would generally be a set-back?

Main reason for that is two-fold: First, as obvious as it may sound, you'd be permanently committing to a pawn structure, second and tightly related to the first, you'd be creating targets for your opponent. The latter may be, to name a few, in the form of weakened dark squares, for instance if there's a dark-square battery on the c7-h2 diagonal that you'd want to preferably deal with by playing g3, but having previously played h3 may make that difficult and g3 would expose h3 and g3 itself to potential sacrifices. Another type of target would be, a pawn push on the king-side by your opponent, challenging h3 with g4 attempting to undermine your king-side pawns and open a file towards your king. 

The takeaway here can be generalized: pawn moves in chess are always a commitment, the later you have to commit to them, the more pawn structures you can opt for, and conversely, the sooner you commit to one, be it willingly or forced by your opponent, the earlier you create targets for your opponent. Targets are not always weak pieces/pawns (such as back pawns, or isolated ones), but they can also be strategic targets that can be exploited in your position, prime example being weakened-black/white-squares, or more abstractly, your pawn-breaks in the position can become limited, and the few remaining ones can more easily be prevented/delayed by your opponent, which ultimately means you will have a hard time undermining your opponent's structure and creating active play. This is closely related to space control in chess, which you can read more about here. With all of this in mind, now you can see for example why the English opening is generally is hard to face: it is extremely versatile in pawn structures it can lead to, consequently, the opponent has a harder time to resolve the dilemma of which plan to opt for! Instead, e4 and d4 openings are more committal by definition (as we start by moving a central pawn already), so we can more reliably establish systems against them

Now let's take an extreme example where our rule of thumb is being perfectly illustrated:

 [title "Michael Adams vs Garry Kasparov, Linares 2005"]
 [fen "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"]

 1.e4 c5 2.Nf3 d6 3.d4 cxd4 4.Nxd4 Nf6 5.Nc3 a6 6.Be3 e6 7.Be2 Qc7 8.Qd2 b5 9.a3 Bb7 10.f3 Nc6 11.O-O-O b4 12.axb4 Nxb4 13.g4 Be7 14.g5 Nd7 15.h4 Nc5 16.Kb1 Rb8 17.h5 O-O 18.g6 Bf6 19.Rdg1 Ba8 20.Bg5 Be5 21.gxh7+ Kxh7 22.Nb3 Nxc2 23.Nxc5 Na3+ 24.Ka2 Qxc5 25.Na4 Nc2 26.Kb1 Qa3 0-1


Notice how Kasparov pushes with a6-b5-b4 on the queen-side in order to launch his attack on white's long castled king and yet never moves any of his king-side pawns until the end as to avoid giving white any entry-points or targets. Notice how Adams had to play earlier a3 in order to prevent b4 as it would force his c3 knight to be mis-placed in turn weakening the e4 support. And having committed to a3 is precisely the target Kasparov used with b5-b4 to open the b-file, which he then exploited beautifully. This is a cherry picked example, but it's very appropriate here because all of our previous points culminate in one game! More generally, Sicilian players are accustomed to this type of pawn structures (i.e., knowing not to commit their king-side pawns and just push as fast as possible on the queen-side).