Melt butter in a large pot and slowly cook sliced onions over medium-low heat until deeply caramelized, about 35–40 minutes.
Add minced garlic and cook for 1 minute.
Stir in flour and cook briefly.
Pour in beef or vegetable broth and add thyme. Simmer for 20 minutes.
Season with salt and black pepper.
Ladle soup into oven-safe bowls, top with toasted baguette slices and grated Gruyère cheese.
Broil until the cheese is melted, bubbly, and golden brown. Serve immediately.