The goal of the study was to describe the population pharmacokinetics of trimethoprim, sulfamethoxazole, and N-acetyl sulfamethoxazole in hospitalized patients. Furthermore, this study used the model to optimize dosing regimens of cotrimoxazole for Pneumocystis jirovecii pneumonia and in patients with renal insufficiency or with continuous renal replacement therapy (CRRT). This was a retrospective multicenter observational cohort study based on therapeutic drug monitoring (TDM) data from hospitalized patients treated with cotrimoxazole. We developed two population pharmacokinetic (POPPK) models: a model of trimethoprim and an integrated model with both sulfamethoxazole and N-acetyl sulfamethoxazole concentrations. Monte Carlo simulations were performed to determine the optimal dosing regimen. A total of 348 measurements from 168 patients were available. The estimated glomerular filtration rate (eGFR) and CRRT were included as covariates on the clearance of all three compounds. Cotrimoxazole TID 1,920 mg and b.i.d. 2,400 mg led to sufficient exposure for infections with P. jirovecii in patients without renal insufficiency. To reach equivalent exposure, a dose reduction of 33.3% is needed in patients with an eGFR of 10 mL/minute/1.73 m2 and of 16.7% for an eGFR of 30 mL/minute/1.73 m2. N-acetyl sulfamethoxazole accumulates in patients with a reduced eGFR. CRRT increased the clearance of sulfamethoxazole, but not trimethoprim or N-acetyl sulfamethoxazole, compared with the median clearance in the population. Doubling the sulfamethoxazole dose is needed for patients on CRRT to reach equivalent exposure.