We present a method to determine optimal monetary policy in heterogeneous-agent economies with nominal frictions and aggregate shocks, under various assumptions regarding fiscal policy. We analyze models with either sticky prices or sticky wages. In the sticky-price economy, when fiscal policy is optimally set, optimal monetary policy implements price stability. Inflation volatility remains low when fiscal policy follows empirically relevant rules. The inflation response is more pronounced when the Phillips curve is steep and profits are skewed toward highly productive agents. In the sticky-wage economy, optimal price inflation becomes significantly more volatile, while wage inflation remains small. Under both types of nominal rigidity, agents with lower productivity tend to benefit more from optimal monetary policy.