Prompt
How do I trace prompts, model calls, tool use, and final responses in an LLM app?
Latest observation
Oct 29, 2026
OpenAI APIWeb search: off
The standard way is to add tracing/observability around each step of your LLM workflow so you can see:
- the original prompt/input
- each model call
- any tool/function calls
- intermediate outputs
- the final response
- timing, errors, retries, token usage
What to trace
A good trace usually records these spans/events:
-
User request / entry span
- request ID
- user/session ID
- route/action
- timestamp
-
Prompt construction
- system prompt
- developer prompt
- user message
- retrieved context
- templates/variables
- sanitized/redacted sensitive data
-
LLM call span
- model name/version
- temperature/top_p/etc.
- input messages
- output text
- token counts
- latency
- retries / rate-limit handling
- finish reason
-
Tool call spans
- tool name
- arguments
- tool result
- duration
- errors/exceptions
-
Post-processing / finalization
- response formatting
- policy checks / moderation
- citations / grounding
- final output returned to the user
How to implement it
1) Use distributed tracing with spans
Treat each user request as a trace, and each step as a span.
Example structure:
- Trace:
chat_request_123- Span:
prompt_build - Span:
llm_call_1 - Span:
tool_search - Span:
llm_call_2 - Span:
final_response
- Span:
2) Attach useful metadata
Store tags/attributes like:
model=gpt-4.1tool=web_searchlatency_ms=842prompt_tokens=1200completion_tokens=240user_id=...conversation_id=...
3) Capture inputs and outputs carefully
For debugging, record:
- full prompt/messages
- tool arguments/results
- model outputs
But in production, redact or hash sensitive data:
- secrets/API keys
- PII
- auth tokens
- internal documents if needed
4) Add correlation IDs
Pass a trace_id or request_id through:
- API layer
- orchestration layer
- model client wrapper
- tool executor
This lets you connect logs, metrics, and traces.
5) Use an observability tool or framework
Common options:
- OpenTelemetry for vendor-neutral tracing
- LangSmith for LLM app tracing
- OpenAI tracing / SDK instrumentation if available in your stack
- Helicone, PromptLayer, Langfuse, Arize Phoenix, WhyLabs, etc.
Minimal example pattern
Pseudo-code:
with tracer.start_as_current_span("chat_request") as span:
span.set_attribute("user_id", user_id)
with tracer.start_as_current_span("build_prompt") as pspan:
prompt = build_prompt(messages, context)
pspan.set_attribute("prompt_length", len(prompt))
with tracer.start_as_current_span("llm_call") as lspan:
result = llm_client.chat(prompt)
lspan.set_attribute("model", "gpt-4.1")
lspan.set_attribute("input_tokens", result.usage.input_tokens)
lspan.set_attribute("output_tokens", result.usage.output_tokens)
if result.tool_calls:
for tc in result.tool_calls:
with tracer.start_as_current_span("tool_call") as tspan:
tspan.set_attribute("tool_name", tc.name)
tool_result = run_tool(tc.name, tc.args)
tspan.set_attribute("tool_latency_ms", tool_result.latency_ms)
final = postprocess(result)
span.set_attribute("final_status", "ok")
Best practices
- Keep prompts/versioning immutable so you can reproduce runs.
- Log prompt templates and version IDs.
- Track retries and fallback models.
- Measure latency per step, not just total request time.
- Record tool I/O, especially if tool outputs affect the answer.
- Support replay/debug mode for offline reproduction.
- Add sampling if volume is high.
- Be privacy-conscious and apply retention policies.
Recommended approach
If you’re starting fresh:
- Use OpenTelemetry for core traces.
- Add an LLM-specific tracer (Langfuse/LangSmith/Helicone/etc.) for prompt and tool visibility.
- Wrap your LLM client and tools with instrumentation.
- Standardize IDs and metadata across your app.
If you want, I can also show:
- a Python example with OpenTelemetry
- a LangChain / LangGraph tracing setup
- a Node.js example
- a recommended schema for LLM traces