Prompt

How do I trace prompts, model calls, tool use, and final responses in an LLM app?

Technology / Observability3 observationsLast seen Oct 29, 2026

Latest observation

Oct 29, 2026OpenAI 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:

  1. User request / entry span

    • request ID
    • user/session ID
    • route/action
    • timestamp
  2. Prompt construction

    • system prompt
    • developer prompt
    • user message
    • retrieved context
    • templates/variables
    • sanitized/redacted sensitive data
  3. LLM call span

    • model name/version
    • temperature/top_p/etc.
    • input messages
    • output text
    • token counts
    • latency
    • retries / rate-limit handling
    • finish reason
  4. Tool call spans

    • tool name
    • arguments
    • tool result
    • duration
    • errors/exceptions
  5. 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

2) Attach useful metadata

Store tags/attributes like:

  • model=gpt-4.1
  • tool=web_search
  • latency_ms=842
  • prompt_tokens=1200
  • completion_tokens=240
  • user_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:

  1. Use OpenTelemetry for core traces.
  2. Add an LLM-specific tracer (Langfuse/LangSmith/Helicone/etc.) for prompt and tool visibility.
  3. Wrap your LLM client and tools with instrumentation.
  4. 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

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (3 observations for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as dotdotPR. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.