Custom Metrics
Custom metrics allow you to visualize your own metrics on a Deployment. You define the metric's name, axis labels, and chart type, then push data points whenever you have new values to record.
Creating a Custom Metric
Use create_custom_metric to register a new metric on a Deployment. You must supply a GraphType (line or bar) and axis labels. Axis units are optional.
from deeploy.models import CreateCustomMetric
from deeploy.enums import GraphType
metric = client.create_custom_metric(
deployment_id,
CreateCustomMetric(
name='Daily Token Usage',
graph_type=GraphType.LINE,
title_x_axis='Date',
title_y_axis='Total tokens',
unit_x_axis='day',
)
)
print(metric.id) # save this to add data points later
Adding Data Points
Use create_custom_metric_data_points to push one or more data points to an existing metric. Each data point has an x_value (a date string or number) and a y_value (a float). You can optionally associate a list of prediction_log_ids with a data point for traceability.
from deeploy.models import CreateCustomMetricDataPoint
custom_metric_id = metric.id # from the create step above
data_points = [
CreateCustomMetricDataPoint(x_value='2026-03-06', y_value=312.4),
CreateCustomMetricDataPoint(x_value='2026-03-07', y_value=289.1),
]
client.create_custom_metric_data_points(deployment_id, custom_metric_id, data_points)
Example: LLM Token Usage per Day
The following example fetches request logs for the past 7 days from an OpenAI schema compatible external Deployment, extracts the total_tokens field from each LLM response body, computes the daily total, and submits those totals as data points to a custom metric.
Request logs must be fetched with include_raw_body=True so that the raw LLM response — including token usage — is returned.
Example implementation
from collections import defaultdict
from datetime import datetime, timedelta, timezone
from deeploy import Client
from deeploy.models import CreateCustomMetric, CreateCustomMetricDataPoint, GetRequestLogsOptions
from deeploy.enums import GraphType
token = "DPT..."
deployment_id = "..."
workspace_id = "..."
client = Client(
# if using the Deeploy cloud version 'host' is 'app.deeploy.ml'
host="app.deeploy.ml",
workspace_id=workspace_id,
access_key="...",
secret_key="..."
)
# --- 1. Create the custom metric ---
metric = client.create_custom_metric(
deployment_id,
CreateCustomMetric(
name='Daily Token Usage',
graph_type=GraphType.LINE,
title_x_axis='Date',
title_y_axis='Total tokens',
unit_x_axis='day',
),
)
# --- 2. Determine the time window: last 7 complete days ---
now = datetime.now(tz=timezone.utc)
end_of_today = now.replace(hour=0, minute=0, second=0, microsecond=0) + timedelta(days=1)
start_of_window = end_of_today - timedelta(days=7)
start_ms = int(start_of_window.timestamp() * 1000)
end_ms = int(end_of_today.timestamp() * 1000)
# --- 3. Page through all request logs in the window ---
PAGE_SIZE = 100
offset = 0
all_logs = []
while True:
page = client.get_request_logs(
deployment_id,
params=GetRequestLogsOptions(
start=start_ms,
end=end_ms,
limit=PAGE_SIZE,
offset=offset,
),
include_raw_body=True,
)
all_logs.extend(page)
if len(page) < PAGE_SIZE:
break
offset += PAGE_SIZE
# --- 4. Group total_tokens by calendar day ---
# This example assumes LLM deployments that return an OpenAI-compatible response body with a
# top-level "usage" object: { "prompt_tokens": int, "completion_tokens": int, "total_tokens": int }
tokens_by_day: dict[str, list[int]] = defaultdict(list)
for log in all_logs:
if log.raw_response_body is None:
continue
usage = log.raw_response_body.get('usage')
if not usage:
continue
total_tokens = usage.get('total_tokens')
if total_tokens is None:
continue
day = log.created_at[:10]
tokens_by_day[day].append(total_tokens)
# --- 5. Build data points for each of the 7 days ---
data_points = []
for i in range(7):
day = (start_of_window + timedelta(days=i)).strftime('%Y-%m-%d')
daily_tokens = tokens_by_day.get(day, [])
summed_tokens = sum(daily_tokens) if daily_tokens else 0.0
data_points.append(
CreateCustomMetricDataPoint(
x_value=day,
y_value=summed_tokens,
)
)
# --- 6. Submit the data points ---
client.create_custom_metric_data_points(deployment_id, metric['id'], data_points)
print(f"Submitted {len(data_points)} data points to metric '{metric['name']}' ({metric['id']})")