AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
feat(platform/admin): add CSV export, cache tokens in logs, fix LLM cost on failure
Zamil Majdy89091cb90fb9
diff --git a/autogpt_platform/backend/backend/api/features/admin/platform_cost_routes.py b/autogpt_platform/backend/backend/api/features/admin/platform_cost_routes.pyindex fcf13dc9c7..f9b8aa817a 100644--- a/autogpt_platform/backend/backend/api/features/admin/platform_cost_routes.py+++ b/autogpt_platform/backend/backend/api/features/admin/platform_cost_routes.py@@ -10,6 +10,7 @@ from backend.data.platform_cost import (PlatformCostDashboard,get_platform_cost_dashboard,get_platform_cost_logs,+ get_platform_cost_logs_for_export,)from backend.util.models import Pagination@@ -82,3 +83,35 @@ async def get_cost_logs(page_size=page_size,),)+++class PlatformCostExportResponse(BaseModel):+ logs: list[CostLogRow]+ total_rows: int+ truncated: bool+++ "/logs/export",+ response_model=PlatformCostExportResponse,+ summary="Export Platform Cost Logs",+)+async def export_cost_logs(+ admin_user_id: str = Security(get_user_id),+ start: datetime | None = Query(None),+ end: datetime | None = Query(None),+ provider: str | None = Query(None),+ user_id: str | None = Query(None),+):+ logger.info("Admin %s exporting platform cost logs", admin_user_id)+ logs, truncated = await get_platform_cost_logs_for_export(+ start=start,+ end=end,+ provider=provider,+ user_id=user_id,+ )+ return PlatformCostExportResponse(+ logs=logs,+ total_rows=len(logs),+ truncated=truncated,+ )diff --git a/autogpt_platform/backend/backend/blocks/llm.py b/autogpt_platform/backend/backend/blocks/llm.pyindex 26901ea9ab..7ab6c48b77 100644--- a/autogpt_platform/backend/backend/blocks/llm.py+++ b/autogpt_platform/backend/backend/blocks/llm.py@@ -1602,6 +1602,10 @@ class AIStructuredResponseGeneratorBlock(AIBlockBase):error_feedback_message = f"Error calling LLM: {e}"+ # All retries exhausted or user-error break: persist accumulated cost so+ # the executor can still charge/report the spend even on failure.+ if total_provider_cost is not None:+ self.merge_stats(NodeExecutionStats(provider_cost=total_provider_cost))raise RuntimeError(error_feedback_message)def response_format_instructions(diff --git a/autogpt_platform/backend/backend/data/platform_cost.py b/autogpt_platform/backend/backend/data/platform_cost.pyindex e4732ef059..f45e5301eb 100644--- a/autogpt_platform/backend/backend/data/platform_cost.py+++ b/autogpt_platform/backend/backend/data/platform_cost.py@@ -157,6 +157,8 @@ class CostLogRow(BaseModel):output_tokens: int | None = Noneduration: float | None = Nonemodel: str | None = None+ cache_read_tokens: int | None = None+ cache_creation_tokens: int | None = Noneclass PlatformCostDashboard(BaseModel):@@ -370,6 +372,8 @@ async def get_platform_cost_logs(p."costMicrodollars" AS cost_microdollars,p."inputTokens" AS input_tokens,p."outputTokens" AS output_tokens,+ p."cacheReadTokens" AS cache_read_tokens,+ p."cacheCreationTokens" AS cache_creation_tokens,p."duration",p."model"FROM {{schema_prefix}}"PlatformCostLog" p@@ -399,9 +403,85 @@ async def get_platform_cost_logs(cost_microdollars=r.get("cost_microdollars"),input_tokens=r.get("input_tokens"),output_tokens=r.get("output_tokens"),+ cache_read_tokens=r.get("cache_read_tokens"),+ cache_creation_tokens=r.get("cache_creation_tokens"),duration=r.get("duration"),model=r.get("model"),)for r in rows]return logs, total+++EXPORT_MAX_ROWS = 100_000+++async def get_platform_cost_logs_for_export(+ start: datetime | None = None,+ end: datetime | None = None,+ provider: str | None = None,+ user_id: str | None = None,+) -> tuple[list[CostLogRow], bool]:+ """Return all matching rows up to EXPORT_MAX_ROWS.++ Returns (rows, truncated) where truncated=True means the result was capped+ and the caller should warn the user that not all rows are included.+ """+ if start is None:+ start = datetime.now(tz=timezone.utc) - timedelta(days=DEFAULT_DASHBOARD_DAYS)+ where_sql, params = _build_where(start, end, provider, user_id, "p")+ limit_idx = len(params) + 1++ rows = await query_raw_with_schema(+ f"""+ SELECT+ p."id",+ p."createdAt" AS created_at,+ p."userId" AS user_id,+ u."email",+ p."graphExecId" AS graph_exec_id,+ p."nodeExecId" AS node_exec_id,+ p."blockName" AS block_name,+ p."provider",+ p."trackingType" AS tracking_type,+ p."costMicrodollars" AS cost_microdollars,+ p."inputTokens" AS input_tokens,+ p."outputTokens" AS output_tokens,+ p."cacheReadTokens" AS cache_read_tokens,+ p."cacheCreationTokens" AS cache_creation_tokens,+ p."duration",+ p."model"+ FROM {{schema_prefix}}"PlatformCostLog" p+ LEFT JOIN {{schema_prefix}}"User" u ON u."id" = p."userId"+ WHERE {where_sql}+ ORDER BY p."createdAt" DESC, p."id" DESC+ LIMIT ${limit_idx}+ """,+ *params,+ EXPORT_MAX_ROWS + 1,+ )++ truncated = len(rows) > EXPORT_MAX_ROWS+ rows = rows[:EXPORT_MAX_ROWS]++ return [+ CostLogRow(+ id=r["id"],+ created_at=r["created_at"],+ user_id=r.get("user_id"),+ email=_mask_email(r.get("email")),+ graph_exec_id=r.get("graph_exec_id"),+ node_exec_id=r.get("node_exec_id"),+ block_name=r["block_name"],+ provider=r["provider"],+ tracking_type=r.get("tracking_type"),+ cost_microdollars=r.get("cost_microdollars"),+ input_tokens=r.get("input_tokens"),+ output_tokens=r.get("output_tokens"),+ cache_read_tokens=r.get("cache_read_tokens"),+ cache_creation_tokens=r.get("cache_creation_tokens"),+ duration=r.get("duration"),+ model=r.get("model"),+ )+ for r in rows+ ], truncateddiff --git a/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/__tests__/helpers.test.ts b/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/__tests__/helpers.test.tsindex 1a7e8b3163..4cd7afbaec 100644--- a/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/__tests__/helpers.test.ts+++ b/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/__tests__/helpers.test.ts@@ -1,7 +1,9 @@import { describe, expect, it } from "vitest";+import type { CostLogRow } from "@/app/api/__generated__/models/costLogRow";import type { ProviderCostSummary } from "@/app/api/__generated__/models/providerCostSummary";import {toDateOrUndefined,+ buildCostLogsCsv,formatMicrodollars,formatTokens,formatDuration,@@ -317,3 +319,50 @@ describe("toUtcIso", () => {expect(result).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/);});});++describe("buildCostLogsCsv", () => {+ function makeLog(overrides: Partial<CostLogRow>): CostLogRow {+ return {+ id: "abc123",+ created_at: "2026-01-15T10:00:00Z" as unknown as Date,+ block_name: "LLMBlock",+ provider: "anthropic",+ ...overrides,+ };+ }++ it("emits a header row and one data row", () => {+ const csv = buildCostLogsCsv([makeLog({})]);+ const lines = csv.split("\r\n");+ expect(lines).toHaveLength(2);+ expect(lines[0]).toContain("Time (UTC)");+ expect(lines[0]).toContain("Provider");+ expect(lines[1]).toContain("anthropic");+ });++ it("escapes double-quotes in field values", () => {+ const csv = buildCostLogsCsv([makeLog({ block_name: 'Say "Hello"' })]);+ expect(csv).toContain('"Say ""Hello"""');+ });++ it("converts cost_microdollars to USD with 8 decimal places", () => {+ const csv = buildCostLogsCsv([makeLog({ cost_microdollars: 1_234_567 })]);+ expect(csv).toContain("1.23456700");+ });++ it("includes cache token columns", () => {+ const csv = buildCostLogsCsv([+ makeLog({ cache_read_tokens: 500, cache_creation_tokens: 100 }),+ ]);+ const lines = csv.split("\r\n");+ expect(lines[0]).toContain("Cache Read Tokens");+ expect(lines[0]).toContain("Cache Creation Tokens");+ expect(lines[1]).toContain('"500"');+ expect(lines[1]).toContain('"100"');+ });++ it("returns only header for empty log list", () => {+ const csv = buildCostLogsCsv([]);+ expect(csv.split("\r\n")).toHaveLength(1);+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/components/LogsTable.tsx b/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/components/LogsTable.tsxindex a5942a2fbf..46920d15bc 100644--- a/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/components/LogsTable.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/components/LogsTable.tsx@@ -14,11 +14,33 @@ interface Props {logs: CostLogRow[];pagination: Pagination | null;onPageChange: (page: number) => void;+ onExport: () => Promise<void>;+ exporting: boolean;}-function LogsTable({ logs, pagination, onPageChange }: Props) {+function LogsTable({+ logs,+ pagination,+ onPageChange,+ onExport,+ exporting,+}: Props) {return (<div className="flex flex-col gap-4">+ <div className="flex items-center justify-between">+ <span className="text-sm text-muted-foreground">+ {pagination+ ? `${pagination.total_items.toLocaleString()} total rows`+ : ""}+ </span>+ <button+ onClick={onExport}+ disabled={exporting}+ className="rounded border px-3 py-1.5 text-sm hover:bg-muted disabled:opacity-50"+ >+ {exporting ? "Exporting…" : "Export CSV"}+ </button>+ </div><div className="overflow-x-auto"><table className="w-full text-left text-sm"><thead className="border-b text-xs uppercase text-muted-foreground">diff --git a/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/components/PlatformCostContent.tsx b/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/components/PlatformCostContent.tsxindex 9e4d24f824..81aa761b26 100644--- a/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/components/PlatformCostContent.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/components/PlatformCostContent.tsx@@ -41,6 +41,8 @@ export function PlatformCostContent({ searchParams }: Props) {handleRateOverride,updateUrl,handleFilter,+ exporting,+ handleExport,} = usePlatformCostContent(searchParams);return (@@ -224,6 +226,8 @@ export function PlatformCostContent({ searchParams }: Props) {logs={logs}pagination={pagination}onPageChange={(p) => updateUrl({ page: p.toString() })}+ onExport={handleExport}+ exporting={exporting}/></div>)}diff --git a/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/components/usePlatformCostContent.ts b/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/components/usePlatformCostContent.tsindex 01db1c5130..99074c5d35 100644--- a/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/components/usePlatformCostContent.ts+++ b/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/components/usePlatformCostContent.ts@@ -3,11 +3,17 @@import { useRouter, useSearchParams } from "next/navigation";import { useState } from "react";import {+ getV2ExportPlatformCostLogs,useGetV2GetPlatformCostDashboard,useGetV2GetPlatformCostLogs,} from "@/app/api/__generated__/endpoints/admin/admin";import { okData } from "@/app/api/helpers";-import { estimateCostForRow, toLocalInput, toUtcIso } from "../helpers";+import {+ buildCostLogsCsv,+ estimateCostForRow,+ toLocalInput,+ toUtcIso,+} from "../helpers";interface InitialSearchParams {start?: string;@@ -37,6 +43,7 @@ export function usePlatformCostContent(searchParams: InitialSearchParams) {const [rateOverrides, setRateOverrides] = useState<Record<string, number>>({},);+ const [exporting, setExporting] = useState(false);// Pass ISO date strings through `as unknown as Date` so Orval's URL builder// forwards them as-is. Date.toString() produces a format FastAPI rejects;@@ -105,6 +112,33 @@ export function usePlatformCostContent(searchParams: InitialSearchParams) {});}+ async function handleExport() {+ setExporting(true);+ try {+ const response = await getV2ExportPlatformCostLogs(filterParams);+ const data = okData(response);+ if (!data) throw new Error("Export failed: unexpected response");+ const csv = buildCostLogsCsv(data.logs);+ const blob = new Blob([csv], { type: "text/csv;charset=utf-8;" });+ const url = URL.createObjectURL(blob);+ const a = document.createElement("a");+ a.href = url;+ a.download = `platform_costs_${new Date().toISOString().slice(0, 10)}.csv`;+ document.body.appendChild(a);+ a.click();+ document.body.removeChild(a);+ URL.revokeObjectURL(url);+ if (data.truncated) {+ // eslint-disable-next-line no-console+ console.warn(+ `Export truncated: only the first ${data.total_rows} rows were included.`,+ );+ }+ } finally {+ setExporting(false);+ }+ }+const totalEstimatedCost =dashboard?.by_provider.reduce((sum, row) => {const est = estimateCostForRow(row, rateOverrides);@@ -132,5 +166,7 @@ export function usePlatformCostContent(searchParams: InitialSearchParams) {handleRateOverride,updateUrl,handleFilter,+ exporting,+ handleExport,};}diff --git a/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/helpers.tsindex 7b53567f18..9883a5a952 100644--- a/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/helpers.ts+++ b/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/helpers.ts@@ -1,3 +1,4 @@+import type { CostLogRow } from "@/app/api/__generated__/models/costLogRow";import type { ProviderCostSummary } from "@/app/api/__generated__/models/providerCostSummary";const MICRODOLLARS_PER_USD = 1_000_000;@@ -225,3 +226,54 @@ export function toUtcIso(local: string) {const d = new Date(local);return isNaN(d.getTime()) ? "" : d.toISOString();}++const CSV_HEADERS = [+ "Time (UTC)",+ "User ID",+ "Email",+ "Block",+ "Provider",+ "Type",+ "Model",+ "Cost (USD)",+ "Input Tokens",+ "Output Tokens",+ "Cache Read Tokens",+ "Cache Creation Tokens",+ "Duration (s)",+ "Graph Exec ID",+ "Node Exec ID",+];++function csvEscape(val: unknown): string {+ const s = val == null ? "" : String(val);+ return `"${s.replace(/"/g, '""')}"`;+}++export function buildCostLogsCsv(logs: CostLogRow[]): string {+ const header = CSV_HEADERS.map(csvEscape).join(",");+ const rows = logs.map((log) =>+ [+ log.created_at,+ log.user_id,+ log.email,+ log.block_name,+ log.provider,+ log.tracking_type,+ log.model,+ log.cost_microdollars != null+ ? (log.cost_microdollars / 1_000_000).toFixed(8)+ : null,+ log.input_tokens,+ log.output_tokens,+ log.cache_read_tokens,+ log.cache_creation_tokens,+ log.duration,+ log.graph_exec_id,+ log.node_exec_id,+ ]+ .map(csvEscape)+ .join(","),+ );+ return [header, ...rows].join("\r\n");+}diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.jsonindex 1a4c362898..a36b1e1502 100644--- a/autogpt_platform/frontend/src/app/api/openapi.json+++ b/autogpt_platform/frontend/src/app/api/openapi.json@@ -7473,6 +7473,81 @@}}}+ },+ "/api/admin/platform-costs/logs/export": {+ "get": {+ "tags": ["v2", "admin", "platform-cost", "admin"],+ "summary": "Export Platform Cost Logs",+ "operationId": "getV2Export platform cost logs",+ "security": [{ "HTTPBearerJWT": [] }],+ "parameters": [+ {+ "name": "start",+ "in": "query",+ "required": false,+ "schema": {+ "anyOf": [+ { "type": "string", "format": "date-time" },+ { "type": "null" }+ ],+ "title": "Start"+ }+ },+ {+ "name": "end",+ "in": "query",+ "required": false,+ "schema": {+ "anyOf": [+ { "type": "string", "format": "date-time" },+ { "type": "null" }+ ],+ "title": "End"+ }+ },+ {+ "name": "provider",+ "in": "query",+ "required": false,+ "schema": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Provider"+ }+ },+ {+ "name": "user_id",+ "in": "query",+ "required": false,+ "schema": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "User Id"+ }+ }+ ],+ "responses": {+ "200": {+ "description": "Successful Response",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/PlatformCostExportResponse"+ }+ }+ }+ },+ "401": {+ "$ref": "#/components/responses/HTTP401NotAuthenticatedError"+ },+ "422": {+ "description": "Validation Error",+ "content": {+ "application/json": {+ "schema": { "$ref": "#/components/schemas/HTTPValidationError" }+ }+ }+ }+ }+ }}},"components": {@@ -8948,6 +9023,14 @@"anyOf": [{ "type": "integer" }, { "type": "null" }],"title": "Output Tokens"},+ "cache_read_tokens": {+ "anyOf": [{ "type": "integer" }, { "type": "null" }],+ "title": "Cache Read Tokens"+ },+ "cache_creation_tokens": {+ "anyOf": [{ "type": "integer" }, { "type": "null" }],+ "title": "Cache Creation Tokens"+ },"duration": {"anyOf": [{ "type": "number" }, { "type": "null" }],"title": "Duration"@@ -15746,6 +15829,20 @@"expires_in_hours"],"title": "UploadFileResponse"+ },+ "PlatformCostExportResponse": {+ "properties": {+ "logs": {+ "items": { "$ref": "#/components/schemas/CostLogRow" },+ "type": "array",+ "title": "Logs"+ },+ "total_rows": { "type": "integer", "title": "Total Rows" },+ "truncated": { "type": "boolean", "title": "Truncated" }+ },+ "type": "object",+ "required": ["logs", "total_rows", "truncated"],+ "title": "PlatformCostExportResponse"}},"securitySchemes": {