Background Frailty assessment based on routine electronic health records (EHR) may be a good alternative to time-consuming frailty scales. This study mainly aimed to develop and validate an electronic frailty index (eFI) using routine EHR for elderly inpatients and analyse the correlations between frailty and hospitalized events and costs.Methods Based on the cumulative deficit model, we created an eFI from routine EHR. In a prospective cohort, we validated the effectiveness of the eFI by consistency with the comprehensive geriatric assessment-frailty index (CGA-FI). Then, we analysed the correlations between frailty and hospitalized events by logistic regression and costs by generalized linear regression models.Results During the study period, 49,226 elderly inpatients from the EHR were included in the analysis. There were 42,821 (87.0%) patients with sufficient data to calculate an eFI. The cut-off value for the upper tertile of the eFI for these patients was 0.15. A strong correlation between the CGA-FI and eFI was shown in the validation cohort of 685 subjects (Pearson’s r = 0.716, P < 0.001). The sensitivity and specificity for an eFI ≥ 0.15 to identify frailty defined as a CGA-FI ≥ 0.25 were 64.8% and 88.7%, respectively. After adjusting for age, gender, and operation, an eFI ≥ 0.15 showed an independent association with long hospital stay (odds ratio [OR] = 2.889, P < 0.001) and death in hospital (OR = 19.97, P < 0.001) for elderly inpatients from all departments. Moreover, after adjusting for age, gender, and operation, eFI values (per 0.1) were positively associated with total costs (β = 0.453, P < 0.001), examination costs (β = 0.269, P < 0.001), treatment costs (β = 0.414, P < 0.001), nursing costs (β = 0.381, P < 0.001), pharmacy costs (β = 0.524, P < 0.001), and material costs (β = 0.578, P < 0.001) for elderly inpatients from all departments.Conclusions It is feasible to develop an effective eFI from routine EHR for elderly inpatients from a general hospital in China. Frailty is an independent risk factor for long hospital stay and death in hospital. As the degree of frailty increases, the hospitalized costs for elderly inpatients increase accordingly.