Skip to content

Commit b7f65e4

Browse files
authored
feat(tasks): add runtime_adapter label to run token metrics (#71447)
1 parent e7a7472 commit b7f65e4

4 files changed

Lines changed: 21 additions & 3 deletions

File tree

products/tasks/backend/sandbox/images/Dockerfile.sandbox-base

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,11 @@ RUN set -eux; \
105105
ENV AGENTSH_SERVER=http://127.0.0.1:18080
106106

107107
# Install rtk (https://github.com/rtk-ai/rtk), a proxy that compresses the output of
108-
# common dev commands before it reaches the model. The agent auto-detects it on PATH
109-
# and routes eligible Bash commands through it; POSTHOG_RTK=0 (set per run from the
110-
# task processing context) opts a run out.
108+
# common dev commands before it reaches the model. Claude runs auto-detect it on PATH
109+
# and route eligible Bash commands through it via a PreToolUse rewrite hook; Codex has
110+
# no command-rewrite channel, so its runs are told to prefix eligible commands in the
111+
# developer instructions instead. POSTHOG_RTK=0 (set per run from the task processing
112+
# context) opts a run out of both.
111113
ARG RTK_VERSION=0.43.0
112114
RUN set -eux; \
113115
arch="$(dpkg --print-architecture)"; \

products/tasks/backend/temporal/metrics.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ def _bool_label(value: bool | None) -> str:
7171
return "true" if value else "false"
7272

7373

74+
_ALLOWED_RUNTIME_ADAPTERS = {"claude", "codex"}
75+
76+
77+
def _runtime_adapter_label(value: str | None) -> str:
78+
"""Bounded label: unexpected values collapse to "other" to cap cardinality."""
79+
if not value:
80+
return "unknown"
81+
return value if value in _ALLOWED_RUNTIME_ADAPTERS else "other"
82+
83+
7484
def increment_snapshot_usage(
7585
used_snapshot: bool,
7686
*,
@@ -139,6 +149,7 @@ def record_run_token_usage(
139149
origin_product: str | None,
140150
run_environment: str | None,
141151
rtk_enabled: bool | None,
152+
runtime_adapter: str | None,
142153
status: str | None,
143154
) -> None:
144155
"""Record a terminal run's token expenditure (from ``TaskRun.state.token_usage``).
@@ -150,6 +161,7 @@ def record_run_token_usage(
150161
"origin_product": origin_product or "unknown",
151162
"run_environment": run_environment or "unknown",
152163
"rtk_enabled": _bool_label(rtk_enabled),
164+
"runtime_adapter": _runtime_adapter_label(runtime_adapter),
153165
"status": status or "unknown",
154166
}
155167
for kind, key in _RUN_TOKEN_KINDS.items():

products/tasks/backend/temporal/process_task/activities/tests/test_update_task_run_status.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def test_terminal_transition_captures_analytics_with_usage(
9696
**(test_task_run.state or {}),
9797
"token_usage": dict(TOKEN_USAGE),
9898
"rtk_effective": True,
99+
"runtime_adapter": "codex",
99100
}
100101
test_task_run.save(update_fields=["state"])
101102

@@ -116,6 +117,7 @@ def test_terminal_transition_captures_analytics_with_usage(
116117
assert props["run_environment"] == test_task_run.environment
117118
mock_record.assert_called_once()
118119
assert mock_record.call_args.kwargs["rtk_enabled"] is True
120+
assert mock_record.call_args.kwargs["runtime_adapter"] == "codex"
119121
assert mock_record.call_args.kwargs["status"] == status
120122

121123
@pytest.mark.django_db(transaction=True)

products/tasks/backend/temporal/process_task/activities/update_task_run_status.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,13 @@ def _capture_terminal_analytics(task_run: TaskRun, input: UpdateTaskRunStatusInp
104104
state = task_run.state if isinstance(task_run.state, dict) else {}
105105
usage = state.get("token_usage")
106106
if isinstance(usage, dict):
107+
adapter = state.get("runtime_adapter")
107108
record_run_token_usage(
108109
usage,
109110
origin_product=task_run.task.origin_product,
110111
run_environment=task_run.environment,
111112
rtk_enabled=task_run.effective_rtk(),
113+
runtime_adapter=adapter if isinstance(adapter, str) else None,
112114
status=input.status,
113115
)
114116
except Exception:

0 commit comments

Comments
 (0)