Custom metric
Custom Metric
Opik allows you to define your own custom metrics, which is especially important when the metrics you need are not already available out of the box.
When to Create Custom Metrics
It is specially relevant to define your own metrics when:
- You have domain-specific goals
- Standard metrics don’t capture the nuance you need
- You want to align with business KPIs
- You’re experimenting with new evaluation approaches
If you want to write an LLM as a Judge metric, you can use either the G-Eval metric or create your own from scratch.
Writing your own custom metrics
To define a custom metric, you need to subclass the BaseMetric class and implement the score
method and an optional ascore method:
The score method has access to the following parameters:
- Flattened dataset item: If your dataset item is of the format
{"input": "...", "expected_output": "..."}, the score method will receiveinputandexpected_outputparameters. - Task output: If your task output is of the format
{"output": "..."}, the score method will receive anoutputparameter. - Task span: If you define a parameter named
task_span, we will pass the full evaluation task trace to your score method. If you don’t need access to the trajectory data, we recommend not defining thetask_spanparameter.
The score method should return a ScoreResult object. The ascore method is optional and can be
used to compute asynchronously if needed.
You can also return a list of ScoreResult objects as part of your custom metric. This is useful if you want to
return multiple scores for a given input and output pair.
Now you can use the custom metric to score LLM outputs:
Also, this metric can now be used in the evaluate function as explained here: Evaluating LLMs.
Example: Accessing trajectory data in a custom metric
You can access the trajectory data in a custom metric by using the task_span parameter.
In order to access the full trajectory data, make sure you have integrated your evaluation task with Opik’s tracing features. You learn more about how to evaluate agent trajectories in the Evaluate Agent Trajectory guide.
Example: Creating a metric with OpenAI model
You can implement your own custom metric by creating a class that subclasses the BaseMetric class and implements the score method.
You can then use this metric to score your LLM outputs:
In this example, we used the OpenAI Python client to call the LLM. You don’t have to use the OpenAI Python client, you can update the code example above to use any LLM client you have access to.
Example: Adding support for many LLM providers
In order to support a wide range of LLM providers, we recommend using the litellm library to call your LLM. This allows you to support hundreds of models without having to maintain a custom LLM client.
Opik providers a LitellmChatModel class that wraps the litellm library and can be used in your custom metric:
You can then use this metric to score your LLM outputs:
Example: Creating a metric with multiple scores
You can implement a metric that returns multiple scores, which will display as separate columns in the UI when using it in an evaluation.
To do so, setup your score method to return a list of ScoreResult objects.
Example: Enforcing structured outputs
In the examples above, we ask the LLM to respond with a JSON object. However as this is not enforced, it is possible that the LLM returns a non-structured response. In order to avoid this, you can use the litellm library to enforce a structured output. This will make our custom metric more robust and less prone to failure.
For this we define the format of the response we expect from the LLM in the LLMJudgeBinaryResult class and pass it to the LiteLLM client:
Similarly to the previous example, you can then use this metric to score your LLM outputs:
Creating a custom metric using G-Eval
G-eval allows you to specify a set of criteria for your metric and it will use a Chain of Thought prompting technique to create some evaluation steps and return a score. You can read more about this advanced metric here.
To use G-Eval, you will need to specify a task introduction and evaluation criteria:
Custom Conversation Metrics
For evaluating multi-turn conversations and dialogue systems, you’ll need specialized conversation metrics. These metrics evaluate entire conversation threads rather than single input-output pairs.
Learn how to create custom conversation metrics in the Custom Conversation Metrics guide.
Using your custom metric in production
Custom metrics are not limited to offline evaluation. Once a custom metric scores your production traces, you can monitor it in the Opik dashboard and set up alerts on it, exactly like Opik’s built-in metrics. Alerts threshold on a feedback score name, and any score logged to a project is eligible — Opik does not care which system produced it.
There are two ways to get your metric’s scores onto production traces.
Run it as an online evaluation rule
Custom LLM as a Judge and custom Python code metrics can run automatically against production traces and threads. Each writes a feedback score under the name you give the rule. See Online evaluation rules.
Score in your own pipeline and push the result
Keep your metric code wherever you like — your application, a scheduled job, your CI — and attach the score to the trace, span or thread yourself:
log_spans_feedback_scores and log_threads_feedback_scores do the same for spans and threads, and opik_context.update_current_trace(feedback_scores=[...]) works inline inside a tracked function. From any other language, use the PUT /v1/private/traces/feedback-scores REST endpoint.
Use this path when you want full control over the metric logic, the model it uses (if any), and its cost.
Alerting on the score
Once scores are flowing, create an alert with the Trace feedback score threshold trigger (or Thread feedback score threshold for conversation-level metrics) and select your metric’s name under Feedback Score Name.
A score name appears in the Feedback Score Name dropdown only after at least one score with that name has been logged to the project. If you have just defined a custom metric, log one score before creating the alert.
Two things to keep in mind:
- The score must be attached to a trace, span or thread. Metrics computed purely offline as part of an experiment (via
evaluate()) cannot be thresholded — the only experiment-related alert event isexperiment:finished, which reports completion and carries no threshold. - Set the alert’s time window wider than your scoring cadence. Alerts compare the average score within the window, and a window containing no scores is skipped rather than fired. A nightly batch scoring job paired with a one-hour window will rarely fire.
What’s next
Creating custom metrics is just the beginning of building a comprehensive evaluation system for your LLM applications. In this guide, you’ve learned how to create custom metrics using different approaches, from simple metrics to sophisticated LLM-as-a-judge implementations, including specialized conversation thread metrics for multi-turn dialogue evaluation.
From here, you might want to:
- Evaluate your LLM application following the Evaluate your LLM application guide
- Evaluate conversation threads using the Evaluate Threads guide
- Explore built-in metrics in the Metrics overview
- Alert on your custom metric in production with feedback score alerts