Skip to content

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): per-model cost breakdown and Anthropic cache token tracking

Zamil Majdyb2d89c3a66c7
diff --git a/autogpt_platform/backend/backend/blocks/llm.py b/autogpt_platform/backend/backend/blocks/llm.py
index 1e2ca23c37..cadd6bd33c 100644
--- a/autogpt_platform/backend/backend/blocks/llm.py
+++ b/autogpt_platform/backend/backend/blocks/llm.py
@@ -738,6 +738,8 @@ class LLMResponse(BaseModel):
tool_calls: Optional[List[ToolContentBlock]] | None
prompt_tokens: int
completion_tokens: int
+ cache_read_tokens: int = 0
+ cache_creation_tokens: int = 0
reasoning: Optional[str] = None
provider_cost: float | None = None
@@ -1046,6 +1048,11 @@ async def llm_call(
tool_calls=tool_calls,
prompt_tokens=resp.usage.input_tokens,
completion_tokens=resp.usage.output_tokens,
+ cache_read_tokens=getattr(resp.usage, "cache_read_input_tokens", None) or 0,
+ cache_creation_tokens=getattr(
+ resp.usage, "cache_creation_input_tokens", None
+ )
+ or 0,
reasoning=reasoning,
)
elif provider == "groq":
@@ -1467,6 +1474,8 @@ class AIStructuredResponseGeneratorBlock(AIBlockBase):
token_stats = NodeExecutionStats(
input_token_count=llm_response.prompt_tokens,
output_token_count=llm_response.completion_tokens,
+ cache_read_token_count=llm_response.cache_read_tokens,
+ cache_creation_token_count=llm_response.cache_creation_tokens,
)
self.merge_stats(token_stats)
last_attempt_cost = llm_response.provider_cost
diff --git a/autogpt_platform/backend/backend/copilot/token_tracking.py b/autogpt_platform/backend/backend/copilot/token_tracking.py
index f48749e712..e84b64d449 100644
--- a/autogpt_platform/backend/backend/copilot/token_tracking.py
+++ b/autogpt_platform/backend/backend/copilot/token_tracking.py
@@ -202,6 +202,8 @@ async def persist_and_record_usage(
cost_microdollars=cost_microdollars,
input_tokens=prompt_tokens,
output_tokens=completion_tokens,
+ cache_read_tokens=cache_read_tokens or None,
+ cache_creation_tokens=cache_creation_tokens or None,
model=model,
tracking_type=tracking_type,
tracking_amount=tracking_amount,
diff --git a/autogpt_platform/backend/backend/data/model.py b/autogpt_platform/backend/backend/data/model.py
index add0c6b5cf..0fa7330566 100644
--- a/autogpt_platform/backend/backend/data/model.py
+++ b/autogpt_platform/backend/backend/data/model.py
@@ -850,6 +850,8 @@ class NodeExecutionStats(BaseModel):
llm_retry_count: int = 0
input_token_count: int = 0
output_token_count: int = 0
+ cache_read_token_count: int = 0
+ cache_creation_token_count: int = 0
extra_cost: int = 0
extra_steps: int = 0
provider_cost: float | None = None
diff --git a/autogpt_platform/backend/backend/data/platform_cost.py b/autogpt_platform/backend/backend/data/platform_cost.py
index 6865967627..7f157ac283 100644
--- a/autogpt_platform/backend/backend/data/platform_cost.py
+++ b/autogpt_platform/backend/backend/data/platform_cost.py
@@ -44,6 +44,8 @@ class PlatformCostEntry(BaseModel):
cost_microdollars: int | None = None
input_tokens: int | None = None
output_tokens: int | None = None
+ cache_read_tokens: int | None = None
+ cache_creation_tokens: int | None = None
data_size: int | None = None
duration: float | None = None
model: str | None = None
@@ -69,6 +71,8 @@ async def log_platform_cost(entry: PlatformCostEntry) -> None:
costMicrodollars=entry.cost_microdollars,
inputTokens=entry.input_tokens,
outputTokens=entry.output_tokens,
+ cacheReadTokens=entry.cache_read_tokens,
+ cacheCreationTokens=entry.cache_creation_tokens,
dataSize=entry.data_size,
duration=entry.duration,
model=entry.model,
@@ -118,9 +122,12 @@ def _mask_email(email: str | None) -> str | None:
class ProviderCostSummary(BaseModel):
provider: str
tracking_type: str | None = None
+ model: str | None = None
total_cost_microdollars: int
total_input_tokens: int
total_output_tokens: int
+ total_cache_read_tokens: int = 0
+ total_cache_creation_tokens: int = 0
total_duration_seconds: float = 0.0
total_tracking_amount: float = 0.0
request_count: int
@@ -222,15 +229,18 @@ async def get_platform_cost_dashboard(
SELECT
p."provider",
p."trackingType" AS tracking_type,
+ p."model",
COALESCE(SUM(p."costMicrodollars"), 0)::bigint AS total_cost,
COALESCE(SUM(p."inputTokens"), 0)::bigint AS total_input_tokens,
COALESCE(SUM(p."outputTokens"), 0)::bigint AS total_output_tokens,
+ COALESCE(SUM(p."cacheReadTokens"), 0)::bigint AS total_cache_read_tokens,
+ COALESCE(SUM(p."cacheCreationTokens"), 0)::bigint AS total_cache_creation_tokens,
COALESCE(SUM(p."duration"), 0)::float AS total_duration,
COALESCE(SUM(p."trackingAmount"), 0)::float AS total_tracking_amount,
COUNT(*)::bigint AS request_count
FROM {{schema_prefix}}"PlatformCostLog" p
WHERE {where_p}
- GROUP BY p."provider", p."trackingType"
+ GROUP BY p."provider", p."trackingType", p."model"
ORDER BY total_cost DESC
LIMIT {MAX_PROVIDER_ROWS}
""",
@@ -275,9 +285,12 @@ async def get_platform_cost_dashboard(
ProviderCostSummary(
provider=r["provider"],
tracking_type=r.get("tracking_type"),
+ model=r.get("model"),
total_cost_microdollars=r["total_cost"],
total_input_tokens=r["total_input_tokens"],
total_output_tokens=r["total_output_tokens"],
+ total_cache_read_tokens=r.get("total_cache_read_tokens", 0),
+ total_cache_creation_tokens=r.get("total_cache_creation_tokens", 0),
total_duration_seconds=r.get("total_duration", 0.0),
total_tracking_amount=r.get("total_tracking_amount", 0.0),
request_count=r["request_count"],
diff --git a/autogpt_platform/backend/backend/executor/cost_tracking.py b/autogpt_platform/backend/backend/executor/cost_tracking.py
index b1381d18c0..dcfc4885a5 100644
--- a/autogpt_platform/backend/backend/executor/cost_tracking.py
+++ b/autogpt_platform/backend/backend/executor/cost_tracking.py
@@ -262,6 +262,10 @@ async def log_system_credential_cost(
# Use 'provider_cost_raw' — the value's unit varies by tracking
# type (USD for cost_usd, count for items/characters/per_run, etc.)
meta["provider_cost_raw"] = stats.provider_cost
+ if stats.cache_read_token_count:
+ meta["cache_read_tokens"] = stats.cache_read_token_count
+ if stats.cache_creation_token_count:
+ meta["cache_creation_tokens"] = stats.cache_creation_token_count
_schedule_log(
db_client,
@@ -278,6 +282,8 @@ async def log_system_credential_cost(
cost_microdollars=cost_microdollars,
input_tokens=stats.input_token_count,
output_tokens=stats.output_token_count,
+ cache_read_tokens=stats.cache_read_token_count or None,
+ cache_creation_tokens=stats.cache_creation_token_count or None,
data_size=stats.output_size if stats.output_size > 0 else None,
duration=stats.walltime if stats.walltime > 0 else None,
model=model_name,
diff --git a/autogpt_platform/backend/migrations/20260409000000_add_cache_tokens_to_platform_cost_log/migration.sql b/autogpt_platform/backend/migrations/20260409000000_add_cache_tokens_to_platform_cost_log/migration.sql
new file mode 100644
index 0000000000..21c2f0d6b6
--- /dev/null
+++ b/autogpt_platform/backend/migrations/20260409000000_add_cache_tokens_to_platform_cost_log/migration.sql
@@ -0,0 +1,2 @@
+ALTER TABLE "PlatformCostLog" ADD COLUMN "cacheReadTokens" INTEGER;
+ALTER TABLE "PlatformCostLog" ADD COLUMN "cacheCreationTokens" INTEGER;
diff --git a/autogpt_platform/backend/schema.prisma b/autogpt_platform/backend/schema.prisma
index abe7e51d5d..2a6633bbc6 100644
--- a/autogpt_platform/backend/schema.prisma
+++ b/autogpt_platform/backend/schema.prisma
@@ -842,6 +842,8 @@ model PlatformCostLog {
inputTokens Int?
outputTokens Int?
+ cacheReadTokens Int? // Anthropic cache read tokens (billed at 10% of base)
+ cacheCreationTokens Int? // Anthropic cache write tokens (billed at 125% of base)
dataSize Int? // bytes
duration Float? // seconds
model String?
diff --git a/autogpt_platform/frontend/package.json b/autogpt_platform/frontend/package.json
index 90c2645272..00e9e6fc8a 100644
--- a/autogpt_platform/frontend/package.json
+++ b/autogpt_platform/frontend/package.json
@@ -150,6 +150,7 @@
"@types/react-dom": "18.3.5",
"@types/react-modal": "3.16.3",
"@types/react-window": "2.0.0",
+ "@types/twemoji": "13.1.2",
"@vitejs/plugin-react": "5.1.2",
"@vitest/coverage-v8": "4.0.17",
"axe-playwright": "2.2.2",
diff --git a/autogpt_platform/frontend/pnpm-lock.yaml b/autogpt_platform/frontend/pnpm-lock.yaml
index 95b49e3a22..057719def1 100644
--- a/autogpt_platform/frontend/pnpm-lock.yaml
+++ b/autogpt_platform/frontend/pnpm-lock.yaml
@@ -367,6 +367,9 @@ importers:
'@types/react-window':
specifier: 2.0.0
+ '@types/twemoji':
+ specifier: 13.1.2
+ version: 13.1.2
'@vitejs/plugin-react':
specifier: 5.1.2
@@ -3705,6 +3708,10 @@ packages:
resolution: {integrity: sha512-ScaPdn1dQczgbl0QFTeTOmVHFULt394XJgOQNoyVhZ6r2vLnMLJfBPd53SB52T/3G36VI1/g2MZaX0cwDuXsfw==}
+ '@types/[email protected]':
+ resolution: {integrity: sha512-vPNsrN08aRI2Gmdo+Ds3zZXzUk6igp1Hg+JPCeHavpiUGfgth/tGiHLQxfSrKzPXeRC0zbLs8WaUZSYxRWPbNg==}
+ deprecated: This is a stub types definition. twemoji provides its own type definitions, so you do not need this installed.
+
resolution: {integrity: sha512-CmBKiL6NNo/OqgmMn95Fk9Whlp2mtvIv+KNpQKN2F4SjvrEesubTRWGYSg+BnWZOnlCaSTU1sMpsBOzgbYhnsA==}
@@ -12681,6 +12688,10 @@ snapshots:
optional: true
+ '@types/[email protected]':
+ dependencies:
+ twemoji: 14.0.2
+
'@types/[email protected]': {}
'@types/[email protected]': {}
diff --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.ts
index 25d4f1e064..1c3292f4a1 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
@@ -133,7 +133,7 @@ describe("estimateCostForRow", () => {
request_count: 10,
});
// override = 0.05 * 10 * 1_000_000 = 500_000
- expect(estimateCostForRow(row, { "google_maps:per_run": 0.05 })).toBe(
+ expect(estimateCostForRow(row, { "google_maps:per_run:": 0.05 })).toBe(
500_000,
);
});
diff --git a/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/components/ProviderTable.tsx b/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/components/ProviderTable.tsx
index 8448253587..684d4b83bc 100644
--- a/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/components/ProviderTable.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/components/ProviderTable.tsx
@@ -24,6 +24,9 @@ function ProviderTable({ data, rateOverrides, onRateOverride }: Props) {
<th scope="col" className="px-4 py-3">
Provider
</th>
+ <th scope="col" className="px-4 py-3">
+ Model
+ </th>
<th scope="col" className="px-4 py-3">
Type
</th>
@@ -55,12 +58,18 @@ function ProviderTable({ data, rateOverrides, onRateOverride }: Props) {
// For cost_usd rows the provider reports USD directly so rate
// input doesn't apply; otherwise show an editable input.
const showRateInput = tt !== "cost_usd";
- const key = rateKey(row.provider, tt);
+ const key = rateKey(row.provider, tt, row.model);
const fallback = defaultRateFor(row.provider, tt);
const currentRate = rateOverrides[key] ?? fallback;
return (
- <tr key={key} className="border-b hover:bg-muted">
+ <tr
+ key={`${row.provider}:${row.tracking_type}:${row.model ?? ""}`}
+ className="border-b hover:bg-muted"
+ >
<td className="px-4 py-3 font-medium">{row.provider}</td>
+ <td className="px-4 py-3 text-muted-foreground">
+ {row.model || "—"}
+ </td>
<td className="px-4 py-3">
<TrackingBadge trackingType={row.tracking_type} />
</td>
@@ -115,7 +124,7 @@ function ProviderTable({ data, rateOverrides, onRateOverride }: Props) {
{data.length === 0 && (
<tr>
<td
- colSpan={7}
+ colSpan={8}
className="px-4 py-8 text-center text-muted-foreground"
>
No cost data yet
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.ts
index 63d14a82c1..c1ca0b099c 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
@@ -111,13 +111,14 @@ export function defaultRateFor(
}
}
-// Overrides are keyed on `${provider}:${tracking_type}` since the same
-// provider can have multiple rows with different billing models.
+// Overrides are keyed on `${provider}:${tracking_type}:${model}` since the
+// same provider can have multiple rows with different billing models and models.
export function rateKey(
provider: string,
trackingType: string | null | undefined,
+ model?: string | null,
): string {
- return `${provider}:${trackingType ?? "per_run"}`;
+ return `${provider}:${trackingType ?? "per_run"}:${model ?? ""}`;
}
export function estimateCostForRow(
@@ -136,17 +137,34 @@ export function estimateCostForRow(
}
const rate =
- rateOverrides[rateKey(row.provider, tt)] ??
+ rateOverrides[rateKey(row.provider, tt, row.model)] ??
defaultRateFor(row.provider, tt);
if (rate === null || rate === undefined) return null;
// Compute the amount for this tracking type, then multiply by rate.
let amount: number;
switch (tt) {
- case "tokens":
+ case "tokens": {
+ // Anthropic cache tokens are billed at different rates:
+ // - cache reads: 10% of base input rate
+ // - cache writes: 125% of base input rate
+ // - uncached input: 100% of base input rate
+ const cacheRead = row.total_cache_read_tokens ?? 0;
+ const cacheWrite = row.total_cache_creation_tokens ?? 0;
+ if (cacheRead > 0 || cacheWrite > 0) {
+ const uncachedInput = row.total_input_tokens;
+ const output = row.total_output_tokens;
+ const cost =
+ (uncachedInput / 1000) * rate +
+ (cacheRead / 1000) * rate * 0.1 +
+ (cacheWrite / 1000) * rate * 1.25 +
+ (output / 1000) * rate;
+ return Math.round(cost * MICRODOLLARS_PER_USD);
+ }
// Rate is per-1K tokens.
amount = (row.total_input_tokens + row.total_output_tokens) / 1000;
break;
+ }
case "characters":
// Rate is per-1K chars. trackingAmount aggregates char counts.
amount = (row.total_tracking_amount || 0) / 1000;
@@ -175,6 +193,11 @@ export function trackingValue(row: ProviderCostSummary) {
if (tt === "cost_usd") return formatMicrodollars(row.total_cost_microdollars);
if (tt === "tokens") {
const tokens = row.total_input_tokens + row.total_output_tokens;
+ const cacheRead = row.total_cache_read_tokens ?? 0;
+ const cacheWrite = row.total_cache_creation_tokens ?? 0;
+ if (cacheRead > 0 || cacheWrite > 0) {
+ return `${formatTokens(tokens)} tokens (+${formatTokens(cacheRead)} cached)`;
+ }
return `${formatTokens(tokens)} tokens`;
}
if (tt === "sandbox_seconds" || tt === "walltime_seconds")
diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.json
index 3f013c4509..1a4c362898 100644
--- a/autogpt_platform/frontend/src/app/api/openapi.json
+++ b/autogpt_platform/frontend/src/app/api/openapi.json
@@ -12335,6 +12335,10 @@
"anyOf": [{ "type": "string" }, { "type": "null" }],
"title": "Tracking Type"
},
+ "model": {
+ "anyOf": [{ "type": "string" }, { "type": "null" }],
+ "title": "Model"
+ },
"total_cost_microdollars": {
"type": "integer",
"title": "Total Cost Microdollars"
@@ -12347,6 +12351,16 @@
"type": "integer",
"title": "Total Output Tokens"
},
+ "total_cache_read_tokens": {
+ "type": "integer",
+ "title": "Total Cache Read Tokens",
+ "default": 0
+ },
+ "total_cache_creation_tokens": {
+ "type": "integer",
+ "title": "Total Cache Creation Tokens",
+ "default": 0
+ },
"total_duration_seconds": {
"type": "number",
"title": "Total Duration Seconds",