6. Given the function $f(x)=\frac{x^{2}+3 x+1}{x^{2}-x+1}$. Then the range of $f(x)$ is ( ).
(A) $\left[-1, \frac{5}{3}\right]$
(B) $\left[-\frac{1}{3}, 5\right]$
(C) $\left[-\frac{5}{3}, 1\right]$
(D) $\left[-5, \frac{1}{3}\right]$
6. B.

Notice that, $x^{2}-x+1>0$.
Therefore, the domain of $f(x)$ is all real numbers.
Let $y=\frac{x^{2}+3 x+1}{x^{2}-x+1}$. Then
$$
\begin{array}{l}
y\left(x^{2}-x+1\right)=x^{2}+3 x+1 \\
\Rightarrow(y-1) x^{2}-(y+3) x+(y-1)=0 .
\end{array}
$$

If the coefficient of the quadratic term $y-1=0$, then $x=0$.
If the coefficient of the quadratic term $y-1 \neq 0$, then
$$
\begin{array}{l}
\Delta=(y+3)^{2}-4(y-1)^{2} \geqslant 0 \\
\Rightarrow 3 y^{2}-14 y-5 \leqslant 0 \\
\Rightarrow y \in\left[-\frac{1}{3}, 5\right] .
\end{array}
$$

Therefore, the range of $f(x)$ is $\left[-\frac{1}{3}, 5\right]$.