How we built our own LLM tracing, and how you can build yours.
โ 5 min readLLM observabilityLangGraphAWS Bedrock
IN SHORT
- The model already reports tokens and latency. Just save them.
- Save traces in your own database, linked to the chat message.
- One row per LLM call, one row per tool call.
- Trace the guardrail and judge calls too. They also cost tokens.
What do we want to see?
For every chat message, we want to answer five questions:| Question | What to save |
|---|---|
| What did the user ask? | The question |
| What did the model see? | The exact prompt |
| What did it do? | Every LLM call and tool call |
| What did it cost? | Tokens and latency |
| Was it right? | A quality score and user feedback |
The design
Two tables do all the work:llm_trace: one row per LLM calltool_invocation: one row per tool call

Level 1: Trace one LLM call
Every model response already has token counts and latency. In LangChain, they are inusage_metadata and response_metadata. Save them. Do not count tokens yourself.
# after each LLM call, save one row:
model_name = response.model_name
input_tokens = response.usage.input_tokens
output_tokens = response.usage.output_tokens
latency_ms = response.latency
raw = the full response as JSON
Keep the full response as JSON. For reasoning models, it also saves the model’s thinking, with no extra work.
๐ก TIP
Latency can come as a number, or as a list when the response is streamed. Handle both.
Level 2: Trace one agent turn
An agent may call the LLM several times to answer one question. Add these calls up into one row for the turn. The chat history also holds all the old turns. So only use the messages after the last user message:this_turn = messages after the last user message
for each AI message in this_turn:
input_tokens += its input tokens
output_tokens += its output tokens
Level 3: Trace tools
Many wrong answers come from a tool, not the model. So save every tool call intool_invocation: name, arguments, output, status and latency.
The model does not report tool latency, so time it yourself:
start = time.time() result = await run_tool(...) latency_ms = (time.time() - start) * 1000
โ ๏ธ WATCH OUT
“Not found” is not a failure. Count a failure only when the tool could not do its job, like a network or API error. Otherwise your failure-rate chart looks worse than reality.
Level 4: Trace multi-step flows
Some flows run fixed steps, each with its own LLM call: understand, then generate, then verify, then answer. Save one row per step, with its exact prompt and output. Then you can see which step went wrong, or which one costs the most. To collect these calls without passing a trace object everywhere, use a PythonContextVar. It is a small store that belongs to the current request:
calls = ContextVar("llm_calls")
# at the start of the request
calls.set([])
# one helper for every LLM call
async def call_llm(prompt, step):
response = await llm.ainvoke(prompt)
calls.get().append((step, prompt, response))
return response
# at the end of the request: save one row per recorded call
Every LLM call goes through this one helper, so no step is missed.
๐ก TIP
Prompts can be very long. Cap the text you store. We keep up to 60,000 characters per field.
Level 5: Trace guardrails
A guardrail check (is the question safe? what is the intent?) is an LLM call too. Trace it as its own row, and also save its decision on the chat message. Then, even when a message is blocked and the agent never runs, you can still see why it was blocked and what the check cost.๐ก TIP
Also save the rewritten question. “What about the second one?” means nothing alone. The rewritten version explains many strange answers.
Level 6: Was the answer right?
Tokens tell you the cost, not the quality. For quality, a second model (an “LLM judge”) scores each answer. For example: is the answer supported by the documents we found? Save the score on the chat message. If it is below a threshold, flag the message so a person can review it.๐ NOTE
The judge is an LLM call too. Save its tokens, or your totals will be too low.
Level 7: The dashboard
Everything is in your database, so the dashboard is simple SQL. Useful cards:- Total and average tokens
- Response time trend
- Tool calls and failure rate
- Latency per tool
- Blocked messages (guardrail)
- Flagged answers (judge)


tenant_id, so each client sees only their own usage.
Own tracing or a third-party tool?
| Own tracing | Third-party tool | |
|---|---|---|
| Where data lives | Your database | The vendor’s cloud (some allow self-hosting) |
| Join with your business data | Easy, same database | Harder, separate system |
| Cost | Your existing database | Usually per trace or a subscription |
| Timeline view, replay, datasets | No | Usually yes |
| Setup | Code to write and maintain | Quick |
Start small
You do not need everything on day one. Build in this order:Make one helper for all LLM calls. Save a row per call: model, input tokens, output tokens, latency, raw response.
Link each row to the chat message.
Add a tool table: name, arguments, output, status, latency.
Save the guardrail and judge results on the message.
Build the dashboard with SQL.
Final thoughts
Before this, “why did the bot say that?” was a guess. Now it is a query. We open the message and see the prompt, the tools and the cost.
You do not need a big platform for this. You need one helper around your LLM calls, and two tables to keep what it records.
BUILT INTO RESOLVR
See this tracing at work in Resolvr, our AI support engineer
Resolvr answers technical questions from your own product documentation, 24/7. Every answer is scored, and every conversation is logged. You see exactly where customers struggle.
70%
fewer routine tickets for human agents
15%
higher CSAT
24/7
global coverage, no extra headcount