Tracing a GenAI App (Notebook)
This quickstart helps you integrate your GenAI app with MLflow Tracing if you use a Jupyter Notebook as your development environment. If you use a local IDE, please use the IDE quickstart instead.
What you'll achieve
By the end of this tutorial, you will have:
- A Jupyter Notebook with a linked MLflow Experiment for your GenAI app
- Used MLflow Tracing to instrument your app.
Prerequisites
- Python Environment: Python 3.8+ with pip installed
- Jupyter Notebook: Access to Jupyter Notebook or JupyterLab
- OpenAI API Key: Access to OpenAI API
Step 1: Create a Jupyter Notebook
Creating a Jupyter Notebook and setting up an MLflow Experiment will be the container for your GenAI application. Learn more about the Experiment and what it contains in the data model section.
- Open Jupyter Notebook or JupyterLab
- Create a new notebook
- Create your first cell to set up the environment
Step 2: Install and configure MLflow
Install the latest version of MLflow for the best experience with GenAI capabilities, including the most comprehensive tracing features and robust support.
You can install MLflow in your notebook by running:
!pip install --upgrade "mlflow>=3.1" openai python-dotenv
While tracing features are available in MLflow 2.15.0+, it is strongly recommended to install MLflow 3 (specifically 3.1 or newer) for the latest GenAI capabilities, including expanded tracing features and robust support.
Step 3: Instrument your application
Select the appropriate integration for your application:
OpenAI Integration
-
Create a cell in your notebook to configure your environment and MLflow:
import mlflow
import openai
from openai import OpenAI
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Set up MLflow experiment
mlflow.set_experiment("genai-tracing-quickstart") -
Create a cell in your notebook with the below code
Here, we use the
@mlflow.trace
decorator that makes it easy to trace any Python application combined with the OpenAI automatic instrumentation to captures the details of the call to the OpenAI SDK.# Enable MLflow's autologging to instrument your application with Tracing
mlflow.openai.autolog()
# Create an OpenAI client
client = OpenAI(
api_key=os.getenv("OPENAI_API_KEY") # Set your OpenAI API key in environment
)
# Use the trace decorator to capture the application's entry point
@mlflow.trace
def my_app(input: str):
# This call is automatically instrumented by `mlflow.openai.autolog()`
response = client.chat.completions.create(
model="gpt-3.5-turbo", # You can replace with gpt-4, gpt-4-turbo, etc.
messages=[
{
"role": "system",
"content": "You are a helpful assistant.",
},
{
"role": "user",
"content": input,
},
],
)
return response.choices[0].message.content
my_app(input="What is MLflow?") -
Run the Notebook cell to execute the code
Alternative: Other LLM Providers
# For other providers, you can use manual tracing
import mlflow
@mlflow.trace
def my_custom_app(input: str):
# Your custom LLM integration here
# This could be Anthropic, Hugging Face, local models, etc.
with mlflow.start_span(name="llm_call") as span:
span.set_inputs({"input": input})
# Your LLM call here
response = "Your LLM response"
span.set_outputs({"response": response})
return response
my_custom_app(input="What is MLflow?")
Step 4: View the Trace in MLflow
-
Start the MLflow UI by running in your terminal:
mlflow ui
-
Open your browser to
http://localhost:5000
-
Navigate to your experiment: "genai-tracing-quickstart"
-
You will now see the generated trace in the Traces tab
-
Click on the trace to view its details
Understanding the Trace
The trace you've just created shows:
- Root Span: Represents the inputs to the
my_app(...)
function- Child Span: Represents the OpenAI completion request
- Attributes: Contains metadata like model name, token counts, and timing information
- Inputs: The messages sent to the model
- Outputs: The response received from the model
This simple trace already provides valuable insights into your application's behavior, such as:
- What was asked
- What response was generated
- How long the request took
- How many tokens were used (affecting cost)
Next Steps
Congratulations! You've successfully built your first GenAI application with MLflow Tracing and OpenAI.
- Learn more about Tracing: Learn about tracing concepts, manual tracing for more control, tracking users & sessions, and tracking environments & context to your traces.
- Evaluate your application: Use MLflow's evaluation capabilities to measure the quality of your GenAI app.
For more complex applications like RAG systems or multi-step agents, MLflow Tracing provides even more value by revealing the inner workings of each component and step.