Skip to main content

tools for genai request tracing

Summary

  • GenAI request tracing needs three tools working together: automatic instrumentation for your LLM and agent frameworks, a manual span API for your own code, and request-level context (user, session, tags) so you can find a specific request again.
  • MLflow Tracing provides all three in one open-source SDK: mlflow.<library>.autolog(), the @mlflow.trace decorator and mlflow.start_span() context manager, and mlflow.update_current_trace() for tags and metadata.
  • Each traced request becomes one trace: a root span with child spans for every model call, retrieval and tool call, each carrying inputs, outputs and timing.
  • On Databricks, the same instrumentation works for agents deployed anywhere. Set the tracking URI to Databricks and traces land in an MLflow experiment, with Unity Catalog storage recommended for production.

tools for genai request tracing

The tools you need for GenAI request tracing are an auto-instrumentation layer for your frameworks, a manual span API for the code those frameworks don't cover, and a way to attach request, user and session identifiers to each trace. Without all three, you get either gaps in the span tree or traces you can't connect back to a user complaint. MLflow Tracing, which is open source, covers the full set in one SDK, and the same code runs against a self-hosted MLflow server or Databricks.

What does request tracing capture?

A request trace follows one call through your application from input to output. MLflow captures "the inputs, outputs, and metadata associated with each intermediate step of a request". The @mlflow.trace decorator, for example, records the function name, inputs, outputs and execution time, and manages parent-child relationships and exception recording without extra code. Nested calls produce a tree: a root span for the request, then child spans for prompt building, retrieval, model calls and tool calls.

Choosing the right tracing tool for each part of a request

Tool When to use it Trade-off
mlflow.<library>.autolog() Framework and provider calls (LangChain, LangGraph, DSPy, LlamaIndex and others) Covers only what the integration patches
@mlflow.trace Your own functions: routing, pre- and post-processing, business logic Place it as the outermost decorator, or it may miss changes from inner decorators
mlflow.start_span() A block of code inside a function You set inputs and outputs yourself on the span object
mlflow.update_current_trace() Tags, metadata, user and session IDs on the current trace Must run inside a traced function
MlflowClient trace APIs Custom trace ID schemes or integration with an existing system Every start needs a matching end, and the docs say client APIs aren't thread-safe by default

Autologging and manual tracing compose. Enable autologging for each framework your agent uses and add @mlflow.trace to group those calls under one parent span. The client API is the exception: it doesn't interoperate with the decorator or mlflow.start_span(), so use one style per trace.

Tracing one request end to end

import mlflow
from mlflow.entities import SpanType

mlflow.langchain.autolog()  # framework calls become child spans

@mlflow.trace(span_type=SpanType.CHAIN)
def handle_request(question, user_id, session_id):
    mlflow.update_current_trace(
        metadata={"mlflow.trace.user": user_id, "mlflow.trace.session": session_id},
        tags={"environment": "production"},
    )
    with mlflow.start_span(name="build_prompt") as span:
        span.set_inputs({"question": question})
        prompt = f"Answer briefly: {question}"
        span.set_outputs(prompt)
    return agent.invoke(prompt)

trace = mlflow.get_trace(mlflow.get_last_active_trace_id())

The user and session metadata let you group multi-turn conversations and look up a request later with mlflow.search_traces(). To tie end-user ratings to the request, return the trace ID to the client and record the rating with mlflow.log_feedback(), which stores it as feedback on that trace. For services that call each other, MLflow documents propagating trace context across services so one request stays one trace.

How Databricks approaches request tracing

Instrumentation doesn't change on Databricks. An agent running outside Databricks sends traces to a workspace once you set these environment variables before the process starts:

export DATABRICKS_HOST="https://your-workspace.cloud.databricks.com"
export DATABRICKS_TOKEN="your-databricks-token"
export MLFLOW_TRACKING_URI=databricks
export MLFLOW_EXPERIMENT_NAME="/Shared/production-genai-agent"

For deployed agents, Databricks recommends the lightweight mlflow-tracing package. A Model Serving endpoint can write traces to Unity Catalog tables when it authenticates as a service principal with OAuth (or with a personal access token) and has the experiment ID in its MLFLOW_EXPERIMENT_ID environment variable. One documented gotcha: on Model Serving, set client_request_id as a tag, not as a metadata attribute, or trace export breaks.
Unity Catalog storage makes request traces queryable with SQL and governed with table permissions. At the model layer, Unity Gateway adds its own request records: usage tracking captures token usage and latency for model requests in the system.ai_gateway.usage system table, and inference tables log request and response payloads from model services to Unity Catalog Delta tables. Use traces for the full path of a request and gateway records for cross-team usage and spend. See manual tracing and Unity Gateway usage tracking.

FAQs

Do I have to change my code to trace an LLM framework?

Usually one line. Calling mlflow.<library>.autolog() patches the library so each LLM invocation, tool call and agent step is captured as a span. On Databricks serverless compute you must call it explicitly, because autologging isn't on by default there.

How do I find a specific user's request later?

Attach mlflow.trace.user and mlflow.trace.session metadata with mlflow.update_current_trace(), then filter with mlflow.search_traces() on those keys.

Can I trace TypeScript or JavaScript agents?

Yes. The mlflow-tracing npm package provides a function wrapper, a block-tracing API and a class method decorator for TypeScript.

Does tracing slow down requests?

MLflow logs traces asynchronously by default for OSS MLflow and for Databricks non-notebook workloads such as jobs and model serving. In Databricks notebooks, async logging is off until you set MLFLOW_ENABLE_ASYNC_TRACE_LOGGING=true.

The information provided herein is for general informational purposes only and may not reflect the most current product capabilities or configurations.