# Write an eval

Write an eval that runs an agent or a tool on test cases and scores each answer with metrics. Run it after every change to catch a worse answer before you publish.

## Before you start

You need an agent. See [Create an agent](/docs/agents/create-agent.md).

## Write an eval

**Studio**

1. In your project, open **Building blocks** > **Evals**.
2. Click **Create eval**.
3. Enter a **Name**.
4. Under **Target**, pick the agent.
5. Under **Add dataset**, keep the first test case: `input` "Capital of France?", `reference` "Paris".
6. Under **Add metrics**, keep the first metric: the answer contains "Paris".
7. Click **Create eval**.
8. Under **Recent runs**, click **Run eval**.
9. When the run finishes, click **Open report**. You should now see **Passed** `1/1`.

### Troubleshooting

**If the run fails with "Eval not found: eval:…"**, an eval made in Studio can't run yet. Create it from the API tab, then run it here.

**Code**

1. In a project from the AI Agent template (see [Build your first agent](/docs/start/first-agent.md)), create an eval for its `assistant`. The file name becomes its ID, so this one is `divide`:

```ts title="evals/divide.eval.ts"
import { datasets, evalAgent, metrics } from 'veryfront/eval'

export default evalAgent({
  name: 'Divide',
  target: 'agent:assistant',
  dataset: datasets.inline([{ id: 'divide', input: 'What is 128 divided by 8?' }]),
  metrics: [metrics.agent.calledTool('calculator').gate(), metrics.answer.contains({ text: '16' }).gate()],
})
```

2. Run it:

```bash
npm run eval -- divide
```

3. Read the result. You should now see `1/1 passed`.

### Try it in a sandbox

```playground
step: Create the project | npm create -y veryfront@latest my-agent -- --template ai-agent --yes
step: Go into it | cd my-agent
step: Create the eval | mkdir -p evals && printf '%s\n' "import { datasets, evalAgent, metrics } from 'veryfront/eval'" '' 'export default evalAgent({' "  name: 'Divide'," "  target: 'agent:assistant'," "  dataset: datasets.inline([{ id: 'divide', input: 'What is 128 divided by 8?' }])," "  metrics: [metrics.agent.calledTool('calculator').gate(), metrics.answer.contains({ text: '16' }).gate()]," '})' > evals/divide.eval.ts
check: npm run eval -- divide
expect: 1/1 passed
```

**API**

1. Set `PROJECT` to your project's ID ([Find your IDs](/docs/authentication/api-keys.md#find-your-ids)): `export PROJECT=<id>`.
2. Create the eval for your agent, here `support`, with your [API key](/docs/authentication/api-keys.md):

```bash
curl -X POST https://api.veryfront.com/projects/$PROJECT/evals \
  -H "Authorization: Bearer $VERYFRONT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Capitals",
    "target": "agent:support",
    "dataset": {
      "kind": "inline", "editable": true, "dynamic": false,
      "examples": [{ "id": "q1", "input": "Capital of France?", "reference": "Paris" }]
    },
    "metrics": [{
      "name": "answer.contains", "family": "answer", "severity": "gate",
      "config": { "text": "Paris" }, "editable": true, "dynamic": false
    }]
  }'
```

3. Set `EVAL_ID` to the `id` in the response, `eval:capitals`: `export EVAL_ID=eval:capitals`.
4. Run it:

```bash
curl -X POST https://api.veryfront.com/runs \
  -H "Authorization: Bearer $VERYFRONT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "project_id": "'$PROJECT'",
    "target": { "type": "task", "id": "eval" },
    "config": { "eval_id": "'$EVAL_ID'" },
    "execution": { "runtime": { "type": "main_branch" } }
  }'
```

5. List the eval's runs until the first one's `status` is `completed`. You should now see `"passed": 1` in its `summary`:

```bash
curl https://api.veryfront.com/projects/$PROJECT/evals/$EVAL_ID/runs \
  -H "Authorization: Bearer $VERYFRONT_API_KEY"
```

### Try it in a sandbox

```playground
step: Set PROJECT | export PROJECT=$VERYFRONT_PROJECT_ID
step: Add the support agent | curl -fsS -X POST https://api.veryfront.com/projects/$PROJECT/agents -H "Authorization: Bearer $VERYFRONT_API_KEY" -H "Content-Type: application/json" -d '{ "id": "support", "name": "Support assistant", "system_prompt": "Answer questions in one short sentence." }'
step: Create the eval | curl -X POST https://api.veryfront.com/projects/$PROJECT/evals -H "Authorization: Bearer $VERYFRONT_API_KEY" -H "Content-Type: application/json" -d '{ "name": "Capitals", "target": "agent:support", "dataset": { "kind": "inline", "editable": true, "dynamic": false, "examples": [{ "id": "q1", "input": "Capital of France?", "reference": "Paris" }] }, "metrics": [{ "name": "answer.contains", "family": "answer", "severity": "gate", "config": { "text": "Paris" }, "editable": true, "dynamic": false }] }'
step: Set EVAL_ID | export EVAL_ID=eval:capitals
step: Run it | curl -X POST https://api.veryfront.com/runs -H "Authorization: Bearer $VERYFRONT_API_KEY" -H "Content-Type: application/json" -d '{ "project_id": "'$PROJECT'", "target": { "type": "task", "id": "eval" }, "config": { "eval_id": "'$EVAL_ID'" }, "execution": { "runtime": { "type": "main_branch" } } }'
step: Wait until it completes | for i in $(seq 60); do curl -s https://api.veryfront.com/projects/$PROJECT/evals/$EVAL_ID/runs -H "Authorization: Bearer $VERYFRONT_API_KEY" | grep -q '"status":"completed"' && break; sleep 3; done
check: curl https://api.veryfront.com/projects/$PROJECT/evals/$EVAL_ID/runs -H "Authorization: Bearer $VERYFRONT_API_KEY"
expect: "passed":1
```

## With MCP

1. Call `veryfront__create_eval`, with your project's slug or ID as `project_reference`:

```json
{
  "project_reference": "my-project",
  "name": "Capitals",
  "target": "agent:support",
  "dataset": {
    "kind": "inline",
    "editable": true,
    "dynamic": false,
    "examples": [{ "id": "q1", "input": "Capital of France?", "reference": "Paris" }]
  },
  "metrics": [{
    "name": "answer.contains",
    "family": "answer",
    "severity": "gate",
    "config": { "text": "Paris" },
    "editable": true,
    "dynamic": false
  }]
}
```

2. Call `veryfront__run_eval` with `"project_reference": "my-project"` and `"eval_id": "eval:capitals"`. Keep the `run_id` it returns.
3. Call `veryfront__list_eval_runs` with `"project_reference": "my-project"` and `"eval_id": "eval:capitals"` until the first run's `status` is `completed`.
4. Call `veryfront__get_eval_run` with the same `project_reference` and `eval_id`, and the `run_id` from step 2. It worked when its `summary` shows `"passed": 1`.

## Next steps

* [Run an eval](/docs/agents/run-eval.md)
* [Debug a run](/docs/observability/debug.md)
