A search for the rare decays $B_s^0totau^+tau^-$ and $B^0totau^+tau^-$ is performed using proton--proton collision data collected with the LHCb detector. The data sample corresponds to an integrated luminosity of 3fb$^{-1}$ collected in 2011 and 2012. The $tau$ leptons are reconstructed through the decay $tau^-topi^-pi^+pi^- u_{tau}$. Assuming no contribution from $B^0totau^+tau^-$ decays, an upper limit is set on the branching fraction $mathcal{B}(B_s^0totau^+tau^-) < 6.8times 10^{-3}$ at 95% confidence level. If instead no contribution from $B_s^0totau^+tau^-$ decays is assumed, the limit is $mathcal{B}(B^0totau^+tau^-)< 2.1 times 10^{-3}$ at 95% confidence level. These results correspond to the first direct limit on $mathcal{B}(B_s^0totau^+tau^-)$ and the worlds best limit on $mathcal{B}(B^0totau^+tau^-)$.