Skip to main content
You can run experiments programmatically using the Traceloop SDK. This allows you to systematically evaluate different AI model configurations, prompts, and approaches with your datasets.

SDK Initialization

First, initialize the Traceloop SDK.
Prerequisites: You need an API key set as the environment variable TRACELOOP_API_KEY. Generate one in Settings →

Basic Experiment Structure

An experiment consists of:
  • A dataset to test against
  • A task function that defines what your AI system should do
  • Evaluators to measure performance

Task Functions

Create a task function that define how your AI system processes each dataset row. The task is one of the experiments parameters, it will run it on each dataset row. The task function signature expects:
  • Input: An optional dictionary containing the dataset row data
  • Output: A dictionary with your task results
A task can perform any desired internal operation—such as LLM calls, semantic search, RAG, or custom business logic. The task output is then used as the evaluator input and is automatically logged in the Traceloop platform.
Ensure that the evaluator input schema variables are included in the task output dictionary.
You can add extra attributes to the task output even if they are not evaluator input parameters—these will also be logged to the platform.

Running Experiments

Use the experiment.run() method to execute your experiment by selecting a dataset as the source data, choosing the evaluators to run, and assigning a slug to make it easy to rerun later.

experiment.run() Parameters

  • dataset_slug (str): Identifier for your dataset
  • dataset_version (str): Version of the dataset to use, experiment can only run on a published version
  • task (function): Async function that processes each dataset row
  • evaluators (list): List of evaluator slugs to measure performance
  • experiment_slug (str): Unique identifier for this experiment
  • stop_on_error (boolean): Whether to stop on first error (default: False)
  • wait_for_results (boolean): Whether to wait for async tasks to complete, when not waiting the results will be found in the ui (default: True)

Comparing Different Approaches

You can run multiple experiments to compare different approaches—whether by using different datasets, trying alternative task functionality, or testing variations in prompts, models, or business logic.

Full Examples

For complete, working examples that you can run and modify:

Python Example

TypeScript Example