06 Experiments
Workshop source
Workshop material is maintained in the public langfuse/langfuse-workshop repository. Use the repository for the runnable app, checkpoint branches, and local setup.
Why experiments
A trace tells you about one turn. An experiment tells you about behavior across the dataset. Every experiment run does the same three things:
- Pulls each item from the dataset.
- Runs the item's input through the agent: same
runSupportConversation(...)the web app uses, so the trace shape is the same as production. - Scores the actual output with one or more evaluators, either against the expected output or on a reference-free quality criterion.
Complementary materials:
- Docs: Experiments via SDK
- Academy: Experiments and Evaluate
Goal
By the end of this chapter:
- You can run the full dataset against the agent on demand.
- Every item gets a
behavior_matchscore (decision model + deterministic match) and acorrectnessscore (LLM-as-a-judge regression check), both fromrunExperimentcallbacks. - Both scores plus the per-item traces are visible in Langfuse and ready to compare against future runs.
Starting point
git checkout checkpoint/06-experimentsYour dataset is seeded in Langfuse. scripts/run-dataset.ts is already in the repo.
Step 1: Choose the evaluators
In 05 you decided what the dataset should evaluate: correct answers, and the right behavior (reply, defer, or ask a follow-up). Each goal gets one evaluator, and each reads its own field from expectedOutput:
behavior_match(decision model + deterministic match): did Specs reply, defer, or ask a follow-up when we expected it to? The OpenAI Decisions API picks one of three fixed labels, and the script compares that label withexpectedBehavior.correctness(LLM-as-a-judge regression check): is the answer still correct, or did a change make it worse? A semantic-equivalence check againstidealAnswer: paraphrases pass, missing required detail or a contradicted fact fails. A prompt fix that makes the agent refuse too much, or drop a step, shows up here.
Treat this set as a starting point: the simplest check for each goal from 05. Impersonation and lengthy replies get no evaluator here, so read traces for those, and add one (for example a word-count check) if they become worth tracking.
Step 2: Understand the run script
Open scripts/run-dataset.ts. The file is annotated with numbered comments (// --- 1. Boot the OpenTelemetry SDK ..., // --- 3. The behavior_match evaluator ..., etc.) so you can read it section by section. At a high level:
- Loads the hosted dataset from Langfuse by
DATASET_NAME. - For each item, calls the same
runSupportConversation(...)the web app uses. - Uses
dataset.runExperiment(...)to roll all per-item traces into a single run row. - Attaches
behavior_matchandcorrectnessas callback evaluators on that same call.
The traces produced are the same shape as production traces: same dad-it-support-chat-turn root, same OpenAI generation, same tool spans. No extra Langfuse UI setup is required for any of the scores.
Both scores are callback evaluators inside runExperiment. They run in your process right after each item finishes, so the console summary and the Langfuse run already include every score when the script exits. We intentionally avoid Langfuse Platform evaluators in this chapter: configuring one needs existing experiment data to preview and map variables, which creates a chicken-and-egg problem before your first run.
The moving parts of the run
The whole run is one call to runExperiment. The shape boils down to:
await dataset.runExperiment({
name: "Dad IT Support Agent experiment",
runName, // unique label for this run; shows up in the Experiments tab
description: "...",
metadata: { model: env.openaiModel },
maxConcurrency: 1, // run items one at a time
task: async (item) => {
const response = await runSupportConversation({ /* item.input */ });
return response.answer;
},
evaluators: [
async ({ input, output, expectedOutput }) => ({
name: "behavior_match",
value: /* 1 if decideBehavior(...) === expectedOutput.expectedBehavior, else 0 */,
comment: "..."
}),
async ({ output, expectedOutput }) => ({
name: "correctness",
value: /* 0 or 1 from the LLM judge */,
comment: "..."
})
]
});Three things to understand:
taskis your application logic: we call straight intorunSupportConversation(...), which means every trace this script produces looks identical to a production trace.evaluatorsis a list of callbacks. Each evaluator runs aftertaskreturns and attaches a score to the item trace. Here we use one behavior check and one LLM-as-a-judge regression check.runNamegroups every per-item trace into one row in the Langfuse Experiments view. Pick a name that changes per run (we include the timestamp) so two runs don't collide.
Step 3: Review the behavior_match evaluator
behavior_match has two parts:
- A decision model.
decideBehavior(...)sends the conversation plus the agent's answer to the OpenAI Decisions API (POST /v1/decisions, modelgpt-6-luna) as onechoicequestion. The API can only answer with one of the supplied values,reply,defer_question, orask_follow_up, and returns a confidence alongside it. - A deterministic match. The callback compares that label with the item's
expectedBehavior:1on a match,0otherwise. The comment records both labels and the confidence, for exampleExpected defer_question, got reply (confidence 0.85).
Why split it this way?
- The model only does the part that needs judgement: labelling the answer. It never sees the expected label, so it can't be nudged towards it.
- The score itself is a plain equality check, so it is easy to read and to trust.
- Decisions returns a bounded answer fast and cheaply, so this second model call barely adds to the run time.
Low-confidence labels are worth reading by hand. They usually mean the answer mixes behaviors, for example declining an out-of-scope request and then walking Dad through it anyway.
Step 4: Review the correctness regression check
The last callback in evaluators calls OpenAI with a semantic-equivalence judge prompt. It treats idealAnswer as the source of truth and asks whether the agent answer preserves every material meaning, fact, and constraint: paraphrases are fine; missing required detail or a contradicted fact is not. The callback maps the judge's true/false result to a correctness score of 0 or 1 plus a short reasoning comment.
The judge uses your existing OPENAI_API_KEY / OPENAI_MODEL from .env: the same credentials the agent already uses. behavior_match uses the same OPENAI_API_KEY, with gpt-6-luna fixed in the script. You do not need the Langfuse-side default evaluator model from session 4 for this chapter.
Step 5: Run the dataset
npm run dataset:runThe script finishes by printing a formatted run summary in the console. Item-level traces and both scores show up in Langfuse as the run executes. Because the evaluators are callbacks, you should already see behavior_match and correctness in that console summary, not as a later async fill-in.
This first run is your baseline experiment. It uses the production dad-it-support-agent prompt from monitoring, so expect misses on the out-of-scope and vague items, including out-of-scope questions that come up mid-conversation. In 07 you compare it against the prompt suggested at the end of error analysis.
What to inspect in Langfuse
- The new Run under your dataset → one row per item with two scores:
behavior_matchandcorrectness, plus a trace link. - Item-level traces: identical shape to production traces.
- The dataset's chart view → per-run averages for every score, ready for side-by-side comparison after future changes.
How to verify you are done
- One run row appears under the dataset.
- Every item has a trace and both scores attached.
- The console summary printed both
behavior_matchandcorrectness. - Trace shape matches a normal production trace.
Wrap-up
The two scores match the two goals from 05: behavior match for "did Specs reply, defer, or ask a follow-up when it should?" and correctness for "is the answer still correct, or did the change make it worse?" Both run as callbacks on runExperiment, so the first workshop run is fully scored without platform evaluator setup.
They are a starting point. The suite grows with your error analysis: each new failure category you decide to track becomes a dataset item and, where needed, an evaluator.
The Langfuse skill (/langfuse) knows the recommended evaluator shapes and setup patterns: this walkthrough exists so you see what the skill is doing under the hood.
End state
This is the starting point for 07-evaluation.