The ab initio cluster model approach has been used to study the electronic structure and magnetic coupling of KCuF 3 and K 2 CuF 4 in their various ordered polytype crystal forms. Due to a cooperative Jahn-Teller distortion these systems exhibit strong anisotropies. In particular, the magnetic properties strongly differ from those of isomorphic compounds. Hence, KCuF 3 is a quasi-one-dimensional ͑1D͒ nearest neighbor Heisenberg antiferromagnet whereas K 2 CuF 4 is the only ferromagnet among the K 2 M F 4 series of compounds ͑M ϭMn, Fe, Co, Ni, and Cu͒ behaving all as quasi-2D nearest neighbor Heisenberg systems. Different ab initio techniques are used to explore the magnetic coupling in these systems. All methods, including unrestricted Hartree-Fock, are able to explain the magnetic ordering. However, quantitative agreement with experiment is reached only when using a state-of-the-art configuration interaction approach. Finally, an analysis of the dependence of the magnetic coupling constant with respect to distortion parameters is presented. ͓S0163-1829͑99͒09831-8͔