Here's my reasoning:
$$ g(f(x)) = \frac{1}{x^2 + 4} = \frac{1}{(x^2+1)+3}$$
Since $x^2 + 1 = f(x)$
$$g(f(x)) = \frac{1}{f(x)+3} \implies g(x) = \frac{1}{x+3} $$
With $g(x)$, Note: $x \ne -3$ $(x \in \mathbb{R})$
Your definition is fine if you just modify composition to be a function from the set
$$\{(f,g) \in T\times T : x\in \operatorname{dom}(f) \Rightarrow f(x)\in \operatorname{dom}(g)\}$$
but this is a somewhat unusual way to axiomatize composition as an operation. More typical is that one regards a function $f:A\rightarrow B$ to have a fixed domain ($A$) and a fixed codomain ($B$) which may be different (larger) than the image $f(A)$. Functions $f:A\rightarrow B$ and $g:C\rightarrow D$ can then only be composed as $g\circ f$ if $B=C$.
This is a bit counterintuitive at first since it means that you wouldn't be allowed to compose a function $f:\mathbb R\rightarrow \mathbb R_{\geq 0}$ with a function $g:\mathbb R \rightarrow \mathbb R$ as $g\circ f$ since the codomain of $f$ doesn't match the domain of $g$, even though it's a subset. The fix to this is to note that since $\mathbb R_{\geq 0}\subseteq \mathbb R$ there is an inclusion map $\iota : \mathbb R_{\geq 0}\rightarrow\mathbb R$ define by $\iota(x)=x$. Then, we can compose $g\circ \iota \circ f$ as desired.
This definition is convenient because it turns out that sometimes we really do care about the codomain - for instance, in linear algebra, there's a notion of duality where you can take a map $f:V\rightarrow W$ and "reverse" it into a map $f^*:W^*\rightarrow V^*$ called the adjoint of $f$, where different codomains would give different adjoints - hence we have to record the codomain. In algebraic topology, the codomain sometimes matters since if you take a circle and map by inclusion it into a plane, the circle can suddenly be shrunk to a point, which it couldn't before - meaning that an inclusion map did something meaningful and that we shouldn't ignore it.
Then, essentially, composition can be defined by first choosing some collection of functions $T$ and a collection of possible domains and codomains $O$, and letting $\operatorname{dom},\operatorname{codom}:T\rightarrow O$ be functions taking a function to their domain and codomain respectively. Composition is then defined to have the domain of
$$\{(g,f) \in T\times T : \operatorname{dom}(g) = \operatorname{codom}(f)\}$$
and an output in $T$ such that $\operatorname{dom}(g\circ f) =\operatorname{dom}(f)$ and $\operatorname{codom}(g\circ f)=\operatorname{codom}(g)$ and such that $h\circ (g\circ f)=(h\circ g)\circ f$. One also desires that for every possible object $o$ in $O$ there be some identity map $\operatorname{id}_o$ such that $f\circ \operatorname{id}_o = f$ and $\operatorname{id}_o\circ f = f$ whenever these compositions are legal. These definitions describe a category - and "functions between sets" is one example of a category, but there turns out to be a lot of other examples, in the same way that "the integers" is an example of a ring, but not the only one - for instance, there's a category of vector spaces with linear maps between them and a category of topological spaces with continuous maps between them, all satisfying this same general setup for composition.
Best Answer
Notice that if there are distinct $b_1,b_2\in B$ such that $f(b_1)=f(b_2)$, you won’t necessarily be able to cancel $f$: there might be some $a\in A$ such that $g(a)=b_1$ and $h(a)=b_2$, but you’d still have $(f\circ g)(a)=(f\circ h)(a)$. Thus, you want $f$ to be injective (one-to-one). Can you prove that that’s sufficient?