Preheat oven to 350 degrees. Lightly grease a 9x13 casserole dish and set aside.
Peel potatoes, rinse, and slice to ⅛" thickness. I recommend using a mandoline slicer if you have one available.
In a saucepan over medium heat, add butter. Once melted, add onions and cook until they begin to brown and turn translucent, about 4-5 minutes. Add garlic and cook an additional 30 seconds. Add flour and stir to coat and create a paste. Cook for one minute.
Whisk in broth and milk. Continue whisking until no lumps remain. Cook sauce for another 3-4 minutes, or until thickened slightly. Remove from heat. Add salt, pepper, and thyme, stirring to incorporate.
In prepared 9x13 pan, arrange ¼ of sliced potatoes in the bottom of the dish, overlapping slightly. Pour ¼ of sauce over potatoes. Repeat once more, adding another layer of potatoes and sauce. Next, add half of shredded gruyere cheese in an even layer. Add two more layers of potatoes and sauce (for a total of four layers each of potato and sauce), and top with remaining shredded cheese.
Cover casserole with foil and bake for 45 minutes. Uncover and bake an additional 15-20 minutes, or until potatoes can easily be pierced through with a fork in the center of the casserole. The thicker your potatoes are sliced, the longer it will take for them to cook through.
Optionally, turn on broiler for 2-3 minutes to lightly brown cheese on top before serving.
I recommend freshly shredding cheese from a block instead of using pre-shredded bagged cheese, which is often coated in an anti-clumping powder that becomes grainy when melted.
Leftover casserole will keep for up to 4 days in a sealed container in the refrigerator.
Casserole can be assembled ahead and stored in the refrigerator overnight before baking. Cover potatoes with plastic wrap, pressing down to keep out as much air as possible. Exposed potatoes may discolor slightly overnight. While oven is preheating, bring casserole to room temperature on the counter top, then bake as directed above.
Casserole can be baked, cooled, and stored in the refrigerator overnight. To reheat entire baked casserole, bake, covered, at 350 degrees for 30 minutes, or until heated through in the middle.