Using the Kuznetsov trace formula, we prove a spectral decomposition for the sums of generalized Dirichlet $L$-functions. Among applications are an explicit formula relating norms of prime geodesics to moments of symmetric square $L$-functions and an asymptotic expansion for the average of central values of generalized Dirichlet $L$-functions.