In situ tailoring of two-dimensional materials' phases under external stimulus facilitates the manipulation of their properties for electronic, quantum and energy applications. However, current methods are mainly limited to the transitions among phases with unchanged chemical stoichiometry. Here we propose on-device phase engineering that allows us to realize various lattice phases with distinct chemical stoichiometries. Using palladium and selenide as a model system, we show that a PdSe2 channel with prepatterned Pd electrodes can be transformed into Pd17Se15 and Pd4Se by thermally tailoring the chemical composition ratio of the channel. Different phase configurations can be obtained by precisely controlling the thickness and spacing of the electrodes. The device can be thus engineered to implement versatile functions in situ, such as exhibiting superconducting behaviour and achieving ultralow-contact resistance, as well as customizing the synthesis of electrocatalysts. The proposed on-device phase engineering approach exhibits a universal mechanism and can be expanded to 29 element combinations between a metal and chalcogen. Our work highlights on-device phase engineering as a promising research approach through which to exploit fundamental properties as well as their applications.