Every so often an LLM inquiry in my application would take noticeably longer than the one before it, even though nothing had failed and the answer that eventually showed up was usually a fine one.
An inquiry goes to a model together with a set of tools, each of which is backed by one of several data stores. The model rarely answers right away. It asks for a tool call, my code runs the tool and sends the result back, and then the model takes another look and either answers or asks for another tool. I call each of those round trips a cycle, and what I had seen was that the slow inquiries were the ones that went through too many of them.
So how many cycles did that take?
To be honest, when I first noticed this I didn’t know, and that bothered me more than the wait itself.
Every cycle is a separate call to the model, and the calls are far from equal because each one carries the ever-increasing context. The last call of an inquiry includes the original question plus every tool call and tool result that came before it. So what feels like one question to the person asking is a string of requests to the provider, each bigger than the one before.
When an inquiry ran long I wanted to know how many cycles it had taken and which of them was the expensive one. I also wanted to know whether the model had asked for the same tool twice, and whether a different model would have gotten there in fewer.
Can’t I just log this myself?
Some might argue that all of this is a logging problem, and that wrapping the model call and writing down the token counts and the time would be enough. For an application that talks to one model, I think they are right.
It stops being enough around the time a second model shows up. Each provider reports usage in its own shape, and turning tokens into dollars means keeping a price table current. Trying a different model is also still a code change followed by a deploy, which logging does nothing about.
LiteLLM can run as a proxy that sits between an application and the model providers and exposes an OpenAI-compatible endpoint, so the application talks to one place no matter which model ends up answering.
Model switching made simple
In the proxy’s config.yaml, each entry has a model_name, which is the name the application asks for, and a litellm_params.model, which is the model that gets called.
model_list:
- model_name: inquiry-model
litellm_params:
model: provider-a/model-a
api_key: os.environ/PROVIDER_A_KEY
The application asks for inquiry-model and never learns what’s behind it. Finding out whether a different model gets through that slow inquiry in fewer cycles takes a one-line change in this file and the same inquiry sent again, with nothing to deploy.
Fallbacks are configured in the same file, for the days when a provider is having trouble. With a second entry and one more setting, a call to inquiry-model that keeps failing is sent to inquiry-model-backup instead, while the application goes on asking for the same name.
model_list:
- model_name: inquiry-model
litellm_params:
model: provider-a/model-a
api_key: os.environ/PROVIDER_A_KEY
- model_name: inquiry-model-backup
litellm_params:
model: provider-b/model-b
api_key: os.environ/PROVIDER_B_KEY
litellm_settings:
fallbacks: [{"inquiry-model": ["inquiry-model-backup"]}]
The proxy’s UI also comes with a Model Compare playground that runs the same prompt against up to three models side by side and shows time to first token, total latency, token usage and cost for each.
Observability FTW
In my opinion observability is the more valuable half of what LiteLLM offers. If every cycle of an inquiry carries the same litellm_session_id, the proxy groups those requests into one session. For each request in that session, the logs page shows the spend, the token usage and which tools the model called.
A slow inquiry then shows up as one session with one row per cycle. The token counts should climb from row to row, since each call carries more of the conversation than the one before it. If one row is three times the size of the previous one, some tool returned far more than it should have, and I know which one to look at. If two rows in a row call the same tool with the same arguments, the model is repeating itself.
None of this requires me to read a prompt. The proxy doesn’t store request and response content unless store_prompts_in_spend_logs is turned on, which I think is the right default when the inquiries belong to my customers. Counting cycles and comparing token counts works without it, and only a question like “why did it ask for that tool again?” needs the setting switched on.
Every response also comes back with headers that report how long the call took, how much of that time was the proxy itself, and what the call cost. The cost is split into input, output and cache reads, which was more detail than I expected. Since later cycles resend the same growing conversation, the cache-read figure is how I can tell whether prompt caching is helping.
What about my tools?
The gateway sees my model calls but it doesn’t see my tools running. If the slow part of an inquiry is one of the data stores taking its time, the gateway can tell me, with great precision, that something happened between one call and the next.
The proxy can send its logs to OpenTelemetry with callbacks: ["otel"], and it accepts a traceparent header that ties its records to a trace my application started. My application records a span, which is a timed entry, around each tool call and passes the trace id along with every model call. Both sets of spans then land under one trace in a tracing backend like Langfuse or Grafana, where they read as a single timeline of model calls and tool calls. On that timeline a slow cycle and a slow data store no longer look the same.
Should you consider an LLM gateway like LiteLLM?
These are the benefits I’d point to:
- Models as configuration: comparing or switching models is a one-line change in the proxy’s config and doesn’t require deploying the application.
- Cycles per inquiry: one session id groups every cycle, so I can count them.
- Cost per customer: each customer account gets its own key and each request carries the end user’s id in the
x-litellm-end-user-idheader, so the proxy tracks spend per key and spend per end user and can hold either to a budget. - Latency on every response: the duration and overhead headers show whether the model was slow or the extra hop was.
- Retries and fallbacks: two more response headers count how many of each were attempted.
- Tools and model calls in one trace: with OpenTelemetry and
traceparent, my tool spans and the model calls sit on one timeline in my tracing backend.
If your application makes one model call per request, to one provider and from one service, you probably don’t need a gateway, and logging the call yourself will serve you fine. I’d recommend one once a single request turns into several model calls, or once you want to compare models without redeploying. Those are the situations where “what did that request cost and why was it slow?” has no good answer otherwise. LiteLLM is the one I’d start with, because the same proxy handles both the model switching and the per-request record.
A small start is enough to find out whether it helps you:
- Run the proxy with a single model name that points at the model you already use.
- Send a session id on every cycle of an inquiry, while leaving content storage off.
- Send a handful of inquiries that people have already made through it, including a couple of the slow ones.
- Point the model name at a second model and send the same inquiries again.
That gives you cycles, tokens and spend per inquiry for two models, along with what the overhead header says about the extra hop.
For this application my answer was yes. The rollout was small, since the application only had to point at the proxy and ask for a model by name. The observability gains were substantial: I was able to identify slow queries that my tools were making, which before I couldn’t tell apart from a slow model.