# Custom metrics

This guide will show you how to automatically add custom metrics to your model from code using the Python SDK.&#x20;

For more information on the syntax of custom metrics, check the [Custom Metric Syntax](/api-reference/custom-metric-syntax.md) documentation.

## Defining Custom Metrics

To add new custom metrics:

```python
conversion_rate = aporia.CustomMetric(
    "Conversion Rate",
    code="""
        count(filter="purchased=TRUE") / count()
    """,
)

model_revenue = aporia.CustomMetric(
    "Model Revenue",
    code="""
        sum(
            column="candidate_item_price",
            filter="purchased=TRUE AND ranking_index<=4"
        ) / count()*2
    """,
)
```

In this example, we're adding two new custom metrics - **conversion rate** and **model revenue**. To add the custom metrics to your model, pass them to the model object:

```python
model = aporia.Model(
    "My Model",
    type=aporia.ModelType.RANKING,
    versions=[model_version],
    segments=[platform_segment, country_segments],
    custom_metrics=[conversion_rate, model_revenue]
)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.aporia.com/ml-monitoring-as-code/custom-metrics.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
