LLM Observability
Why observability
Building an agent locally, you run it and look at the output. The trouble starts in production, and sometimes in development too. You need to know which stage is bad: the input, the retrieval, the decision, or the LLM call.
- Why did this step take 10 seconds? Is it the LLM call or something else?
- What costs the most in the pipeline? (e.g. caching was never turned on)
- Why did this LLM call return garbage? What went into it?
- Seeing inside the agent -> <span class="term" data-term="observability" data-def="Logging what happens inside each step of an agent so it can be inspected and debugged.">Observability
Observability does not raise performance by itself. It lets you see, so you can take action: fix caching, make a step async, correct inputs.
- Slow step with tiny work -> a step was accidentally run synchronously
- Expensive step -> prompt caching forgotten
- Errors also get logged at the stage where they occur
Why print statements fail
Print loses structure. Therefore fails.
- Nested calls: cannot tell parent log from child log from inner child
- Async calls: output appears in whatever finishes first, so order and timing are unclear
- Multi-step loops: no way to know which step a tool call came from
- Cannot query: e.g. "how many steps took more than 10 seconds?"
| Flat prints | Structured log |
|---|---|
| 120k tokens, model, prompt, tool call, prompt, output ... in one line stream | Agent -> Step 1 (prompt, model, tokens) -> Tool call 1 -> Step 2 -> sub-steps |
| Hunt for step 2, then its LLM call, then why it is slow | See step 2, its prompt, model, tokens and slow sub-step at a glance |
What we need: structure, parent-child relationships, and the ability to query.
Trace, span, attributes
- Trace - the whole life of one agent request, the root of the tree
- - Example: one request to a medical QA agent is one trace
- - One trace per task or question
- Span - one operation inside a trace
- - LLM calls, tool calls, agent steps, custom logic are all spans
- - A span can contain child spans (step -> LLM call -> tool call)
- Attributes - key-value metadata attached to a span
- - Latency, model, input, environment, SDK version, user, session, cost, tags
Example
Medical QA agent (trace): fetch patient history (span) -> LLM call to assess recent problems (span) -> tool call to fetch data (span) -> tool call to check if immediate help is needed (span) -> final LLM call (span).
Because of this tree, time order (what happened before and after what) is preserved. Causality -> knowing what happened at which stage and after what.
OpenTelemetry and the export pipeline
Standard for describing what happens inside any running system (backends, infra, LLM agents) -> OpenTelemetry. It does not care about the vendor. Platforms differ only in how they show the data.
- OTLP - OpenTelemetry Protocol, the format used to send data out
- Until export, data is still local; the backend is where you view it
- Once attributes are in the backend, you can filter by latency, prompt size, model
- The backend can also put an LLM on top that reads traces and tells you what is going wrong
Project idea: build a minimal observability tool on raw OpenTelemetry. Additions Langfuse lacks: chat with your traces, suggestions from traces, and a daily summary page of failed tools and LLM calls.
What LLM observability platforms add
OpenTelemetry gives structure and chronological order. But its raw output is JSON full of ids, very hard to read. General APM tools (application performance monitoring) like Datadog or Better Stack show what goes in and out, but not tokens consumed or which prompt ran at which stage.
- Readable tree view of traces
- Sessions and users
- Monitors and alerts (e.g. error count rises)
- Prompts, costs, scores
- Filter by user, session, tag, latency, model
- Evals on top of tracing data
| Tool | Note |
|---|---|
| Langfuse | Open source, self-hostable |
| LangSmith | Fits the LangChain / deep agents ecosystem |
| Helicone and others | Same core idea, different extras |
Scores - a number attached to a trace.
- From an LLM judge (e.g. 8 out of 10)
- From heuristics (latency over 10 s, output tokens above a limit)
- From human review
from langfuse import observe
@observe()
def agent_step(prompt):
...- Decorator on a function -> logs what goes in and out
- Importing the provider client through Langfuse (e.g. langfuse.openai) tracks model, tokens and prompts
- A score can be created against a trace id
Cutting cost in production: model routing and fusion
- Prompt caching
- Smaller or open-source models where quality holds
- Model routing - send each step to a suitable model
- - Risky: it can hurt performance unless tested on thousands of traces
- Enterprises usually buy tokens directly from providers, not host GPUs
- Planning step uses a frontier model; exploring files uses a cheaper one; the real fix switches back to frontier
- Routing signals: is the agent failing often, still exploring, context pressure, cache state
- Problem: calling another model with unshared context loses prompt caching, costing more
- Fix: main and side model each keep their own persistent cache
- Fugu (Sakana) - an orchestrator over a pool of models that can also call itself recursively; reported as expensive with unimpressive results
- Multi-agents are an API-level idea; mixture of experts is inside the transformer architecture
Loop engineering
- Two things to define: what has to be completed, and the sign of completion
- Example: run all tests; if any fail, rework and rerun; stop when they pass
- Context: compact at a chosen threshold so the model does not hallucinate
- Use sub-agents so their context does not pollute the main agent
Glossary
- Observability
- Logging what happens inside each step of an agent so it can be inspected and debugged.
- Trace
- The full lifecycle of one agent request, the root of a tree of spans.
- Span
- One operation inside a trace, such as an LLM call or tool call; spans can nest.
- Attributes
- Key-value metadata attached to a span, like latency, model, user or cost.
- OpenTelemetry
- Vendor-neutral industry standard for describing what happens inside a running system.
- OTLP
- OpenTelemetry Protocol, the format used to export telemetry data.
- APM
- Application performance monitoring, general tools like Datadog that are not LLM-aware.
- Model routing
- Sending different steps or tasks to different models to save cost.
- Score
- A number attached to a trace from an LLM judge, heuristic or human.