Skip to content

🙌 OpenHands: AI-Driven Development

feat: show per-run LLM cost in the Activity Log and exports (#16351)

Hiep Le82bdd88269af
diff --git a/__tests__/components/automations/detail/activity-log-item.test.tsx b/__tests__/components/automations/detail/activity-log-item.test.tsx
index d417d0948d..8bec84fa66 100644
--- a/__tests__/components/automations/detail/activity-log-item.test.tsx
+++ b/__tests__/components/automations/detail/activity-log-item.test.tsx
@@ -233,3 +233,58 @@ describe("ActivityLogItem — timestamp fallback", () => {
expect(container.textContent).not.toContain("2030");
});
});
+
+describe("ActivityLogItem — run cost", () => {
+ beforeEach(() => {
+ __resetActiveStoreForTests();
+ setRegisteredBackends([localBackend]);
+ setActiveSelection({ backendId: localBackend.id });
+ });
+
+ afterEach(() => {
+ __resetActiveStoreForTests();
+ });
+
+ it("shows the accumulated LLM cost reported for the run", () => {
+ // Arrange
+ const run = makeRun({ cost: 0.4213 });
+
+ // Act
+ renderItem(run);
+
+ // Assert
+ expect(screen.getByText("$0.4213")).toBeInTheDocument();
+ });
+
+ it("shows a measured zero cost instead of hiding it", () => {
+ // Arrange: the service stores 0 only when the SDK reported a real
+ // zero-cost run, so it must stay distinguishable from an unknown cost.
+ const run = makeRun({ cost: 0 });
+
+ // Act
+ renderItem(run);
+
+ // Assert
+ expect(screen.getByText("$0.0000")).toBeInTheDocument();
+ });
+
+ // `null` is a run whose cost the service could not determine (cancelled,
+ // watchdog timeout, or predating cost tracking); `undefined` is an
+ // automation service too old to send the field at all. Neither has a cost
+ // to show.
+ it.each([
+ ["null", null],
+ ["undefined", undefined],
+ ])("shows no cost when the reported cost is %s", (_label, cost) => {
+ // Arrange
+ const run = makeRun({ cost });
+
+ // Act
+ renderItem(run);
+
+ // Assert
+ expect(
+ screen.queryByText((content) => content.startsWith("$")),
+ ).not.toBeInTheDocument();
+ });
+});
diff --git a/__tests__/utils/automation-activity-log-export.test.ts b/__tests__/utils/automation-activity-log-export.test.ts
index 5a17fd92cd..b1c0186cd5 100644
--- a/__tests__/utils/automation-activity-log-export.test.ts
+++ b/__tests__/utils/automation-activity-log-export.test.ts
@@ -56,6 +56,7 @@ const sampleRow = (
conversation_id: "c1",
conversation_url: "http://localhost:8000/conversations/c1",
error: "boom",
+ cost: null,
...overrides,
});
@@ -89,6 +90,29 @@ describe("automation-activity-log-export", () => {
).toEqual(sampleRow());
});
+ it("carries the run's accumulated cost into the export row", () => {
+ // Arrange
+ const run = sampleRun({ cost: 0.4213 });
+
+ // Act
+ const row = mapAutomationRunToExportRow(run, automation);
+
+ // Assert
+ expect(row.cost).toBe(0.4213);
+ });
+
+ it("serializes the cost as a raw number so spreadsheets can total it", () => {
+ // Arrange
+ const rows: AutomationRunExportRow[] = [sampleRow({ cost: 0.4213 })];
+
+ // Act
+ const csv = serializeActivityLogRowsCsv(rows);
+
+ // Assert
+ expect(csv.split("\n")[0]).toContain("cost");
+ expect(csv.split("\n")[1]).toContain("0.4213");
+ });
+
it("serializes CSV with conversation URL and escaped fields", () => {
const rows: AutomationRunExportRow[] = [
sampleRow({
diff --git a/src/components/features/automations/detail/activity-log-item.tsx b/src/components/features/automations/detail/activity-log-item.tsx
index 38037f76f5..2f830d6f9e 100644
--- a/src/components/features/automations/detail/activity-log-item.tsx
+++ b/src/components/features/automations/detail/activity-log-item.tsx
@@ -38,6 +38,19 @@ function getConversationUrl(conversationId: string): string {
return `/conversations/${conversationId}`;
}
+/**
+ * Format the run's accumulated LLM cost, or return null when it is unknown.
+ *
+ * A genuine `0` is rendered (`$0.0000`) rather than hidden: the automation
+ * service records zero only when the SDK reported a real zero-cost run, and
+ * leaves the value null when the cost could not be determined. Matches the
+ * 4-decimal USD convention used by the conversation metrics modal.
+ */
+function formatRunCost(cost: number | null | undefined): string | null {
+ if (typeof cost !== "number" || !Number.isFinite(cost)) return null;
+ return `$${cost.toFixed(4)}`;
+}
+
export function ActivityLogItem({ run, automation }: ActivityLogItemProps) {
const { t, i18n } = useTranslation("openhands");
const hasConversation = !!run.conversation_id;
@@ -65,6 +78,7 @@ export function ActivityLogItem({ run, automation }: ActivityLogItemProps) {
effectiveStartedAt,
i18n.language,
);
+ const formattedCost = formatRunCost(run.cost);
const handleLogsClick = (
e:
@@ -103,6 +117,15 @@ export function ActivityLogItem({ run, automation }: ActivityLogItemProps) {
)}
</div>
<div className="flex items-center gap-2">
+ {formattedCost && (
+ <span
+ data-testid="run-cost"
+ title={t(I18nKey.AUTOMATIONS$DETAIL$RUN_COST)}
+ className="text-xs tabular-nums text-muted"
+ >
+ {formattedCost}
+ </span>
+ )}
{logsButton}
<RunStatusBadge status={run.status} />
</div>
diff --git a/src/i18n/translation.json b/src/i18n/translation.json
index 2dbf869bad..500c592c22 100644
--- a/src/i18n/translation.json
+++ b/src/i18n/translation.json
@@ -14092,6 +14092,23 @@
"uk": "Журнал активності",
"ca": "Registre d'activitat"
},
+ "AUTOMATIONS$DETAIL$RUN_COST": {
+ "en": "Cost",
+ "ja": "コスト",
+ "zh-CN": "成本",
+ "zh-TW": "成本",
+ "ko-KR": "비용",
+ "no": "Kostnad",
+ "it": "Costo",
+ "pt": "Custo",
+ "es": "Costo",
+ "ar": "التكلفة",
+ "fr": "Coût",
+ "tr": "Maliyet",
+ "de": "Kosten",
+ "uk": "Вартість",
+ "ca": "Cost"
+ },
"AUTOMATIONS$DETAIL$BACK_TO_LIST": {
"en": "Back to Automations",
"ja": "オートメーション一覧に戻る",
diff --git a/src/types/automation.ts b/src/types/automation.ts
index 31609ad240..fa73590814 100644
--- a/src/types/automation.ts
+++ b/src/types/automation.ts
@@ -85,6 +85,14 @@ export interface AutomationRun {
*/
bash_command_id: string | null;
error_detail: string | null;
+ /**
+ * Accumulated LLM cost of the run in USD, reported by the SDK in the
+ * completion callback. `null` means unknown — the run predates cost
+ * tracking, or ended without a callback (cancelled, watchdog timeout).
+ * Absent entirely when the automation service is older than the release
+ * that added the field, hence optional.
+ */
+ cost?: number | null;
started_at: string;
completed_at: string | null;
}
@@ -109,4 +117,10 @@ export interface AutomationRunExportRow {
conversation_id: string | null;
conversation_url: string | null;
error: string | null;
+ /**
+ * Accumulated LLM cost in USD, or null when unknown. Unlike
+ * `AutomationRun["cost"]` this is always present: the row normalizes a
+ * missing field to null so every exported record has the same shape.
+ */
+ cost: number | null;
}
diff --git a/src/utils/automation-activity-log-export.ts b/src/utils/automation-activity-log-export.ts
index 31f308155b..e315a04449 100644
--- a/src/utils/automation-activity-log-export.ts
+++ b/src/utils/automation-activity-log-export.ts
@@ -21,6 +21,9 @@ const CSV_COLUMNS = [
"conversation_id",
"conversation_url",
"error",
+ // Appended rather than grouped with the other run metrics so existing
+ // consumers that read the CSV by column position keep working.
+ "cost",
] as const;
export function getActivityLogExportFilename(
@@ -98,6 +101,9 @@ export function mapAutomationRunToExportRow(
conversationBaseUrl,
),
error: run.error_detail,
+ // Exports carry the raw number and leave formatting to the consumer; the
+ // Activity Log row is the only place that renders it as USD.
+ cost: run.cost ?? null,
};
}