News / Learn / Production & LLMOps
How do you log and trace LLM calls?
You'll be able to record what each LLM call sent and got back, and follow one user request through every step it touched.
In plain English
An LLM call is a request to a model and a reply from it. Logging saves the details of each call: the input, the output, the model used, the time taken, and the cost. Tracing links many calls together, so you can see the full path of one user request, including any tool calls and retries.
Why it matters
Without logs, a bad answer is a mystery. You cannot tell if the prompt, the context, the model, or a tool caused it. Good logs let you debug a single failure, measure quality and cost over time, and prove what your product did when a customer asks.
How it works
Each model call should produce a record. Keep the fields that matter: a request ID, a timestamp, the model name and settings such as temperature, the full messages sent, the raw response, token counts, latency, and any error. Store these in a place you can search.
Tracing adds structure. A trace is one user request. Inside it, spans represent steps: retrieving documents, calling the model, running a tool, and calling the model again. Each span has a start time, an end time, a parent, and its own inputs and outputs. The parent-child tree shows you where time and errors went.
Propagate one ID through the whole flow. Pass the trace or request ID from the entry point into every function, background job, and tool call. Without this, the pieces stay disconnected and you cannot rebuild the story.
Redact before you store. Prompts often contain names, emails, or account data. Remove or mask sensitive fields before writing logs, and set how long you keep them. Logs are a data store too.
A concrete example
A small wrapper that records every model call with a shared trace ID. The model client here is a stand-in for whichever SDK you use.
import json, time, uuid, logging
log = logging.getLogger("llm")
def call_model(client, *, trace_id, step, model, messages, temperature=0.2):
span_id = str(uuid.uuid4())
start = time.time()
try:
reply = client.generate(model=model, messages=messages, temperature=temperature)
status, error = "ok", None
text = reply.text
usage = reply.usage # e.g. input and output token counts
except Exception as e:
status, error, text, usage = "error", str(e), None, None
raise
finally:
log.info(json.dumps({
"trace_id": trace_id,
"span_id": span_id,
"step": step,
"model": model,
"temperature": temperature,
"latency_ms": round((time.time() - start) * 1000),
"status": status,
"error": error,
"usage": usage,
"input": messages,
"output": text,
}))
return textA short sketch to show the idea. Check the official docs of the tool you use.
What goes wrong
- Logging only the final answer Record every model call and tool call in the chain. Most failures happen in an intermediate step, such as bad retrieved context.
- No shared trace ID Create one ID at the start of each request and pass it into every call. Without it you cannot connect the records.
- Storing raw personal data Redact names, emails, and secrets before writing logs. Set retention limits and restrict who can read them.
- Logging without settings Record the model name, version string, temperature, and tool definitions. The same prompt can behave differently when any of these change.
Going deeper
Learn OpenTelemetry, the common standard for traces and spans, and how tracing tools for LLM apps map onto it. Then study how to turn logged traces into eval datasets, so production failures become test cases.
FAQ
Do I need a special tool to log LLM calls?
No. Structured JSON logs written to a database or log store are enough to start. Dedicated tracing tools add a nicer view and sampling, but the core idea is the same.
What is the difference between a trace and a span?
A trace covers one whole request. A span is one step inside it, such as a model call or a database lookup. Spans nest to show the order and timing.
Should I log every prompt in full?
Log what you need to debug, and redact sensitive parts. For high-volume apps, sample full logs and keep summaries for all requests.
Why log the model name and settings?
Models and settings change over time. Without them you cannot tell whether a quality change came from your code or from a different configuration.
Written with AI by CapyLaunch. Spotted a mistake? Email hello@capylaunch.com.