Skip to content

Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)

Python

Show total request cost across all turns in prompt reports

Abi Raja3c8251ee9bf9
diff --git a/backend/routes/prompt_reports.py b/backend/routes/prompt_reports.py
index 4c3d0037..4b77d716 100644
--- a/backend/routes/prompt_reports.py
+++ b/backend/routes/prompt_reports.py
@@ -7,6 +7,7 @@ cover the whole ``run_logs`` directory so legacy HTML reports are included.
import json
import os
+import re
import shutil
from datetime import datetime, timedelta
from typing import Any
@@ -31,6 +32,7 @@ class PromptReportSummary(BaseModel):
session_id: str
turn: int
size_bytes: int
+ cost_usd: float | None = None
class PromptReportListResponse(BaseModel):
@@ -59,6 +61,39 @@ def _directory_size_bytes(directory: str) -> int:
return total
+_COST_USD_PATTERN = re.compile(
+ r'"cost_usd"\s*:\s*(null|-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)'
+)
+
+
+def _read_report_cost_usd(filepath: str) -> float | None:
+ """Read a report's usage cost without parsing the whole (possibly multi-MB,
+ image-laden) file.
+
+ ``usage`` is the last key the writer emits, so ``cost_usd`` lives at the
+ tail; reading the final chunk and taking the last match keeps listing cheap.
+ """
+ try:
+ size = os.path.getsize(filepath)
+ with open(filepath, "rb") as f:
+ if size > 2048:
+ f.seek(-2048, os.SEEK_END)
+ tail = f.read().decode("utf-8", errors="ignore")
+ except OSError:
+ return None
+
+ matches = _COST_USD_PATTERN.findall(tail)
+ if not matches:
+ return None
+ last = matches[-1]
+ if last == "null":
+ return None
+ try:
+ return float(last)
+ except ValueError:
+ return None
+
+
def _summarize_report_file(directory: str, filename: str) -> PromptReportSummary | None:
match = PROMPT_REPORT_FILENAME_PATTERN.match(filename)
if match is None:
@@ -82,6 +117,7 @@ def _summarize_report_file(directory: str, filename: str) -> PromptReportSummary
session_id=match.group("session"),
turn=int(match.group("turn")),
size_bytes=size_bytes,
+ cost_usd=_read_report_cost_usd(os.path.join(directory, filename)),
)
diff --git a/backend/tests/test_prompt_reports.py b/backend/tests/test_prompt_reports.py
index 72f6b78d..fe23efb5 100644
--- a/backend/tests/test_prompt_reports.py
+++ b/backend/tests/test_prompt_reports.py
@@ -130,6 +130,9 @@ async def test_list_prompt_reports_returns_metadata_and_total_size(
) -> None:
logger = _make_logger()
logger.record_request({"input": ["hello"]})
+ logger.record_usage(
+ TokenUsage(input=1000, output=500, cache_read=200, cache_write=0, total=1700)
+ )
# Legacy artifacts in run_logs count toward total size but are not listed.
legacy_file = logs_path / "run_logs" / "old_report.html"
@@ -143,9 +146,24 @@ async def test_list_prompt_reports_returns_metadata_and_total_size(
assert report.model == "gpt-5.5"
assert report.turn == 1
assert report.size_bytes > 0
+ # Cost is read from the report tail without parsing the whole file.
+ assert report.cost_usd is not None and report.cost_usd > 0
assert response.total_size_bytes >= report.size_bytes + legacy_file.stat().st_size
+async def test_list_prompt_reports_cost_is_none_without_usage(
+ logs_path: Path,
+) -> None:
+ logger = _make_logger()
+ logger.record_request({"input": ["hello"]}) # no record_usage → usage is null
+
+ response = await list_prompt_reports()
+
+ assert len(response.reports) == 1
+ assert response.reports[0].cost_usd is None
+
+
@pytest.mark.asyncio
async def test_get_prompt_report_content_round_trips(logs_path: Path) -> None:
logger = _make_logger()
diff --git a/frontend/src/components/evals/PromptReportsPage.tsx b/frontend/src/components/evals/PromptReportsPage.tsx
index 7d6eb454..622b0c41 100644
--- a/frontend/src/components/evals/PromptReportsPage.tsx
+++ b/frontend/src/components/evals/PromptReportsPage.tsx
@@ -13,6 +13,7 @@ interface PromptReportSummary {
session_id: string;
turn: number;
size_bytes: number;
+ cost_usd: number | null;
}
interface PromptReportListResponse {
@@ -48,6 +49,7 @@ interface SessionGroup {
model: string;
createdAt: string;
sizeBytes: number;
+ costUsd: number | null;
turns: PromptReportSummary[];
}
@@ -82,6 +84,12 @@ function formatTimestamp(isoTimestamp: string): string {
return date.toLocaleString();
}
+function formatCost(costUsd: number | null): string {
+ if (costUsd === null) return "—";
+ if (costUsd > 0 && costUsd < 0.01) return "<$0.01";
+ return `$${costUsd.toFixed(costUsd < 1 ? 4 : 2)}`;
+}
+
function groupBySession(reports: PromptReportSummary[]): SessionGroup[] {
const groups = new Map<string, SessionGroup>();
for (const report of reports) {
@@ -93,11 +101,15 @@ function groupBySession(reports: PromptReportSummary[]): SessionGroup[] {
model: report.model,
createdAt: report.created_at,
sizeBytes: 0,
+ costUsd: null,
turns: [],
};
groups.set(report.session_id, group);
}
group.sizeBytes += report.size_bytes;
+ if (typeof report.cost_usd === "number") {
+ group.costUsd = (group.costUsd ?? 0) + report.cost_usd;
+ }
if (report.created_at > group.createdAt) {
group.createdAt = report.created_at;
}
@@ -584,8 +596,16 @@ function PromptReportsPage() {
<div className="mt-1 truncate font-mono text-sm text-zinc-100">
{group.model}
</div>
- <div className="mt-0.5 text-[11px] text-zinc-500">
- {formatTimestamp(group.createdAt)}
+ <div className="mt-0.5 flex items-center justify-between gap-2 text-[11px]">
+ <span className="text-zinc-500">
+ {formatTimestamp(group.createdAt)}
+ </span>
+ <span
+ className="font-mono font-medium text-emerald-400"
+ title="Total cost across all turns in this request"
+ >
+ {formatCost(group.costUsd)}
+ </span>
</div>
<div className="mt-2 flex flex-wrap gap-1">
{group.turns.map((turnReport) => (