Melt the butter in a large Dutch oven over medium heat.
Add the onions, sugar and salt, stirring to coat.
Cook the onions 40 to 45 minutes, stirring every 5 minutes, until they are deep caramel brown and jammy, lowering the heat if they start to scorch.
Add the garlic and cook 1 minute until fragrant.
Pour in the sherry, scraping up the browned bits, and simmer 2 minutes until mostly evaporated.
Add the beef broth, thyme, bay leaf and 1/2 teaspoon pepper, and bring to a simmer.
Simmer uncovered 20 minutes, then discard the thyme stems and bay leaf.
Meanwhile, toast the baguette slices on a baking sheet under the broiler 1 to 2 minutes per side until golden.
Ladle the soup into oven-safe crocks, float 2 toasted baguette slices on top of each, and mound with the Gruyere.
Broil 2 to 3 minutes until the cheese is melted, bubbling and browned in spots.
Good to Know
The caramelized onion and broth base keeps well for 3 days in the fridge or 2 months frozen; just make the fresh toast and cheese topping the day you serve it.