Adding caramelized onions to basic aglio e olio sweetens the dish a bit, while black olives add color and saltiness.
Put a large pot of heavily salted water on and let it come to a boil.
Meanwhile, cook the onions: Heat oil in a 10-inch or 12-inch skillet over medium heat. Add onions and cook, stirring occasionally, until tender and pale golden at the edges, 20 to 25 minutes. Lower heat if the edges start to brown too quickly or raise heat if onions aren’t turning golden quickly enough.
When the water comes to a boil, cook spaghetti according to package directions until just al dente. Reserve 1 cup pasta water; drain pasta.
Thinly slice garlic cloves, and stir into golden onions, along with red-pepper flakes. Continue to cook until onions are a rich brown, about 10 minutes longer. If the garlic starts to burn, lower the heat.
Move some of the onion mixture to the side of the pan and add anchovy paste to bare spot in the skillet. Cook paste for 1 minute, then add herbs and olives, stir everything together, and cook another 1 minute.
Stir in cooked pasta, butter, and salt to taste, tossing to coat pasta. Add pasta water if it looks dry. Serve with a generous squeeze of lemon, and sprinkle with Parmesan and more Cayenne flakes on top, if desired.