Lithium-based rechargeable batteries have been widely used in portable electronics and show great promise for emerging applications in transportation and wind-solar-grid energy storage, although their safety remains a practical concern. Failures in the form of fire and explosion can be initiated by internal short circuits associated with lithium dendrite formation during cycling. Here we report a new strategy for improving safety by designing a smart battery that allows internal battery health to be monitored in situ. Specifically, we achieve early detection of lithium dendrites inside batteries through a bifunctional separator, which offers a third sensing terminal in addition to the cathode and anode. The sensing terminal provides unique signals in the form of a pronounced voltage change, indicating imminent penetration of dendrites through the separator. This detection mechanism is highly sensitive, accurate and activated well in advance of shorting and can be applied to many types of batteries for improved safety.