Preheat the oven to 375 degrees F.: The kitchen should smell neutral and you should be able to work without rushing, because an oven at 375 degrees F provides steady, even heat that cooks the dish through while browning the top. You might hear the oven click as it reaches temperature and feel steady warmth when you open the door. A common mistake is preheating too briefly, which leads to uneven cooking and longer bake times.
Thinly slice the onions. Heat the 4 tablespoons of butter in a skillet over medium heat. Add in the onions with a big pinch of salt and pepper. Stir. Cook for 10 minutes, stirring occasionally, until the onions soften. Reduce the heat to medium-low, stir in the thyme and cook for another 30 to 40 minutes, stirring often, until the onions are golden and caramelly in color. If at any time the onions start to burn, turn down the heat or add a splash of water to the pan. If the onions are not cooking, you need to raise the temperature a bit.: When you start, the sizzle will be bright and lively as the butter meets the sliced onions . After about 10 minutes the sound settles into gentle, intermittent pops as the moisture releases. The smell shifts from sharp to sweet, and the color of the onions moves from translucent to rich gold. Lowering the heat after initial softening lets sugars develop without burning, producing jammy, deeply flavored strands. A frequent error is keeping the pan too hot, which creates bitter, charred bits instead of even caramelization, so be patient and adjust the heat as needed.
Use a mandoline or sharp knife to thinly slice the potatoes - you want them to be very thin, uniform in size and 1/8 inch thick or thinner. If you are slicing them ahead of time or with a while to go before assembling, place them in a bowl of ice water so they don’t brown.: As you slice, the stack of pale rounds will feel cool and slightly waxy; uniform thickness ensures they cook at the same pace and gives silky layers in the final bake. The ice water keeps them bright and prevents oxidation if there is a pause. A common pitfall is uneven slices that leave some pieces undercooked while others collapse, so aim for steady, controlled strokes or use a mandoline guard for safety.
Make the sauce! Heat the other 4 tablespoons of butter in a saucepan until melted. Whisk in the flour to create a roux. Cook for 2 to 3 minutes, stirring, until the roux is golden in color and smells fragrant and nutty. Slowly stream in the milk while whisking the entire time, bringing the sauce together.: When you melt the butter and add flour , the mixture should foam gently and smell toasty as the raw flour aroma fades; that indicates the roux is ready. Streaming in the milk little by little while whisking prevents lumps and yields a silk smooth béchamel. The sauce will thicken and develop a glossy mouthfeel, which tells you it can coat the back of a spoon. A typical mistake is pouring all the milk at once, which creates lumps, so keep whisking steadily and take your time.
Continue to stir the sauce and whisk in a pinch of salt and pepper and the nutmeg. Reduce the heat to low. Slowly stir in 6 ounces (about half) of the gruyere until melted. Taste and season with more salt and pepper if needed.: As the grated gruyere cheese melts into the warm sauce, you will see ribbons of glossy cheese integrating until the béchamel becomes rich and slightly thicker. The scent will shift to hazelnut and warm dairy notes. Reducing heat prevents separation and keeps the texture smooth. A common issue is overheating the sauce after adding cheese, which can cause oily separation, so keep it gentle and stir until fully incorporated.
Layer half of the potatoes in the baking dish. Cover with half of the cheese sauce, then half of the caramelized onions and half of the remaining grated gruyere. Layer the remaining potatoes on top. Top with the remaining cheese sauce, covering all the potatoes. Top with the remaining caramelized onions and the grated gruyere. Top with the bread crumbs.: When assembling, you should feel the cool weight of the first potato layer and then see the sauce spread into the gaps, smoothing into glossy pockets that cradle the onions and cheese . Alternating layers ensures every slice has a balance of textures. Press gently but do not compact, or the casserole will be dense. A common mistake is skimping on sauce, which leaves dry spots; make sure the layers are evenly coated for consistent creaminess.
Bake uncovered for 45 to 60 minutes, or until the potatoes are tender enough to pick up with a fork. If the top gets too brown towards the end, you can tent it with foil. Let cool slightly before serving.: In the oven you will hear a soft bubbling and see the top develop a golden, blistered crust while steam escapes from the edges. The smell becomes rich and savory, and a knife or fork should glide through layers with no resistance. If the top darkens too quickly while the interior is still firm, tent loosely with foil to protect the crumbs. Avoid removing it too soon, because cutting into a piping hot casserole can cause the custard to break and become watery; allow a short rest so it sets for cleaner slices.