This paper offers a methodology to address the endogeneity of inputs in the directional technology distance function (DTDF)-based formulation of banking technology which explicitly accommodates the presence of undesirable nonperforming loans-an inherent characteristic of the bank's production due to its exposure to credit risk. Specifically, we model nonperforming loans as an undesirable output in the bank's production process. Since the stochastic DTDF describing banking technology is likely to suffer from the endogeneity of inputs, we propose addressing this problem by considering a system consisting of the DTDF and the first-order conditions from the bank's cost minimization problem. The first-order conditions also allow us to identify the cost-optimal directional vector for the banking DTDF, thus eliminating the uncertainty associated with an ad hoc choice of the direction. We apply our cost system approach to the data on large US commercial banks for the 2001-2010 period, which we estimate via Bayesian Markov chain Monte Carlo methods subject to theoretical regularity conditions. We document dramatic distortions in banks' efficiency, productivity growth and scale elasticity estimates when the endogeneity of inputs is assumed away and/or the DTDF is fitted in an arbitrary direction.