Since $S$ is quasi-compact and quasi-separated, the same is true for $X$. By Lemma \ref{lemma-pseudo-coherent-hocolim} we can write $K = \text{hocolim} K_n$ with $K_n$ perfect and $K_n \to K$ inducing an isomorphism on truncations $\tau_{\geq -n}$. Let $K_n^\vee$ be the dual perfect complex (Cohomology, Lemma \ref{cohomology-lemma-dual-perfect-complex}). We obtain an inverse system $\ldots \to K_3^\vee \to K_2^\vee \to K_1^\vee$ of perfect objects. By Lemma \ref{lemma-perfect-relatively-perfect} we see that $K_n^\vee \otimes_{\mathcal{O}_X} E$ is $S$-perfect. Thus we may apply Lemma \ref{lemma-derived-pushforward-rel-perfect} to $K_n^\vee \otimes_{\mathcal{O}_X} E$ and we obtain an inverse system $$ \ldots \to M_3 \to M_2 \to M_1 $$ of perfect complexes on $S$ with $$ M_n = Rf_*(K_n^\vee \otimes_{\mathcal{O}_X}^\mathbf{L} E) = Rf_*R\SheafHom(K_n, E) $$ Moreover, the formation of these complexes commutes with any base change, namely $Lg^*M_n = Rf'_*((L(g')^*K_n)^\vee \otimes_{\mathcal{O}_{X'}}^\mathbf{L} L(g')^*E) = Rf'_*R\SheafHom(L(g')^*K_n, L(g')^*E)$. \medskip\noindent As $K_n \to K$ induces an isomorphism on $\tau_{\geq -n}$, we see that $K_n \to K_{n + 1}$ induces an isomorphism on $\tau_{\geq -n}$. It follows that $K_{n + 1}^\vee \to K_n^\vee$ induces an isomorphism on $\tau_{\leq n}$ as $K_n^\vee = R\SheafHom(K_n, \mathcal{O}_X)$. Suppose that $E$ has tor amplitude in $[a, b]$ as a complex of $f^{-1}\mathcal{O}_Y$-modules. Then the same is true after any base change, see Lemma \ref{lemma-tor-independence-and-tor-amplitude}. We find that $K_{n + 1}^\vee \otimes_{\mathcal{O}_X} E \to K_n^\vee \otimes_{\mathcal{O}_X} E$ induces an isomorphism on $\tau_{\leq n + a}$ and the same is true after any base change. Applying the right derived functor $Rf_*$ we conclude the maps $M_{n + 1} \to M_n$ induce isomorphisms on $\tau_{\leq n + a}$ and the same is true after any base change. Choose a distinguished triangle $$ M_{n + 1} \to M_n \to C_n \to M_{n + 1}[1] $$ Take $S'$ equal to the spectrum of the residue field at a point $s \in S$ and pull back to see that $C_n \otimes_{\mathcal{O}_S}^\mathbf{L} \kappa(s)$ has nonzero cohomology only in degrees $\geq n + a$. By More on Algebra, Lemma \ref{more-algebra-lemma-lift-perfect-from-residue-field} we see that the perfect complex $C_n$ has tor amplitude in $[n + a, m_n]$ for some integer $m_n$. In particular, the dual perfect complex $C_n^\vee$ has tor amplitude in $[-m_n, -n - a]$. \medskip\noindent Let $L_n = M_n^\vee$ be the dual perfect complex. The conclusion from the discussion in the previous paragraph is that $L_n \to L_{n + 1}$ induces isomorphisms on $\tau_{\geq -n - a}$. Thus $L = \text{hocolim} L_n$ is pseudo-coherent, see Lemma \ref{lemma-pseudo-coherent-hocolim}. Since we have $$ R\SheafHom(K, E) = R\SheafHom(\text{hocolim} K_n, E) = R\lim R\SheafHom(K_n, E) = R\lim K_n^\vee \otimes_{\mathcal{O}_X} E $$ (Cohomology, Lemma \ref{cohomology-lemma-colim-and-lim-of-duals}) and since $R\lim$ commutes with $Rf_*$ we find that $$ Rf_*R\SheafHom(K, E) = R\lim M_n = R\lim R\SheafHom(L_n, \mathcal{O}_S) = R\SheafHom(L, \mathcal{O}_S) $$ This proves the formula over $S$. Since the construction of $M_n$ is compatible with base chance, the formula continues to hold after any base change.