Let $\tau_i\sim\exp\left(\lambda\right)$ be independent and identically distributed exponentials with parameter $\lambda$. Then, for given $n$, the sum of these values $$T_n := \sum_{i=0}^n \tau_i$$ follows an Erlang-Distribution with probability density function $$\pi(T_n=T| n,\lambda)={\lambda^n T^{n-1} e^{-\lambda T} \over (n-1)!}\quad\mbox{for }T, \lambda \geq 0.$$
I am interested in the distribution of $T_\tilde n$ where $\tilde n$ is a random variable such that for $\tau_a \sim \exp(\lambda_a)$ exponentially distributed, it holds that $$T_\tilde n \leq \tau_a \\T_{\tilde{n}+1} > \tau_a.$$
In other words, $T_{\tilde n}$ is truncated by an exponential distribution. I fail in deriving the distribution of $\tilde n$ but maybe there is an easier way: $$\pi\left(\tilde n = k\right) = \pi\left(T_n < \tau_a|n=k\right)\\ =1-\int\limits_{\mathbb{R}^+}\sum\limits_{n=0}^{k-1}\frac{1}{n!}\exp\left(-(\lambda+\lambda_a)\tau_a\right)(\tau\lambda_a)^n\lambda_ad\tau_a.$$
However, just sampling and eye-balling looks to me like this density isn't that ugly:
iter <- 20000
lambda_a <- 1
lambda <- 2
df <- data.frame(tau=rep(NA, iter), a=rep(NA, iter))
for(i in 1:iter){
set.seed(i)
a <- rexp(1, rate = lambda_a)
s <- cumsum(rexp(500, rate = lambda))
df[i,] <- c(max(s[1], s[s<a]), a)
}
library(tidyverse)
ggplot(df %>% gather(), aes(x = value, fill = key)) +
geom_density(alpha = .3) + theme_bw()