> ## Documentation Index
> Fetch the complete documentation index at: https://docs.literalai.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Experiments

> Experiments enable continous improvement of your Prompt/Agent -- i.e. guarantee net improvements.

An experiment evaluates the performance of your **LLM system** (simple prompt or multi-step LLM chain/agent) against a **Dataset** and a set of **Evaluation Metrics**.

<Frame caption="Experiment distribution chart">
  <img src="https://mintcdn.com/chainlit-5/SattO4WPsa_7pLDy/images/experiment-overview.gif?s=5127a92324863b069e2c4366976c481d" alt="Experiment distribution chart" width="1497" height="772" data-path="images/experiment-overview.gif" />
</Frame>

## Run Experiments from Literal AI

Run an `Experiment` on a `Prompt` against a `Dataset` and a set of `Scorers` from Literal AI.

Experiments can be run directly from the Prompt Playground. This allows you to run experiments without having to manage an infrastructure.

<Steps>
  <Step title="Prompt to iterate on">
    Go to the Prompt Playground, make modifications to your prompt and vibe-check it.

    <Frame caption="Prompt to iterate on">
      <img src="https://mintcdn.com/chainlit-5/UKa5Vc5x7wx1EBq7/images/playground-start-from-example.gif?s=a7f54b983fc704a3817014ccbb8f67de" alt="Prompt to iterate on" width="1497" height="772" data-path="images/playground-start-from-example.gif" />
    </Frame>

    <Tip>
      If you struggle to start, select one of our examples from the top right corner.
    </Tip>
  </Step>

  <Step title="Pick a Dataset and select Scorers ">
    In the upper right corner, click "Experiment on Dataset".

    <Frame caption="Experiment on Dataset">
      <img src="https://mintcdn.com/chainlit-5/SattO4WPsa_7pLDy/images/experiment-dataset.gif?s=c0dddc3566b749cf6fa09b1c2e56edb3" alt="Experiment on Dataset" width="1497" height="772" data-path="images/experiment-dataset.gif" />
    </Frame>

    <Note>
      You should specify how to resolve prompt variables with your dataset `input`, `expectedOutput`
      and `metadata` columns. The `Scorer` configuration offers to
      use the prompt's completion through the `output` key.
    </Note>

    Running the experiment will redirect you to the Experiment details page, where you can track progress!
  </Step>
</Steps>

More Evaluators to come soon!

## Compare experiments

<Note>You can only compare two experiments if they were run on the same dataset.</Note>

<Frame caption="Comparing two experiments ran on the same dataset.">
  <img src="https://mintcdn.com/chainlit-5/SattO4WPsa_7pLDy/images/compare-experiments.gif?s=0997ac12f79b5d8c1ff7347ae4592a74" alt="Comparing two experiments ran on the same dataset." width="1497" height="772" data-path="images/compare-experiments.gif" />
</Frame>

## Run an experiment from your code

Complex multi-step LLM systems are heavily dependent on your code and instrastructure. Literal AI enables you to evaluate your LLM systems from your own code and then log the results on Literal AI.

Here is a naive example of how you can run an experiment with Literal AI:

<Note>See [installation](/get-started/quick-start) to get your API key and instantiate the SDK</Note>

<CodeGroup>
  ```python Python theme={null}
  inputs = [{"question": "question"}]

  experiment = literalai_client.api.create_experiment(
      name="Foo", params=[{"foo": "bar"}]  # optional
  )

  @literalai_client.run
  def my_agent(input):
      # Faking the agent response
      return {"content": "answer"}

  def score_output(output):
      # Faking the scoring
      return [{"name": "context_relevancy", "type": "AI", "value": 0.6}]

  @literalai_client.experiment_item_run
  def run_and_eval(input):
      output = my_agent(input)
      experiment_item = {
        "scores": score_output(output),
        "input": input,
        "output": output
      }
      experiment.log(experiment_item)

  def run_experiment(inputs):
      for input in inputs:
          run_and_eval(input)

  run_experiment(inputs)
  ```

  ```typescript TypeScript theme={null}
  async function myAgent(input: Record<string, unknown>) {
    // This is a fake agent
    return literalaiClient.step({ name: 'agent', type: 'run' }).wrap(async () => {
      return { answer: 'Fake Answer' };
    });
  }

  function scoreOutput(output: Record<string, unknown>) {
    // This is a fake scoring function
    return [
      {
        name: 'context_relevancy',
        type: 'AI' as const,
        value: 0.6
      }
    ];
  }

  async function main() {
    const experiment = await literalaiClient.api.createExperiment({
      name: 'Foo',
      params: { foo: 'bar' } // optional
    });

    const inputs = [{ question: 'Fake question' }];

    for (const input of inputs) {
      await literalaiClient.experimentItemRun().wrap(async () => {
        const agentOutput = await myAgent(input);
        const scores = scoreOutput(agentOutput);

        const experimentItem = {
          scores: scores,
          input: input,
          output: agentOutput
        };

        await experiment.log(experimentItem);
      });
    }
  }

  main();
  ```
</CodeGroup>

### Link to a Dataset

The best way to run an experiment is to use a [Dataset](/guides/dataset) to store your inputs and expected outputs. This way you can track on which data your experiment was run and compare the results of different experiments.

Using a dataset to run an experiment is very similar to the previous example, except that you are iterating over the items of the dataset:

<CodeGroup>
  ```python Python theme={null}
  dataset_id = "MY_DATASET_ID"

  dataset = literalai_client.api.get_dataset(dataset_id)

  experiment = literalai_client.api.create_experiment(
      dataset_id=dataset_id,
      name="Foo",
      params=[{"foo": "bar"}]  # optional
  )

  @literalai_client.experiment_item_run
  def run_and_eval(item):
      output = my_agent(item.input)
      experiment_item = {
        # Notice that the experiment item is now linked to the dataset item
        "datasetItemId": item.id,
        "scores": score_output(output),
        "input": item.input,
        "output": output
      }
      experiment.log(experiment_item)

  def run_experiment():
      for item in dataset.items:
          run_and_eval(item)
  ```

  ```typescript TypeScript theme={null}
  const datasetId = 'MY_DATASET_ID';
  const dataset = await literalAiClient.api.getDataset({id: datasetId});

  async function main() {
      const experiment = await literalaiClient.api.createExperiment({
          datasetId,
          name: 'Foo',
          params: { foo: 'bar' } // optional
      });

      for (const item of dataset.items) {
          await literalaiClient.experimentItemRun().wrap(async () => {
              const agentOutput = await myAgent(item.input);
              const scores = scoreOutput(agentOutput);

              const experimentItem = {
                  // Notice that the experiment item is now linked to the dataset item
                  datasetItemId: item.id,
                  scores: scores,
                  input: item.input,
                  output: agentOutput
              };

              await experiment.log(experimentItem);
          });
      }
  }
  ```
</CodeGroup>

### Link to a Prompt

If you are evaluating a prompt [living on Literal AI](/guides/prompts), you can bind it to the experiment to track the performance of the prompt.

<CodeGroup>
  ```python Python theme={null}
  prompt = literalai_client.api.get_prompt(name="MY_PROMPT", version=0)

  experiment = literalai_client.api.create_experiment(
      prompt_id=prompt.id,
      name="Foo",
      params=[{"foo": "bar"}]  # optional
  )

  # Run the experiment the same way as before
  ```

  ```typescript TypeScript theme={null}
  const promptName = 'MY_PROMPT';
  const promptVersion = 0
  prompt = literalAiClient.api.getPrompt(promptName, promptVersion);

  async function main() {
      const experiment = await literalaiClient.api.createExperiment({
          promptId: prompt.id,
          name: 'Foo',
          params: { foo: 'bar' } // optional
      });
      // Run the experiment the same way as before
  }
  ```
</CodeGroup>
