Tracing and observability
You cannot improve what you cannot see. Record every step of every run.
Words on this page (8)
- prompt
- : The input text you give a model, including instructions, examples and data.
- tool use
- : The model asks your program to run a function and reads the result.
- tool
- : A function the model can ask your program to run, described by a name, description and argument schema.
- token
- : A piece of text the model reads and writes, usually a word or part of a word. About 4 characters of English on average.
- trace
- : The full record of one run: inputs, model calls, tool calls, outputs, timing and cost.
- eval
- : A repeatable test of AI output quality, run before and after every change.
- latency
- : The time you wait for a response.
- retrieval
- : Finding the most relevant pieces of text for a query.
What to capture
For each run: the inputs, every model call with its and output, every with arguments and result, timing, counts and cost. Tag runs with the version of the prompt and model. show them as a tree you can open and read, and let you search for failures, slow runs and expensive runs.
What you do with it
Sample traces for error analysis, set alerts on error rate and cost, replay a failing run with a changed prompt, and turn real failures into cases. Be careful with privacy: traces contain user data, so redact and set retention limits. Hosted options include LangSmith and Helicone. PostHog and Arize add product analytics and monitoring. Track cost and per request, and alert on production drift.
A worked example: finding a cost spike
Cost per day jumps from $40 to $210. In the trace tool, filter by day and sort by tokens. One conversation made 63 tool calls in a loop reading the same file. The cause: a tool that returned an error the model kept retrying. Fix the error message, add a retry cap. Without traces this would take days to find.
What a trace looks like
A trace is a tree of spans. A span is one timed step. Invented example for a refund question that took 6.2 seconds in total:
- took 0.4 seconds.
- The first model call took 2.1 seconds and chose a tool.
- The tool call took 0.3 seconds.
- The second model call took 3.2 seconds and wrote the answer.
The four spans add up to 6.0 seconds, and 0.2 is your own code. The slow part is the final model call, so shortening its input is the first thing to try.
Why long runs cost so much
Each model call re-sends the whole history. If every step adds 500 tokens, call number n sends about 500 times n tokens. Ten calls send 500 times 55, which is 27,500 tokens. Twenty calls send 500 times 210, which is 105,000. Twice the steps cost nearly four times the input, unless caching helps.
This is why one looping run can dominate a day's bill. Sorting traces by tokens finds it at once.
What goes wrong: gaps in the trace
Symptom: the trace shows a wrong answer but no cause. Often tool errors were caught and not recorded. To confirm, take one failing run and try to explain it using only the trace. If you must guess, a span is missing.
Common mistake
Logging only the final answer. When something goes wrong you need the steps.
An airline pilot has instruments for altitude, fuel and speed, not just a view out of the window. An AI app without tracing is flying by the view out of the window. Tracing shows what the model was asked, what it answered, which tools it used and how long and costly each step was.
Check yourself
Why tag runs with prompt and model versions?
Not quite. Without versions you cannot tell which change caused a difference.
What is a privacy risk of tracing?
Not quite. Redact sensitive data and set a retention period.
Cost jumps from $40 to $210 in a day. Which step finds the cause fastest?
Not quite. Traces show the expensive run and exactly what it did.
You got 0 of 3 right.
Go deeper
- Repo
Langfuse · Open-source tracing, prompt management and evals.
- Repo
Arize · Open-source observability built on OpenTelemetry.
- CourseEvaluating AI AgentsCourse
DeepLearning.AI and Arize · Adding tracing, then choosing the right evaluator.
- DocsLangfuse docsDocs
Langfuse · Tracing, prompt management and evals.
- DocsLangSmith docsDocs
LangChain · Tracing and evaluation for LangChain and other stacks.
- DocsHelicone docsDocs
Helicone · A proxy-based logger for cost and latency.
- DocsArize docsDocs
Arize · Production observability and evaluation.
Spotted a mistake or something unclear? Report it on GitHub.