सामग्री पर जाएँ

लेसन 19 · Tracing and LLM Apps

Auto Tracing, Feedback and Production

Turn on MLflow auto tracing for LLM libraries, group traces by user and session, attach feedback, redact data and control sampling in production.

Manual spans are flexible, but you do not need to wrap every model call yourself. This lesson adds auto tracing, then covers what changes when real users arrive.

Auto tracing

For supported libraries, one line records calls automatically. For the Claude Python SDK:

Python
import mlflow
import anthropic

mlflow.set_experiment("support-bot")
mlflow.anthropic.autolog()

client = anthropic.Anthropic()          # reads ANTHROPIC_API_KEY
message = client.messages.create(
    model="claude-opus-5-5",
    max_tokens=2000,
    messages=[{"role": "user", "content": "Explain what a return policy is."}],
)
print(message.stop_reason)
print("".join(b.text for b in message.content if b.type == "text"))

The integration records prompts, responses, latency, the model name, token usage, tool-call output and errors. As of December 2026, the MLflow docs note limits for this integration: streaming calls are traced without token usage and cost tracking, and batch requests are not supported. Check the integration page for your provider. Similar functions exist for others, such as mlflow.openai.autolog().

You can combine auto tracing with your own @mlflow.trace spans. The model calls then appear inside the tree of your app.

Users and sessions

A conversation has many requests. Add user and session IDs to traces to group them. In MLflow 3.11.0 and later:

Python
@mlflow.trace
def chat(message, session_id, user_id):
    mlflow.update_current_trace(session_id=session_id, user=user_id)
    return reply_to(message)

You can then filter with mlflow.search_traces, using a condition on metadata.`mlflow.trace.session` . Use internal IDs, not names or email addresses.

Feedback

A trace tells you what happened. Feedback says whether it was good:

Python
from mlflow.entities import AssessmentSource, AssessmentSourceType

mlflow.log_feedback(
    trace_id=trace_id,
    name="helpful",
    value=False,
    rationale="Did not mention the 30-day limit.",
    source=AssessmentSource(
        source_type=AssessmentSourceType.HUMAN, source_id="reviewer-17"
    ),
)

Sources can be a human, code or an LLM judge. Expectations record what the correct answer should be, and you can log them with mlflow.log_expectation. A failed trace with a good expectation makes an excellent new test case.

Redact before export

Register span processors that clean spans on the client side, before data leaves your app:

Python
import mlflow

def redact(span):
    ...   # edit span inputs and outputs in place

mlflow.tracing.configure(span_processors=[redact])

The stand-in below tests the kind of cleaning function you might write.

Python Stand-in: redact obvious personal data

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that the name "Sam" survives. Pattern rules catch only what they describe. For broader coverage, the docs show how to use a dedicated library for personal data detection. Test redaction with realistic examples, and do not log data you do not need.

Production settings

  • Lighter package. The mlflow-tracing package has minimal dependencies for production apps. The docs advise against installing it together with the full mlflow package.
  • Sampling. MLFLOW_TRACE_SAMPLING_RATIO sets the share of traces that are exported, and the default is 1.0. You can override it for one root function with @mlflow.trace(sampling_ratio_override=0.2).
  • Async logging. The docs say asynchronous trace logging is on by default for open source MLflow. Its queue has a limit, and new traces can be discarded when it is full.
  • Retention. Decide how long traces live and who may read them.

Sampling and full queues mean that some requests leave no trace. If you must keep a record of every action, for example for compliance, build that in a separate, reliable system.

A review routine

  1. Filter for errors, slow traces and negative feedback.
  2. Read a small sample every week.
  3. Group failures by cause.
  4. Add examples to your evaluation dataset.
  5. Fix, re-evaluate and release.

Collect feedback from real users

A thumbs-up button is a start, but it gives little information. When a user marks an answer as unhelpful, offer an optional short reason. Store the trace ID with the answer in your application, so that you can attach the feedback to the right trace later.

User feedback is noisy. Unhappy users may respond more often than happy ones, and a bad rating may reflect a policy that the user dislikes rather than a wrong answer. Treat it as a signal for review, not as a final score.

Privacy by design

Before you turn on tracing for real users, answer these questions:

  1. What data can appear in a trace?
  2. Do you have a legal basis and a clear notice for storing it?
  3. Who can read traces, and is access logged?
  4. How long do you keep them?
  5. How do you delete a user's data when asked?

The last question is easy to forget. If you tag traces with an internal user ID, you can find the relevant ones later. Rules on personal data differ between countries, so ask your legal or privacy team. This tutorial does not give legal advice.

Watch the volume

Every request can create several spans, and long conversations repeat earlier messages in each model call. Storage grows faster than people expect. Measure the volume during a pilot, then set sampling and retention. If you sample, consider keeping all traces with errors by a separate mechanism, and test that it behaves as you intend, because sampling decides per trace which ones are exported.

When tracing fails

Tracing should not break your app. Test what happens when the tracking server is slow or unreachable. Confirm that user requests still work, and set up an alert when traces stop arriving. Silent loss of traces can otherwise go unnoticed until the day you need them.

Common mistakes

Do not assume that auto tracing records everything in your app. It covers the calls of the supported library. Your own retrieval code, database queries and business rules need manual spans. Also check that your redaction processor handles every place where text can appear, including tool results.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. What does mlflow.anthropic.autolog() mainly do?

    It records calls made through the SDK as traces.
  2. 2. When should sensitive data be redacted?

    Processing before export keeps the sensitive data inside your application.
  3. 3. Why should traces not be your only audit log?

    Traces are for observability and may be incomplete.

Summary

  • Auto tracing records calls from supported libraries.
  • Add session and user IDs, and attach feedback with its source.
  • Redact on the client before export.
  • Tune sampling, and remember that traces may be incomplete.

Next: Version Prompts in the Prompt Registry

आपकी प्रगति इसी ब्राउज़र में सेव होती है। खाते की ज़रूरत नहीं है।