Background: The absence of an FDA-approved medication for the treatment of cocaine use disorder (CUD) may, in part, reflect the varying conditions present when the decision to use cocaine is made, with one medication unlikely to work under all conditions. The objective of this double-blind, placebo-controlled, human laboratory study was to test the effects of modafinil, a medication with mixed efficacy for the treatment of CUD, using a novel self-administration procedure designed to model distinct clinical scenarios. Methods: During modafinil maintenance (0, 300 mg/day), participants chose to self-administer up to 7 doses of smoked cocaine (25 mg) under 9 conditions: immediately after exposure to: (a) cues associated with cocaine and a non-contingent cocaine administration, i.e. 'prime' (25 mg), (b) only cocaine cues, and (c) neither cues nor cocaine. Each condition was tested when self-administered cocaine cost $5, $10 and $15/dose. Results: Nontreatment-seeking cocaine smokers (3 F,13 M), spending $388 +/- 218/week on cocaine and with no history of alcohol use disorder, completed the study. Relative to placebo, modafinil robustly attenuated self-administration when cocaine was expensive ($10,$15/dose) and when there was no 'prime.' Modafinil had no effect on self-administration when cocaine was inexpensive ($5/dose) or when participants received a 'prime.' Conclusions: Modafinil's effects on cocaine-taking varied substantially as a function of recent cocaine exposure and cost, which may help explain the mixed clinical findings. Modafinil may be most effective for preventing relapse in abstinent patients, particularly under conditions in which cocaine is costly, rather than initiating abstinence for those continuing to use cocaine.