> For the complete documentation index, see [llms.txt](https://docs.aporia.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aporia.com/ml-monitoring-as-code/querying-metrics.md).

# Querying metrics

To query metrics from Aporia, initialize a new client and call the `query_metrics` API:

```python
from datetime import datetime, timedelta
from aporia import (
    Aporia,
    MetricDataset,
    MetricParameters,
    TimeRange,
)
from aporia.sdk.datasets import DatasetType

aporia_token = os.environ["APORIA_TOKEN"]
aporia_account = os.environ["APORIA_ACCOUNT"]
aporia_workspace = os.environ["APORIA_WORKSPACE"]

aporia_client = Aporia(
    base_url="https://platform.aporia.com",  # or "https://platform-eu.aporia.com"
    token=aporia_token,
    account_name=aporia_account,
    workspace_name=aporia_workspace,
)

last_week_dataset = MetricDataset(
    dataset_type=DatasetType.SERVING,
    time_range=TimeRange(
        start=datetime.datetime.now() - datetime.timedelta(days=7),
        end=datetime.datetime.now(),
    ),
)

res = metrics.query_batch(
    model_id=model_id,
    metrics=[
        MetricParameters(
            dataset=last_week_dataset,
            name="count",
        ),
    ],
)

print(f"The model had {metrics[0]} predictions last week")

```

## Parameters

The `query_metrics` API has the following parameters:

<table><thead><tr><th width="153">Parameter</th><th width="280">Type</th><th>Description</th></tr></thead><tbody><tr><td>model_id</td><td><code>str</code></td><td>Model ID to query metrics for.</td></tr><tr><td>metrics</td><td><code>List[MetricParameters]</code></td><td>List of metrics to query. </td></tr></tbody></table>

The API can request values for multiple metrics concurrently.&#x20;

### MetricParameters

Here are different fields for the `MetricParameters` object:

<table><thead><tr><th width="186">Field</th><th width="178.33333333333331">Type</th><th>Description</th></tr></thead><tbody><tr><td>name</td><td><code>str</code></td><td>Metric name (see <a data-mention href="/pages/t1YWJC1qqqXyXJYBf4u8#supported-functions">/pages/t1YWJC1qqqXyXJYBf4u8#supported-functions</a>). Required.</td></tr><tr><td>dataset</td><td><code>MetricDataset</code></td><td>Specifies what data to query (training / serving), what segment, and what timeframe. Required.</td></tr><tr><td>column</td><td><code>str</code></td><td>Name of the column to calculate the metric for.<br><br>Required except for the <code>count</code> metric. For performance metrics, this should be the name of the <em>prediction</em>, not the actual.</td></tr><tr><td>k</td><td><code>int</code></td><td>K value for ranking metrics such as nDCG.<br><br>Required only for <code>ndcg_at_k</code>, <code>map_at_k</code>, <code>mrr_at_k</code>, <code>accuracy_at_k</code>, <code>precision_at_k</code>, and <code>recall_at_k</code>. </td></tr><tr><td>threshold</td><td><code>float</code></td><td><p>Threshold to use when calculating binary performance metrics. </p><p></p><p>Required only if the prediction is <code>numeric</code> and the actual is <code>boolean</code>, and the metric is a binary performance metric such as <code>accuracy</code>, <code>recall</code>, <code>precision</code>, <code>f1_score</code>, etc.</p></td></tr><tr><td>custom_metric_id</td><td><code>str</code></td><td><p>Custom metric ID.</p><p></p><p>Required only if you want to query a custom metric.</p></td></tr><tr><td>baseline</td><td><code>MetricDataset</code></td><td><p>Specifies what data to use as baseline. </p><p></p><p>Required only for statistical distances such as <code>js_distance</code>, <code>ks_distance</code>, <code>psi</code>, and <code>hellinger_distance</code>. </p></td></tr></tbody></table>

### MetricDataset

The `MetricDataset` object contains the following fields:

<table><thead><tr><th width="159.33333333333331">Field</th><th width="174">Type</th><th>Description</th></tr></thead><tbody><tr><td>dataset_type</td><td><code>DatasetType</code></td><td>Can be either <code>DatasetType.SERVING</code> or <code>DatasetType.TRAINING</code>. Required.</td></tr><tr><td>time_range</td><td><code>TimeRange</code></td><td>Time range (contains <code>start</code> and <code>end</code> fields). Do not pass this for training.</td></tr><tr><td>model_version</td><td><code>str</code></td><td>Model version to filter by. Optional.</td></tr><tr><td>segment</td><td><code>MetricSegment</code></td><td>Used to query metrics in a specific data segment. Contains <code>id</code> and <code>value</code> fields.</td></tr></tbody></table>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
