Skip to content
mentoor

Topic 1 of 6 · 5 min read

Spans for model, tool and retrieval calls

Goal: Given a request flow, draw its span tree and list the attributes each span must record to explain a wrong or slow answer.

Record every step of an LLM request as its own span, nested under the request that caused it, so a wrong or slow answer can be traced to one step instead of guessed at.

Check yourself · 1 of 3

What is a span in an LLM trace?

Choose one answer

Why it matters

A normal API call has one cause for a slow response. An LLM feature has many: a retrieval query, a prompt build, a first model call, two tool calls, a second model call, a safety check. The final answer is wrong or slow because of one of them, and a single log line at the end cannot say which. A trace splits the request into spans and keeps the parent-child links between them.

How to do it

A span is one timed operation with a name, a start and end time, a status and attributes. A trace is the tree of spans that share one trace id. For an LLM feature, make one span for each of these:

  1. The request (root): the user-facing operation, such as answer_question.
  2. Retrieval: the query text or its hash, the index name, how many chunks came back, their ids and scores.
  3. Prompt build: the template name and version, and how many tokens the assembled prompt has.
  4. Model call: provider, model, input and output token counts, finish reason, latency, and any error.
  5. Tool call: the tool name, its arguments, the result size, and whether it failed.
  6. Agent turn: one span per loop iteration, so you can see the loop count and where it stopped.

Put the model call and tool calls inside the agent turn that triggered them. Do not flatten them into one list: the nesting is what shows that tool call 3 happened because of model call 2.

Record errors on the span where they happen, with a status and an error type. A retry should be a new child span, not an invisible loop inside one span, so you can count retries.

Before and after

Before: one log line per request, answer ok 8.2s. You know it was slow and nothing else.

After, the same request as a tree:

answer_question            8.2s
  retrieve                 0.4s   chunks=0
  build_prompt             0.0s   template=support@v7 tokens=1,980
  chat claude-sonnet       7.7s   in=1,980 out=412 finish=stop

Now the cause is visible: retrieval returned zero chunks and the model answered from memory, slowly and wrongly.

What goes wrong

  • One span for the whole request. You get a duration and no cause.
  • Missing token counts and finish reasons. A truncated answer (finish=length) looks like a bad model.
  • Retries hidden inside a span. The latency looks like one slow call, and the retry bill disappears.
  • Spans that are not linked. If each step starts its own trace, you cannot join them back into one request.

Examples

A tree for an agent request

handle_ticket
  agent_turn 1
    chat model-a        in=2,400 out=60  finish=tool_calls
    tool lookup_order   args={id: 4417} result=ok
  agent_turn 2
    chat model-a        in=2,950 out=180 finish=stop

Two turns, one tool call. If the answer cites the wrong order, open lookup_order first: its arguments show which order id the model asked for.

A retry made visible

A model call fails with a rate-limit error and succeeds on the second try. Logged as two child spans, chat (error, 0.2s) and chat (ok, 3.1s), the parent shows 3.3s and you can see why. Logged inside one span, it shows only a slow success.

Key points

  • A trace is a tree of spans that share one trace id; each span is one timed operation with attributes and a status.
  • Make a separate span for retrieval, prompt build, each model call, each tool call and each agent turn.
  • Nest spans under the step that caused them so the tree shows why a call happened.
  • Model spans need model name, token counts, finish reason, latency and error status to explain a bad answer.
  • Record a retry as its own child span so retry counts and cost stay visible.

Common mistakes

  • Tracing only the final model call and ignoring retrieval and tools.
  • Storing the whole request as one span with a long attribute list.
  • Leaving out finish reason, so truncation looks like a quality problem.
  • Starting a new trace in each service instead of passing the trace context along.

Check yourself · 2 of 3

A request is slow and the trace shows one 7-second model call and a 0.1-second retrieval. Where do you look first?

Choose one answer

Check yourself · 3 of 3

A model call fails once and then succeeds on retry. How should the trace show it?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic