If a metal is used instead of graphite as the anode, then O2 will be liberated. This will not only oxidize the metal of the electrode but also convert some of the Al liberated at the cathode back into Al2O3. Hence, graphite is used for preventing the formation of O2 at the anode. Moreover, graphite is cheaper than other metals.