Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(ai-usage): price prompt-cache reads and writes (#7554)

Wolf Mermelstein3079a1bc43e1
diff --git a/.sqlx/query-0c21b4586c245cfc25a6bbd17ba5e034a0ee2ee3237ded5b1645a58906b3ae6c.json b/.sqlx/query-0c21b4586c245cfc25a6bbd17ba5e034a0ee2ee3237ded5b1645a58906b3ae6c.json
new file mode 100644
index 0000000000..b158e99e81
--- /dev/null
+++ b/.sqlx/query-0c21b4586c245cfc25a6bbd17ba5e034a0ee2ee3237ded5b1645a58906b3ae6c.json
@@ -0,0 +1,46 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT\n price_per_million_in,\n price_per_million_out,\n price_per_million_cache_read,\n price_per_million_cache_write,\n price_per_audio_minute\n FROM ai_pricing\n WHERE model = $1\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "price_per_million_in",
+ "type_info": "Float4"
+ },
+ {
+ "ordinal": 1,
+ "name": "price_per_million_out",
+ "type_info": "Float4"
+ },
+ {
+ "ordinal": 2,
+ "name": "price_per_million_cache_read",
+ "type_info": "Float4"
+ },
+ {
+ "ordinal": 3,
+ "name": "price_per_million_cache_write",
+ "type_info": "Float4"
+ },
+ {
+ "ordinal": 4,
+ "name": "price_per_audio_minute",
+ "type_info": "Float4"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ true,
+ true,
+ true
+ ]
+ },
+ "hash": "0c21b4586c245cfc25a6bbd17ba5e034a0ee2ee3237ded5b1645a58906b3ae6c"
+}
diff --git a/.sqlx/query-039e91325b454f78eaaba921deee2cda2dd09d78073173b60ff32306d8991024.json b/.sqlx/query-1603df291e3d62f08cb38af3f118b2bb25f306bbba797fbd70ce375e45562322.json
similarity index 57%
rename from .sqlx/query-039e91325b454f78eaaba921deee2cda2dd09d78073173b60ff32306d8991024.json
rename to .sqlx/query-1603df291e3d62f08cb38af3f118b2bb25f306bbba797fbd70ce375e45562322.json
index f957d53197..228fa013a5 100644
--- a/.sqlx/query-039e91325b454f78eaaba921deee2cda2dd09d78073173b60ff32306d8991024.json
+++ b/.sqlx/query-1603df291e3d62f08cb38af3f118b2bb25f306bbba797fbd70ce375e45562322.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT user_id, COALESCE(SUM(\n COALESCE(\n total::float8,\n (input_tokens::float8 / 1000000.0) * $4\n + (output_tokens::float8 / 1000000.0) * $5\n )\n ), 0)::float8 AS \"usd!\"\n FROM ai_usage\n WHERE user_id = ANY($1)\n AND created_at >= $2\n AND created_at < $3\n AND count_usage = TRUE\n GROUP BY user_id\n ",
+ "query": "\n SELECT user_id, COALESCE(SUM(\n COALESCE(\n total::float8,\n (input_tokens::float8 / 1000000.0) * $4\n + (output_tokens::float8 / 1000000.0) * $5\n + (cache_read_input_tokens::float8 / 1000000.0) * $6\n + (cache_write_input_tokens::float8 / 1000000.0) * $7\n )\n ), 0)::float8 AS \"usd!\"\n FROM ai_usage\n WHERE user_id = ANY($1)\n AND created_at >= $2\n AND created_at < $3\n AND count_usage = TRUE\n GROUP BY user_id\n ",
"describe": {
"columns": [
{
@@ -20,6 +20,8 @@
"Timestamptz",
"Timestamptz",
"Float8",
+ "Float8",
+ "Float8",
"Float8"
]
},
@@ -28,5 +30,5 @@
null
]
},
- "hash": "039e91325b454f78eaaba921deee2cda2dd09d78073173b60ff32306d8991024"
+ "hash": "1603df291e3d62f08cb38af3f118b2bb25f306bbba797fbd70ce375e45562322"
}
diff --git a/.sqlx/query-16a038db0df182a666e0717ac7f9243efeeb51622f9bea833501850b099cc291.json b/.sqlx/query-16a038db0df182a666e0717ac7f9243efeeb51622f9bea833501850b099cc291.json
deleted file mode 100644
index d34422565a..0000000000
--- a/.sqlx/query-16a038db0df182a666e0717ac7f9243efeeb51622f9bea833501850b099cc291.json
+++ /dev/null
@@ -1,17 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n INSERT INTO ai_pricing (model, price_per_million_in, price_per_million_out, price_per_audio_minute)\n VALUES ($1, $2, $3, $4)\n ON CONFLICT (model) DO UPDATE\n SET price_per_million_in = EXCLUDED.price_per_million_in,\n price_per_million_out = EXCLUDED.price_per_million_out,\n price_per_audio_minute = EXCLUDED.price_per_audio_minute,\n updated_at = NOW()\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Text",
- "Float4",
- "Float4",
- "Float4"
- ]
- },
- "nullable": []
- },
- "hash": "16a038db0df182a666e0717ac7f9243efeeb51622f9bea833501850b099cc291"
-}
diff --git a/.sqlx/query-2263d555eaf02a3751ad11b4e8dad617b604ff14f25a9f68f996f478fc5a9ae0.json b/.sqlx/query-2263d555eaf02a3751ad11b4e8dad617b604ff14f25a9f68f996f478fc5a9ae0.json
deleted file mode 100644
index 9f0f67b00d..0000000000
--- a/.sqlx/query-2263d555eaf02a3751ad11b4e8dad617b604ff14f25a9f68f996f478fc5a9ae0.json
+++ /dev/null
@@ -1,26 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n INSERT INTO ai_usage (\n id, feature, user_id, entity, model,\n input_tokens, output_tokens, audio_seconds,\n price_per_million_in, price_per_million_out, price_per_audio_minute, total,\n count_usage\n )\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Uuid",
- "Text",
- "Text",
- "Uuid",
- "Text",
- "Int8",
- "Int8",
- "Float8",
- "Float4",
- "Float4",
- "Float4",
- "Float4",
- "Bool"
- ]
- },
- "nullable": []
- },
- "hash": "2263d555eaf02a3751ad11b4e8dad617b604ff14f25a9f68f996f478fc5a9ae0"
-}
diff --git a/.sqlx/query-353b008f819185be1bc3f20cbb7429ad9fe4274375707d9ba2ff9347fddb2b4e.json b/.sqlx/query-353b008f819185be1bc3f20cbb7429ad9fe4274375707d9ba2ff9347fddb2b4e.json
new file mode 100644
index 0000000000..50f5866d56
--- /dev/null
+++ b/.sqlx/query-353b008f819185be1bc3f20cbb7429ad9fe4274375707d9ba2ff9347fddb2b4e.json
@@ -0,0 +1,19 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE ai_usage\n SET price_per_million_in = $2::real,\n price_per_million_out = $3::real,\n price_per_million_cache_read = $4::real,\n price_per_million_cache_write = $5::real,\n price_per_audio_minute = $6::real,\n total = CASE\n WHEN (audio_seconds IS NULL) <> ($6::real IS NULL) THEN NULL\n WHEN cache_read_input_tokens > 0 AND $4::real IS NULL THEN NULL\n WHEN cache_write_input_tokens > 0 AND $5::real IS NULL THEN NULL\n ELSE (input_tokens::real / 1000000.0::real) * $2::real\n + (output_tokens::real / 1000000.0::real) * $3::real\n + (cache_read_input_tokens::real / 1000000.0::real) * COALESCE($4::real, 0)\n + (cache_write_input_tokens::real / 1000000.0::real) * COALESCE($5::real, 0)\n + (COALESCE(audio_seconds, 0) / 60.0 * COALESCE($6::real, 0))::real\n END\n WHERE model = $1\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Text",
+ "Float4",
+ "Float4",
+ "Float4",
+ "Float4",
+ "Float4"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "353b008f819185be1bc3f20cbb7429ad9fe4274375707d9ba2ff9347fddb2b4e"
+}
diff --git a/.sqlx/query-645d4533a5ece189935ffc7859067772f61584ab24b98f4b6a66ba30dbf09ae8.json b/.sqlx/query-645d4533a5ece189935ffc7859067772f61584ab24b98f4b6a66ba30dbf09ae8.json
deleted file mode 100644
index 71b33dff7b..0000000000
--- a/.sqlx/query-645d4533a5ece189935ffc7859067772f61584ab24b98f4b6a66ba30dbf09ae8.json
+++ /dev/null
@@ -1,17 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n UPDATE ai_usage\n SET price_per_million_in = $2::real,\n price_per_million_out = $3::real,\n price_per_audio_minute = $4::real,\n total = CASE WHEN (audio_seconds IS NULL) <> ($4::real IS NULL) THEN NULL\n ELSE (input_tokens::real / 1000000.0::real) * $2::real\n + (output_tokens::real / 1000000.0::real) * $3::real\n + (COALESCE(audio_seconds, 0) / 60.0 * COALESCE($4::real, 0))::real END\n WHERE model = $1\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Text",
- "Float4",
- "Float4",
- "Float4"
- ]
- },
- "nullable": []
- },
- "hash": "645d4533a5ece189935ffc7859067772f61584ab24b98f4b6a66ba30dbf09ae8"
-}
diff --git a/.sqlx/query-68dcb987626d91eaa4526ab79ba646da0d94e452286665dcc1e136468a7b2df6.json b/.sqlx/query-68dcb987626d91eaa4526ab79ba646da0d94e452286665dcc1e136468a7b2df6.json
deleted file mode 100644
index 1510f834bd..0000000000
--- a/.sqlx/query-68dcb987626d91eaa4526ab79ba646da0d94e452286665dcc1e136468a7b2df6.json
+++ /dev/null
@@ -1,34 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT price_per_million_in, price_per_million_out, price_per_audio_minute\n FROM ai_pricing\n WHERE model = $1\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "price_per_million_in",
- "type_info": "Float4"
- },
- {
- "ordinal": 1,
- "name": "price_per_million_out",
- "type_info": "Float4"
- },
- {
- "ordinal": 2,
- "name": "price_per_audio_minute",
- "type_info": "Float4"
- }
- ],
- "parameters": {
- "Left": [
- "Text"
- ]
- },
- "nullable": [
- false,
- false,
- true
- ]
- },
- "hash": "68dcb987626d91eaa4526ab79ba646da0d94e452286665dcc1e136468a7b2df6"
-}
diff --git a/.sqlx/query-a3e224b7264094b0b69a08e904af23acd93fd4c01be59324e197522238240d79.json b/.sqlx/query-a3e224b7264094b0b69a08e904af23acd93fd4c01be59324e197522238240d79.json
new file mode 100644
index 0000000000..2ca844d16d
--- /dev/null
+++ b/.sqlx/query-a3e224b7264094b0b69a08e904af23acd93fd4c01be59324e197522238240d79.json
@@ -0,0 +1,30 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO ai_usage (\n id, feature, user_id, entity, model,\n input_tokens, output_tokens,\n cache_read_input_tokens, cache_write_input_tokens, audio_seconds,\n price_per_million_in, price_per_million_out,\n price_per_million_cache_read, price_per_million_cache_write,\n price_per_audio_minute, total,\n count_usage\n )\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17)\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Text",
+ "Uuid",
+ "Text",
+ "Int8",
+ "Int8",
+ "Int8",
+ "Int8",
+ "Float8",
+ "Float4",
+ "Float4",
+ "Float4",
+ "Float4",
+ "Float4",
+ "Float4",
+ "Bool"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "a3e224b7264094b0b69a08e904af23acd93fd4c01be59324e197522238240d79"
+}
diff --git a/.sqlx/query-40ce78006bc37d7088397407fae17e249fa9f8524426c950480b3df116d6729e.json b/.sqlx/query-aa07da306b358f70b0ab59cfee0fa4b1abbf46fb7f17196c079bce976f470159.json
similarity index 57%
rename from .sqlx/query-40ce78006bc37d7088397407fae17e249fa9f8524426c950480b3df116d6729e.json
rename to .sqlx/query-aa07da306b358f70b0ab59cfee0fa4b1abbf46fb7f17196c079bce976f470159.json
index f2bbc0d167..9f634b564b 100644
--- a/.sqlx/query-40ce78006bc37d7088397407fae17e249fa9f8524426c950480b3df116d6729e.json
+++ b/.sqlx/query-aa07da306b358f70b0ab59cfee0fa4b1abbf46fb7f17196c079bce976f470159.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT\n feature,\n user_id,\n entity,\n model,\n input_tokens,\n output_tokens,\n audio_seconds,\n price_per_million_in,\n price_per_million_out,\n price_per_audio_minute,\n total,\n created_at\n FROM ai_usage\n WHERE ($1::timestamptz IS NULL OR created_at >= $1)\n AND ($2::timestamptz IS NULL OR created_at < $2)\n AND (cardinality($3::text[]) = 0 OR user_id = ANY($3))\n AND (cardinality($4::text[]) = 0 OR feature = ANY($4))\n ORDER BY created_at DESC\n ",
+ "query": "\n SELECT\n feature,\n user_id,\n entity,\n model,\n input_tokens,\n output_tokens,\n cache_read_input_tokens,\n cache_write_input_tokens,\n audio_seconds,\n price_per_million_in,\n price_per_million_out,\n price_per_million_cache_read,\n price_per_million_cache_write,\n price_per_audio_minute,\n total,\n created_at\n FROM ai_usage\n WHERE ($1::timestamptz IS NULL OR created_at >= $1)\n AND ($2::timestamptz IS NULL OR created_at < $2)\n AND (cardinality($3::text[]) = 0 OR user_id = ANY($3))\n AND (cardinality($4::text[]) = 0 OR feature = ANY($4))\n ORDER BY created_at DESC\n ",
"describe": {
"columns": [
{
@@ -35,31 +35,51 @@
},
{
"ordinal": 6,
+ "name": "cache_read_input_tokens",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 7,
+ "name": "cache_write_input_tokens",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 8,
"name": "audio_seconds",
"type_info": "Float8"
},
{
- "ordinal": 7,
+ "ordinal": 9,
"name": "price_per_million_in",
"type_info": "Float4"
},
{
- "ordinal": 8,
+ "ordinal": 10,
"name": "price_per_million_out",
"type_info": "Float4"
},
{
- "ordinal": 9,
+ "ordinal": 11,
+ "name": "price_per_million_cache_read",
+ "type_info": "Float4"
+ },
+ {
+ "ordinal": 12,
+ "name": "price_per_million_cache_write",
+ "type_info": "Float4"
+ },
+ {
+ "ordinal": 13,
"name": "price_per_audio_minute",
"type_info": "Float4"
},
{
- "ordinal": 10,
+ "ordinal": 14,
"name": "total",
"type_info": "Float4"
},
{
- "ordinal": 11,
+ "ordinal": 15,
"name": "created_at",
"type_info": "Timestamptz"
}
@@ -79,6 +99,10 @@
false,
false,
false,
+ false,
+ false,
+ true,
+ true,
true,
true,
true,
@@ -87,5 +111,5 @@
false
]
},
- "hash": "40ce78006bc37d7088397407fae17e249fa9f8524426c950480b3df116d6729e"
+ "hash": "aa07da306b358f70b0ab59cfee0fa4b1abbf46fb7f17196c079bce976f470159"
}
diff --git a/.sqlx/query-c15f78da003195440fc4d66057d71dc89c5eb8803a3e90aa742e7399cb591614.json b/.sqlx/query-c15f78da003195440fc4d66057d71dc89c5eb8803a3e90aa742e7399cb591614.json
new file mode 100644
index 0000000000..dc47a3a0af
--- /dev/null
+++ b/.sqlx/query-c15f78da003195440fc4d66057d71dc89c5eb8803a3e90aa742e7399cb591614.json
@@ -0,0 +1,19 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO ai_pricing (\n model, price_per_million_in, price_per_million_out,\n price_per_million_cache_read, price_per_million_cache_write,\n price_per_audio_minute\n )\n VALUES ($1, $2, $3, $4, $5, $6)\n ON CONFLICT (model) DO UPDATE\n SET price_per_million_in = EXCLUDED.price_per_million_in,\n price_per_million_out = EXCLUDED.price_per_million_out,\n price_per_million_cache_read = EXCLUDED.price_per_million_cache_read,\n price_per_million_cache_write = EXCLUDED.price_per_million_cache_write,\n price_per_audio_minute = EXCLUDED.price_per_audio_minute,\n updated_at = NOW()\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Text",
+ "Float4",
+ "Float4",
+ "Float4",
+ "Float4",
+ "Float4"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "c15f78da003195440fc4d66057d71dc89c5eb8803a3e90aa742e7399cb591614"
+}
diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/index.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/index.ts
index 459d7f9228..e12c4b8748 100644
--- a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/index.ts
+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/index.ts
@@ -187,6 +187,8 @@ export * from './pipedreamUpdateRequestEnabled';
export * from './pipedreamUpdateRequestServerName';
export * from './price';
export * from './pricePricePerAudioMinute';
+export * from './pricePricePerMillionCacheRead';
+export * from './pricePricePerMillionCacheWrite';
export * from './projectionStateResponse';
export * from './projectionStateResponseData';
export * from './projectionStateResponseError';
@@ -207,6 +209,8 @@ export * from './sendChatMessageResponse';
export * from './serverResponse';
export * from './setPricingRequest';
export * from './setPricingRequestPricePerAudioMinute';
+export * from './setPricingRequestPricePerMilCacheRead';
+export * from './setPricingRequestPricePerMilCacheWrite';
export * from './setPricingRequestPricePerMilIn';
export * from './setPricingRequestPricePerMilOut';
export * from './sharePermissionV2';
diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/price.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/price.ts
index f5023ea004..aef725f8d8 100644
--- a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/price.ts
+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/price.ts
@@ -5,6 +5,8 @@
* OpenAPI spec version: 1.0.0
*/
import type { PricePricePerAudioMinute } from './pricePricePerAudioMinute';
+import type { PricePricePerMillionCacheRead } from './pricePricePerMillionCacheRead';
+import type { PricePricePerMillionCacheWrite } from './pricePricePerMillionCacheWrite';
/**
* Rates applied to an invocation and its resolved dollar cost.
@@ -12,6 +14,12 @@ import type { PricePricePerAudioMinute } from './pricePricePerAudioMinute';
export interface Price {
/** Price per audio minute (USD), absent for token billing. */
price_per_audio_minute?: PricePricePerAudioMinute;
+ /** Price per million cache-read input tokens (USD), absent when the model
+has no published rate and for audio billing. */
+ price_per_million_cache_read?: PricePricePerMillionCacheRead;
+ /** Price per million cache-write input tokens (USD), absent when the model
+has no published rate and for audio billing. */
+ price_per_million_cache_write?: PricePricePerMillionCacheWrite;
/** Price per million input tokens (USD); zero for audio billing. */
price_per_million_in: number;
/** Price per million output tokens (USD); zero for audio billing. */
diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/pricePricePerMillionCacheRead.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/pricePricePerMillionCacheRead.ts
new file mode 100644
index 0000000000..be03462d96
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/pricePricePerMillionCacheRead.ts
@@ -0,0 +1,12 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * Document Cognition Service
+ * OpenAPI spec version: 1.0.0
+ */
+
+/**
+ * Price per million cache-read input tokens (USD), absent when the model
+has no published rate and for audio billing.
+ */
+export type PricePricePerMillionCacheRead = number | null;
diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/pricePricePerMillionCacheWrite.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/pricePricePerMillionCacheWrite.ts
new file mode 100644
index 0000000000..1d6481b30f
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/pricePricePerMillionCacheWrite.ts
@@ -0,0 +1,12 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * Document Cognition Service
+ * OpenAPI spec version: 1.0.0
+ */
+
+/**
+ * Price per million cache-write input tokens (USD), absent when the model
+has no published rate and for audio billing.
+ */
+export type PricePricePerMillionCacheWrite = number | null;
diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/setPricingRequest.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/setPricingRequest.ts
index 5082dc795b..9aee8d54e7 100644
--- a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/setPricingRequest.ts
+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/setPricingRequest.ts
@@ -5,6 +5,8 @@
* OpenAPI spec version: 1.0.0
*/
import type { SetPricingRequestPricePerAudioMinute } from './setPricingRequestPricePerAudioMinute';
+import type { SetPricingRequestPricePerMilCacheRead } from './setPricingRequestPricePerMilCacheRead';
+import type { SetPricingRequestPricePerMilCacheWrite } from './setPricingRequestPricePerMilCacheWrite';
import type { SetPricingRequestPricePerMilIn } from './setPricingRequestPricePerMilIn';
import type { SetPricingRequestPricePerMilOut } from './setPricingRequestPricePerMilOut';
@@ -16,6 +18,12 @@ export interface SetPricingRequest {
model: string;
/** Price per minute of audio (USD), or null for token-only pricing. */
price_per_audio_minute?: SetPricingRequestPricePerAudioMinute;
+ /** Price per million cache-read input tokens (USD). Omit it when the model
+has no published rate: calls that report cache reads then stay unpriced. */
+ price_per_mil_cache_read?: SetPricingRequestPricePerMilCacheRead;
+ /** Price per million cache-write input tokens (USD). Omit it when the model
+has no published rate: calls that report cache writes then stay unpriced. */
+ price_per_mil_cache_write?: SetPricingRequestPricePerMilCacheWrite;
/** New price per million input tokens (USD). Required for token pricing. */
price_per_mil_in?: SetPricingRequestPricePerMilIn;
/** New price per million output tokens (USD). Required for token pricing. */
diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/setPricingRequestPricePerMilCacheRead.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/setPricingRequestPricePerMilCacheRead.ts
new file mode 100644
index 0000000000..c5778cdcdc
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/setPricingRequestPricePerMilCacheRead.ts
@@ -0,0 +1,12 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * Document Cognition Service
+ * OpenAPI spec version: 1.0.0
+ */
+
+/**
+ * Price per million cache-read input tokens (USD). Omit it when the model
+has no published rate: calls that report cache reads then stay unpriced.
+ */
+export type SetPricingRequestPricePerMilCacheRead = number | null;
diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/setPricingRequestPricePerMilCacheWrite.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/setPricingRequestPricePerMilCacheWrite.ts
new file mode 100644
index 0000000000..0a30bd7568
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/setPricingRequestPricePerMilCacheWrite.ts
@@ -0,0 +1,12 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * Document Cognition Service
+ * OpenAPI spec version: 1.0.0
+ */
+
+/**
+ * Price per million cache-write input tokens (USD). Omit it when the model
+has no published rate: calls that report cache writes then stay unpriced.
+ */
+export type SetPricingRequestPricePerMilCacheWrite = number | null;
diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/usage.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/usage.ts
index d80af98e77..f0e0a5e1b3 100644
--- a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/usage.ts
+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/usage.ts
@@ -13,10 +13,20 @@ import type { UsagePrice } from './usagePrice';
export interface Usage {
/** Audio duration in seconds, absent for token billing. */
audio_seconds?: UsageAudioSeconds;
+ /**
+ * Input tokens read from a prompt cache; zero for audio billing.
+ * @minimum 0
+ */
+ cache_read_input_tokens: number;
+ /**
+ * Input tokens written to a prompt cache; zero for audio billing.
+ * @minimum 0
+ */
+ cache_write_input_tokens: number;
/** Recording timestamp. */
created_at: string;
/**
- * Input tokens; zero for audio billing.
+ * Uncached input tokens; zero for audio billing.
* @minimum 0
*/
input_tokens: number;
diff --git a/apps/web/src/lib/service-clients/service-cognition/openapi.json b/apps/web/src/lib/service-clients/service-cognition/openapi.json
index 404f2e14b7..634f37e3da 100644
--- a/apps/web/src/lib/service-clients/service-cognition/openapi.json
+++ b/apps/web/src/lib/service-clients/service-cognition/openapi.json
@@ -4280,6 +4280,16 @@
"format": "float",
"description": "Price per audio minute (USD), absent for token billing."
},
+ "price_per_million_cache_read": {
+ "type": ["number", "null"],
+ "format": "float",
+ "description": "Price per million cache-read input tokens (USD), absent when the model\nhas no published rate and for audio billing."
+ },
+ "price_per_million_cache_write": {
+ "type": ["number", "null"],
+ "format": "float",
+ "description": "Price per million cache-write input tokens (USD), absent when the model\nhas no published rate and for audio billing."
+ },
"price_per_million_in": {
"type": "number",
"format": "float",
@@ -4513,6 +4523,16 @@
"format": "float",
"description": "Price per minute of audio (USD), or null for token-only pricing."
},
+ "price_per_mil_cache_read": {
+ "type": ["number", "null"],
+ "format": "float",
+ "description": "Price per million cache-read input tokens (USD). Omit it when the model\nhas no published rate: calls that report cache reads then stay unpriced."
+ },
+ "price_per_mil_cache_write": {
+ "type": ["number", "null"],
+ "format": "float",
+ "description": "Price per million cache-write input tokens (USD). Omit it when the model\nhas no published rate: calls that report cache writes then stay unpriced."
+ },
"price_per_mil_in": {
"type": ["number", "null"],
"format": "float",
@@ -5103,13 +5123,32 @@
"Usage": {
"type": "object",
"description": "Measured usage for one invocation.",
- "required": ["input_tokens", "output_tokens", "model", "created_at"],
+ "required": [
+ "input_tokens",
+ "output_tokens",
+ "cache_read_input_tokens",
+ "cache_write_input_tokens",
+ "model",
+ "created_at"
+ ],
"properties": {
"audio_seconds": {
"type": ["number", "null"],
"format": "double",
"description": "Audio duration in seconds, absent for token billing."
},
+ "cache_read_input_tokens": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Input tokens read from a prompt cache; zero for audio billing.",
+ "minimum": 0
+ },
+ "cache_write_input_tokens": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Input tokens written to a prompt cache; zero for audio billing.",
+ "minimum": 0
+ },
"created_at": {
"type": "string",
"format": "date-time",
@@ -5118,7 +5157,7 @@
"input_tokens": {
"type": "integer",
"format": "int64",
- "description": "Input tokens; zero for audio billing.",
+ "description": "Uncached input tokens; zero for audio billing.",
"minimum": 0
},
"model": {
diff --git a/crates/agent/src/completion.rs b/crates/agent/src/completion.rs
index 584f7cfc76..096e2410b3 100644
--- a/crates/agent/src/completion.rs
+++ b/crates/agent/src/completion.rs
@@ -5,8 +5,9 @@
#[cfg(test)]
mod test;
-use crate::model::metering::MeteringContext;
+use crate::model::metering::{MeteringContext, WireProtocol};
use crate::model::router::{ModelRouter, RoutedModel};
+use crate::model::usage_amount::usage_amount;
use crate::telemetry::{ChatSpanHook, GenAiContext, TracedModel};
use ai_usage::{UsageContext, UsageRecorder};
use genai_telemetry::ContentPolicy;
@@ -42,6 +43,7 @@ pub async fn complete<M: ToString>(
let model = model.to_string();
let routed = ModelRouter::shared()?.route_or_default(&model);
let telemetry = telemetry_for(&ctx, &routed);
+ let protocol = routed.protocol();
let response = match routed {
RoutedModel::Anthropic(m) => {
prompt_once(
@@ -80,7 +82,7 @@ pub async fn complete<M: ToString>(
.await?
}
};
- record(recorder, ctx, model, &response);
+ record(recorder, ctx, model, protocol, &response);
Ok(response.output)
})
.await
@@ -104,6 +106,7 @@ pub async fn complete_with_history<M: ToString>(
let model = model.to_string();
let routed = ModelRouter::shared()?.route_or_default(&model);
let telemetry = telemetry_for(&ctx, &routed);
+ let protocol = routed.protocol();
let response = match routed {
RoutedModel::Anthropic(m) => {
prompt_with_history(
@@ -142,7 +145,7 @@ pub async fn complete_with_history<M: ToString>(
.await?
}
};
- record(recorder, ctx, model, &response);
+ record(recorder, ctx, model, protocol, &response);
Ok(response.output)
})
.await
@@ -205,13 +208,10 @@ fn record(
recorder: &dyn UsageRecorder,
ctx: UsageContext,
model: String,
+ protocol: WireProtocol,
response: &PromptResponse,
) {
- recorder.record(ctx.into_event(
- model,
- response.usage.input_tokens,
- response.usage.output_tokens,
- ));
+ recorder.record(ctx.into_event(model, usage_amount(protocol, &response.usage)));
}
/// Build a toolless agent and prompt it with a single user message.
diff --git a/crates/agent/src/model/mod.rs b/crates/agent/src/model/mod.rs
index 54545f91f7..e48fc49230 100644
--- a/crates/agent/src/model/mod.rs
+++ b/crates/agent/src/model/mod.rs
@@ -12,3 +12,4 @@ pub mod metering;
pub mod metering_http;
mod openai;
pub mod router;
+pub(crate) mod usage_amount;
diff --git a/crates/agent/src/model/router.rs b/crates/agent/src/model/router.rs
index 86bd4590a8..d68561ecd0 100644
--- a/crates/agent/src/model/router.rs
+++ b/crates/agent/src/model/router.rs
@@ -40,6 +40,7 @@ use super::metering::{MeteringContext, WireProtocol};
use super::metering_http::MeteredHttpClient;
use super::openai::{OpenAiChatCompletionsModel, OpenAiResponsesModel};
use super::types::Model;
+use super::usage_amount::usage_amount;
use super::{PredefinedModel, ReasoningEffort};
use crate::error::AgentError;
use crate::hook::{BridgeInputs, StreamBridge};
@@ -97,6 +98,16 @@ impl<'a, H: HttpClientExt + Clone + Default + std::fmt::Debug + 'static> RoutedM
}
}
+ /// The wire protocol the provider speaks, which fixes its usage semantics.
+ pub(crate) fn protocol(&self) -> WireProtocol {
+ match self {
+ RoutedModel::Anthropic(_) => WireProtocol::Anthropic,
+ RoutedModel::Gemini(_) => WireProtocol::Gemini,
+ RoutedModel::OpenAiChatCompletions(_) => WireProtocol::ChatCompletions,
+ RoutedModel::OpenAiResponses(_) => WireProtocol::Responses,
+ }
+ }
+
/// The bare model name sent to the provider.
pub(crate) fn model_name(&self) -> &str {
match self {
@@ -214,6 +225,7 @@ impl<H: HttpClientExt + Clone + Default + std::fmt::Debug + 'static> ProviderAge
ProviderAgent::Anthropic(agent) => {
drive_stream(
agent,
+ WireProtocol::Anthropic,
prompt,
history,
max_turns,
@@ -229,6 +241,7 @@ impl<H: HttpClientExt + Clone + Default + std::fmt::Debug + 'static> ProviderAge
ProviderAgent::Gemini(agent) => {
drive_stream(
agent,
+ WireProtocol::Gemini,
prompt,
history,
max_turns,
@@ -244,6 +257,7 @@ impl<H: HttpClientExt + Clone + Default + std::fmt::Debug + 'static> ProviderAge
ProviderAgent::OpenAiChatCompletions(agent) => {
drive_stream(
agent,
+ WireProtocol::ChatCompletions,
prompt,
history,
max_turns,
@@ -259,6 +273,7 @@ impl<H: HttpClientExt + Clone + Default + std::fmt::Debug + 'static> ProviderAge
ProviderAgent::OpenAiResponses(agent) => {
drive_stream(
agent,
+ WireProtocol::Responses,
prompt,
history,
max_turns,
@@ -530,6 +545,7 @@ fn build_agent<M: CompletionModel>(
#[allow(clippy::too_many_arguments)]
async fn drive_stream<M>(
agent: &Agent<M>,
+ protocol: WireProtocol,
prompt: Message,
history: Vec<Message>,
max_turns: usize,
@@ -698,11 +714,11 @@ where
);
// Aggregate analytics only. Financial evidence is
// persisted per HTTP attempt before SDK parsing.
- recorder.record(usage_ctx.clone().into_event(
- model.clone(),
- usage.input_tokens,
- usage.output_tokens,
- ));
+ recorder.record(
+ usage_ctx
+ .clone()
+ .into_event(model.clone(), usage_amount(protocol, &usage)),
+ );
let _ =
driver_tx.send(Ok(StreamPart::Usage(crate::stream::Usage {
input_tokens: usage.input_tokens,
@@ -811,8 +827,10 @@ where
) -> std::pin::Pin<
Box<dyn std::future::Future<Output = ChatCompletionStream<'static>> + Send + 'a>,
> {
+ // Scripted fakes report Anthropic-shaped usage: input excludes cache.
Box::pin(drive_stream(
self,
+ WireProtocol::Anthropic,
prompt,
history,
max_turns,
diff --git a/crates/agent/src/model/usage_amount.rs b/crates/agent/src/model/usage_amount.rs
new file mode 100644
index 0000000000..ea6a69f837
--- /dev/null
+++ b/crates/agent/src/model/usage_amount.rs
@@ -0,0 +1,59 @@
+//! Normalize a provider's reported token usage into the disjoint billing
+//! dimensions of [`UsageAmount::Tokens`].
+//!
+//! rig fills [`Usage`] with each provider's own counters, and the providers
+//! disagree on whether input includes cache traffic.
+
+use ai_usage::UsageAmount;
+use rig_core::completion::Usage;
+
+use super::metering::WireProtocol;
+
+#[cfg(test)]
+mod test;
+
+/// The billable token amount of one completion reported over `protocol`.
+pub(crate) fn usage_amount(protocol: WireProtocol, usage: &Usage) -> UsageAmount {
+ let cache_read = usage.cached_input_tokens;
+ let cache_write = usage.cache_creation_input_tokens;
+ match protocol {
+ // Anthropic input already excludes cache reads and writes, and its
+ // output already counts thinking.
+ WireProtocol::Anthropic => UsageAmount::Tokens {
+ input: usage.input_tokens,
+ output: usage.output_tokens,
+ cache_read,
+ cache_write,
+ },
+ // OpenAI-style input includes cached tokens; output includes reasoning.
+ WireProtocol::Responses | WireProtocol::ChatCompletions => UsageAmount::Tokens {
+ input: uncached_input(protocol, usage),
+ output: usage.output_tokens,
+ cache_read,
+ cache_write,
+ },
+ // Gemini's prompt count includes cached content, and its candidates
+ // exclude thoughts, which bill at the output rate.
+ WireProtocol::Gemini => UsageAmount::Tokens {
+ input: uncached_input(protocol, usage),
+ output: usage.output_tokens + usage.reasoning_tokens,
+ cache_read,
+ cache_write,
+ },
+ }
+}
+
+/// Input that includes cache traffic, minus that traffic.
+fn uncached_input(protocol: WireProtocol, usage: &Usage) -> u64 {
+ let cached = usage.cached_input_tokens + usage.cache_creation_input_tokens;
+ usage.input_tokens.checked_sub(cached).unwrap_or_else(|| {
+ tracing::warn!(
+ ?protocol,
+ input_tokens = usage.input_tokens,
+ cache_read = usage.cached_input_tokens,
+ cache_write = usage.cache_creation_input_tokens,
+ "provider reported more cached than total input tokens"
+ );
+ 0
+ })
+}
diff --git a/crates/agent/src/model/usage_amount/test.rs b/crates/agent/src/model/usage_amount/test.rs
new file mode 100644
index 0000000000..55e948351b
--- /dev/null
+++ b/crates/agent/src/model/usage_amount/test.rs
@@ -0,0 +1,116 @@
+use super::*;
+
+#[test]
+fn anthropic_input_already_excludes_cache_reads_and_writes() {
+ let usage = Usage {
+ input_tokens: 1_200,
+ output_tokens: 300,
+ total_tokens: 147_500,
+ cached_input_tokens: 140_000,
+ cache_creation_input_tokens: 6_000,
+ tool_use_prompt_tokens: 0,
+ reasoning_tokens: 0,
+ };
+
+ assert_eq!(
+ usage_amount(WireProtocol::Anthropic, &usage),
+ UsageAmount::Tokens {
+ input: 1_200,
+ output: 300,
+ cache_read: 140_000,
+ cache_write: 6_000,
+ }
+ );
+}
+
+#[test]
+fn openai_responses_input_includes_cached_tokens_so_they_are_split_out() {
+ let usage = Usage {
+ input_tokens: 150_000,
+ output_tokens: 500,
+ total_tokens: 150_500,
+ cached_input_tokens: 140_000,
+ cache_creation_input_tokens: 0,
+ tool_use_prompt_tokens: 0,
+ reasoning_tokens: 200,
+ };
+
+ assert_eq!(
+ usage_amount(WireProtocol::Responses, &usage),
+ UsageAmount::Tokens {
+ input: 10_000,
+ output: 500,
+ cache_read: 140_000,
+ cache_write: 0,
+ }
+ );
+}
+
+#[test]
+fn chat_completions_prompt_tokens_include_cached_tokens_so_they_are_split_out() {
+ let usage = Usage {
+ input_tokens: 8_000,
+ output_tokens: 120,
+ total_tokens: 8_120,
+ cached_input_tokens: 6_000,
+ cache_creation_input_tokens: 0,
+ tool_use_prompt_tokens: 0,
+ reasoning_tokens: 0,
+ };
+
+ assert_eq!(
+ usage_amount(WireProtocol::ChatCompletions, &usage),
+ UsageAmount::Tokens {
+ input: 2_000,
+ output: 120,
+ cache_read: 6_000,
+ cache_write: 0,
+ }
+ );
+}
+
+#[test]
+fn gemini_prompt_includes_cached_tokens_and_candidates_exclude_thoughts() {
+ let usage = Usage {
+ input_tokens: 50_000,
+ output_tokens: 400,
+ total_tokens: 51_400,
+ cached_input_tokens: 30_000,
+ cache_creation_input_tokens: 0,
+ tool_use_prompt_tokens: 0,
+ reasoning_tokens: 1_000,
+ };
+
+ assert_eq!(
+ usage_amount(WireProtocol::Gemini, &usage),
+ UsageAmount::Tokens {
+ input: 20_000,
+ output: 1_400,
+ cache_read: 30_000,
+ cache_write: 0,
+ }
+ );
+}
+
+#[test]
+fn inclusive_input_smaller_than_its_cache_share_does_not_underflow() {
+ let usage = Usage {
+ input_tokens: 10,
+ output_tokens: 5,
+ total_tokens: 15,
+ cached_input_tokens: 40,
+ cache_creation_input_tokens: 0,
+ tool_use_prompt_tokens: 0,
+ reasoning_tokens: 0,
+ };
+
+ assert_eq!(
+ usage_amount(WireProtocol::Responses, &usage),
+ UsageAmount::Tokens {
+ input: 0,
+ output: 5,
+ cache_read: 40,
+ cache_write: 0,
+ }
+ );
+}
diff --git a/crates/ai_billing/src/composition/test.rs b/crates/ai_billing/src/composition/test.rs
index fa7524d1ec..8fc1729e3e 100644
--- a/crates/ai_billing/src/composition/test.rs
+++ b/crates/ai_billing/src/composition/test.rs
@@ -31,7 +31,15 @@ async fn configured_recorders_preserve_analytics_and_count_only_enabled_billable
(enabled, UsageContext::system(AiFeature::Chat)),
] {
assert!(recorder.tracking().is_some());
- recorder.record(context.into_event("claude-opus-5".into(), 1_000_000, 0));
+ recorder.record(context.into_event(
+ "claude-opus-5".into(),
+ ai_usage::UsageAmount::Tokens {
+ input: 1_000_000,
+ output: 0,
+ cache_read: 0,
+ cache_write: 0,
+ },
+ ));
}
let analytics = ai_usage::outbound::PgUsageRepo::new(pool.clone());
diff --git a/crates/ai_billing/src/outbound/pg_usage_reader.rs b/crates/ai_billing/src/outbound/pg_usage_reader.rs
index 3baf15c993..cddbbd9029 100644
--- a/crates/ai_billing/src/outbound/pg_usage_reader.rs
+++ b/crates/ai_billing/src/outbound/pg_usage_reader.rs
@@ -12,10 +12,14 @@ use sqlx::PgPool;
///
/// These mirror the `claude-opus-5` row seeded into `ai_pricing` by
/// `20260724182218_seed_claude_opus_5_pricing.sql` ($5 in / $25 out per
-/// million tokens), the dearest model the picker offered when the fallback
-/// was chosen. Keep them in step with that seed.
+/// million tokens) and its cache rates from
+/// `20261005221204_ai_prompt_cache_pricing.sql` ($0.50 read / $6.25 write),
+/// the dearest model the picker offered when the fallback was chosen. Keep
+/// them in step with those seeds.
const FALLBACK_PRICE_PER_MILLION_IN: f64 = 5.0;
const FALLBACK_PRICE_PER_MILLION_OUT: f64 = 25.0;
+const FALLBACK_PRICE_PER_MILLION_CACHE_READ: f64 = 0.5;
+const FALLBACK_PRICE_PER_MILLION_CACHE_WRITE: f64 = 6.25;
/// Postgres-backed [`UsageReader`] over the `ai_usage` table.
#[derive(Clone)]
@@ -47,6 +51,8 @@ impl UsageReader for PgUsageReader {
total::float8,
(input_tokens::float8 / 1000000.0) * $4
+ (output_tokens::float8 / 1000000.0) * $5
+ + (cache_read_input_tokens::float8 / 1000000.0) * $6
+ + (cache_write_input_tokens::float8 / 1000000.0) * $7
)
), 0)::float8 AS "usd!"
FROM ai_usage
@@ -61,6 +67,8 @@ impl UsageReader for PgUsageReader {
period.end,
FALLBACK_PRICE_PER_MILLION_IN,
FALLBACK_PRICE_PER_MILLION_OUT,
+ FALLBACK_PRICE_PER_MILLION_CACHE_READ,
+ FALLBACK_PRICE_PER_MILLION_CACHE_WRITE,
)
.fetch_all(&self.pool)
.await
diff --git a/crates/ai_billing/src/outbound/pg_usage_reader/test.rs b/crates/ai_billing/src/outbound/pg_usage_reader/test.rs
index 772e47b01e..202ba64d5f 100644
--- a/crates/ai_billing/src/outbound/pg_usage_reader/test.rs
+++ b/crates/ai_billing/src/outbound/pg_usage_reader/test.rs
@@ -19,12 +19,16 @@ fn completion(
amount: UsageAmount::Tokens {
input: 1_000_000,
output: 0,
+ cache_read: 0,
+ cache_write: 0,
},
model: "test-model".to_string(),
price: Some(Price {
pricing: ModelPricing::Tokens {
input: provider_cost_usd,
output: 0.0,
+ cache_read: None,
+ cache_write: None,
},
total: provider_cost_usd,
}),
@@ -80,6 +84,8 @@ async fn aggregates_only_persisted_counted_rows_for_requested_seats(pool: PgPool
unpriced.cost.amount = UsageAmount::Tokens {
input: 1_000_000,
output: 1_000_000,
+ cache_read: 0,
+ cache_write: 0,
};
repo.insert_usage(&unpriced, true).await.unwrap(); // $5 input + $25 output
repo.insert_usage(&unpriced, false).await.unwrap();
@@ -281,3 +287,94 @@ async fn unpriced_free_features_are_excluded_from_fallback_billing(pool: PgPool)
}]
);
}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn priced_cache_tokens_count_through_the_stored_total(pool: PgPool) {
+ let user = MacroUserIdStr::try_from("macro|[email protected]".to_string()).unwrap();
+ let amount = UsageAmount::Tokens {
+ input: 1_000_000,
+ output: 0,
+ cache_read: 1_000_000,
+ cache_write: 1_000_000,
+ };
+ PgUsageRepo::new(pool.clone())
+ .insert_usage(
+ &CompletionUsage {
+ feature: AiFeature::Chat,
+ user: user.clone(),
+ entity: None,
+ cost: Usage {
+ amount,
+ model: "claude-opus-5".to_string(),
+ price: Price::compute(
+ ModelPricing::Tokens {
+ input: 5.0,
+ output: 25.0,
+ cache_read: Some(0.5),
+ cache_write: Some(6.25),
+ },
+ amount,
+ ),
+ created_at: Utc::now(),
+ },
+ },
+ true,
+ )
+ .await
+ .unwrap();
+
+ let usage = PgUsageReader::new(pool)
+ .usage_cost_cents_by_user(std::slice::from_ref(&user), current_period())
+ .await
+ .unwrap();
+
+ // $5 input + $0.50 cache read + $6.25 cache write.
+ assert_eq!(
+ usage,
+ vec![SeatUsage {
+ user,
+ used_cents: 1_175,
+ }]
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn unpriced_cache_tokens_fall_back_to_the_opus_5_cache_rates(pool: PgPool) {
+ let user = MacroUserIdStr::try_from("macro|[email protected]".to_string()).unwrap();
+ PgUsageRepo::new(pool.clone())
+ .insert_usage(
+ &CompletionUsage {
+ feature: AiFeature::Chat,
+ user: user.clone(),
+ entity: None,
+ cost: Usage {
+ amount: UsageAmount::Tokens {
+ input: 1_000_000,
+ output: 0,
+ cache_read: 1_000_000,
+ cache_write: 1_000_000,
+ },
+ model: "unpriced-cache-model".to_string(),
+ price: None,
+ created_at: Utc::now(),
+ },
+ },
+ true,
+ )
+ .await
+ .unwrap();
+
+ let usage = PgUsageReader::new(pool)
+ .usage_cost_cents_by_user(std::slice::from_ref(&user), current_period())
+ .await
+ .unwrap();
+
+ // $5 input + $0.50 cache read + $6.25 cache write at the Opus 5 rates.
+ assert_eq!(
+ usage,
+ vec![SeatUsage {
+ user,
+ used_cents: 1_175,
+ }]
+ );
+}
diff --git a/crates/ai_billing/src/outbound/settling_recorder/test.rs b/crates/ai_billing/src/outbound/settling_recorder/test.rs
index f770e68f8f..a3bd815441 100644
--- a/crates/ai_billing/src/outbound/settling_recorder/test.rs
+++ b/crates/ai_billing/src/outbound/settling_recorder/test.rs
@@ -175,7 +175,15 @@ async fn record_with_policy(
settlement,
);
let should_count = enforcement.should_count(&user, feature);
- recorder.record(UsageContext::new(feature, user).into_event("test-model".into(), 10, 10));
+ recorder.record(UsageContext::new(feature, user).into_event(
+ "test-model".into(),
+ ai_usage::UsageAmount::Tokens {
+ input: 10,
+ output: 10,
+ cache_read: 0,
+ cache_write: 0,
+ },
+ ));
tokio::time::timeout(Duration::from_secs(2), repo.recorded.notified())
.await
.expect("usage must be recorded under either settlement policy, including after a retry");
diff --git a/crates/ai_usage/src/domain/ports.rs b/crates/ai_usage/src/domain/ports.rs
index 057ff8ee9f..48ba362857 100644
--- a/crates/ai_usage/src/domain/ports.rs
+++ b/crates/ai_usage/src/domain/ports.rs
@@ -93,12 +93,17 @@ pub fn normalize_model_id(model: &str) -> &str {
/// The billable quantity for one AI invocation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UsageAmount {
- /// Token-based inference.
+ /// Token-based inference. The dimensions are disjoint: a token counted in
+ /// one is never counted in another, whatever the provider's own reporting.
Tokens {
- /// Tokens consumed by the input.
+ /// Input tokens neither read from nor written to a prompt cache.
input: u64,
- /// Tokens generated in the output.
+ /// Tokens generated in the output, including reasoning.
output: u64,
+ /// Input tokens read from a prompt cache.
+ cache_read: u64,
+ /// Input tokens written to a prompt cache.
+ cache_write: u64,
},
/// Duration-based audio inference.
Audio {
@@ -110,12 +115,16 @@ pub enum UsageAmount {
/// Rates for a model's billing unit.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ModelPricing {
- /// Prices per million input/output tokens (USD).
+ /// Prices per million tokens (USD), one per [`UsageAmount::Tokens`] dimension.
Tokens {
- /// Input token rate.
+ /// Uncached input token rate.
input: f32,
/// Output token rate.
output: f32,
+ /// Cache read rate, or `None` when the model has no published one.
+ cache_read: Option<f32>,
+ /// Cache write rate, or `None` when the model has no published one.
+ cache_write: Option<f32>,
},
/// Price per minute of audio (USD).
Audio {
@@ -129,7 +138,17 @@ impl ModelPricing {
pub fn validate(self) -> Result<Self> {
let valid = |rate: f32| rate.is_finite() && rate >= 0.0;
let is_valid = match self {
- Self::Tokens { input, output } => valid(input) && valid(output),
+ Self::Tokens {
+ input,
+ output,
+ cache_read,
+ cache_write,
+ } => {
+ valid(input)
+ && valid(output)
+ && cache_read.is_none_or(valid)
+ && cache_write.is_none_or(valid)
+ }
Self::Audio { per_minute } => valid(per_minute),
};
if is_valid {
@@ -150,18 +169,28 @@ pub struct Price {
}
impl Price {
- /// Compute cost only when the rate and usage use the same billing unit.
+ /// Compute cost only when the rate and usage use the same billing unit and
+ /// every dimension with usage has a rate.
pub fn compute(pricing: ModelPricing, amount: UsageAmount) -> Option<Self> {
let total = match (pricing, amount) {
(
- ModelPricing::Tokens { input, output },
+ ModelPricing::Tokens {
+ input,
+ output,
+ cache_read,
+ cache_write,
+ },
UsageAmount::Tokens {
input: input_tokens,
output: output_tokens,
+ cache_read: cache_read_tokens,
+ cache_write: cache_write_tokens,
},
) => {
- input_tokens as f64 / 1_000_000.0 * f64::from(input)
- + output_tokens as f64 / 1_000_000.0 * f64::from(output)
+ per_million(input_tokens, Some(input))?
+ + per_million(output_tokens, Some(output))?
+ + per_million(cache_read_tokens, cache_read)?
+ + per_million(cache_write_tokens, cache_write)?
}
(ModelPricing::Audio { per_minute }, UsageAmount::Audio { duration }) => {
duration.as_secs_f64() / 60.0 * f64::from(per_minute)
@@ -175,6 +204,16 @@ impl Price {
}
}
+/// The cost of `tokens` at a per-million `rate`. A dimension with tokens but no
+/// rate cannot be priced; one with no tokens needs no rate.
+fn per_million(tokens: u64, rate: Option<f32>) -> Option<f64> {
+ match (tokens, rate) {
+ (0, _) => Some(0.0),
+ (tokens, Some(rate)) => Some(tokens as f64 / 1_000_000.0 * f64::from(rate)),
+ (_, None) => None,
+ }
+}
+
/// The measured usage and resolved cost of a single AI call.
#[derive(Debug, Clone)]
pub struct Usage {
@@ -292,17 +331,15 @@ impl UsageContext {
}
/// Build a [`UsageEvent`] from this context plus a completion's model and
- /// token counts.
- pub fn into_event(self, model: String, input_tokens: u64, output_tokens: u64) -> UsageEvent {
+ /// measured usage. Token callers normalize their provider's counters into
+ /// disjoint [`UsageAmount::Tokens`] dimensions first.
+ pub fn into_event(self, model: String, amount: UsageAmount) -> UsageEvent {
UsageEvent {
feature: self.feature,
user: self.user,
entity: self.entity,
model,
- amount: UsageAmount::Tokens {
- input: input_tokens,
- output: output_tokens,
- },
+ amount,
}
}
diff --git a/crates/ai_usage/src/domain/service.rs b/crates/ai_usage/src/domain/service.rs
index c33cca6877..0f199b1b3c 100644
--- a/crates/ai_usage/src/domain/service.rs
+++ b/crates/ai_usage/src/domain/service.rs
@@ -83,6 +83,12 @@ where
if let Some(pricing) = repo.get_pricing(&model).await? {
cost.price = Price::compute(pricing, cost.amount);
+ if cost.price.is_none() {
+ tracing::error!(
+ ?pricing,
+ "ai usage left unpriced: the model has no rate for a dimension it used"
+ );
+ }
}
let span = tracing::Span::current();
span.record("usage.priced", cost.price.is_some());
diff --git a/crates/ai_usage/src/domain/service/test.rs b/crates/ai_usage/src/domain/service/test.rs
index 8646da22b6..cf1a304cb1 100644
--- a/crates/ai_usage/src/domain/service/test.rs
+++ b/crates/ai_usage/src/domain/service/test.rs
@@ -14,12 +14,16 @@ fn completion(feature: AiFeature, total: Option<f32>) -> CompletionUsage {
amount: UsageAmount::Tokens {
input: 1000,
output: 500,
+ cache_read: 0,
+ cache_write: 0,
},
model: "claude-opus-4-8".to_string(),
price: total.map(|t| Price {
pricing: ModelPricing::Tokens {
input: 5.0,
output: 25.0,
+ cache_read: None,
+ cache_write: None,
},
total: t,
}),
@@ -34,6 +38,8 @@ fn price_compute_uses_per_million_rates() {
amount: UsageAmount::Tokens {
input: 1_000_000,
output: 1_000_000,
+ cache_read: 0,
+ cache_write: 0,
},
model: "m".to_string(),
price: None,
@@ -43,6 +49,8 @@ fn price_compute_uses_per_million_rates() {
ModelPricing::Tokens {
input: 5.0,
output: 25.0,
+ cache_read: None,
+ cache_write: None,
},
usage.amount,
)
@@ -50,6 +58,118 @@ fn price_compute_uses_per_million_rates() {
assert!((price.total - 30.0).abs() < 1e-3);
}
+#[test]
+fn price_compute_bills_each_cache_dimension_at_its_own_rate() {
+ let price = Price::compute(
+ ModelPricing::Tokens {
+ input: 5.0,
+ output: 25.0,
+ cache_read: Some(0.5),
+ cache_write: Some(6.25),
+ },
+ UsageAmount::Tokens {
+ input: 2_000,
+ output: 1_000,
+ cache_read: 140_000,
+ cache_write: 6_000,
+ },
+ )
+ .unwrap();
+
+ // 2k * $5 + 1k * $25 + 140k * $0.50 + 6k * $6.25, per million.
+ assert!((price.total - 0.1425).abs() < 1e-6);
+ assert_eq!(
+ price.pricing,
+ ModelPricing::Tokens {
+ input: 5.0,
+ output: 25.0,
+ cache_read: Some(0.5),
+ cache_write: Some(6.25),
+ }
+ );
+}
+
+#[test]
+fn price_compute_needs_no_cache_rate_when_no_cache_tokens_were_used() {
+ let price = Price::compute(
+ ModelPricing::Tokens {
+ input: 30.0,
+ output: 180.0,
+ cache_read: None,
+ cache_write: None,
+ },
+ UsageAmount::Tokens {
+ input: 1_000_000,
+ output: 0,
+ cache_read: 0,
+ cache_write: 0,
+ },
+ )
+ .unwrap();
+
+ assert!((price.total - 30.0).abs() < 1e-4);
+}
+
+#[test]
+fn price_compute_leaves_cache_reads_unpriced_without_a_cache_read_rate() {
+ let price = Price::compute(
+ ModelPricing::Tokens {
+ input: 1.32,
+ output: 3.96,
+ cache_read: None,
+ cache_write: Some(1.65),
+ },
+ UsageAmount::Tokens {
+ input: 1_000,
+ output: 100,
+ cache_read: 5_000,
+ cache_write: 0,
+ },
+ );
+
+ assert_eq!(price, None);
+}
+
+#[test]
+fn price_compute_leaves_cache_writes_unpriced_without_a_cache_write_rate() {
+ let price = Price::compute(
+ ModelPricing::Tokens {
+ input: 5.0,
+ output: 30.0,
+ cache_read: Some(0.5),
+ cache_write: None,
+ },
+ UsageAmount::Tokens {
+ input: 1_000,
+ output: 100,
+ cache_read: 0,
+ cache_write: 5_000,
+ },
+ );
+
+ assert_eq!(price, None);
+}
+
+#[test]
+fn negative_or_non_finite_cache_rates_are_invalid() {
+ for (cache_read, cache_write) in [
+ (Some(-0.1), None),
+ (None, Some(f32::NAN)),
+ (Some(f32::INFINITY), Some(1.0)),
+ ] {
+ assert!(matches!(
+ ModelPricing::Tokens {
+ input: 1.0,
+ output: 2.0,
+ cache_read,
+ cache_write,
+ }
+ .validate(),
+ Err(UsageError::InvalidPricing)
+ ));
+ }
+}
+
#[test]
fn summarize_groups_by_feature_and_totals() {
let rows = vec![
@@ -178,6 +298,8 @@ async fn missing_audio_rate_keeps_usage_unpriced() {
Some(ModelPricing::Tokens {
input: 0.0,
output: 0.0,
+ cache_read: None,
+ cache_write: None,
}),
] {
let repo = FakeRepo::default();
@@ -219,6 +341,8 @@ async fn both_recording_paths_persist_the_same_counting_policy() {
Some(ModelPricing::Tokens {
input: 5.0,
output: 25.0,
+ cache_read: None,
+ cache_write: None,
}),
] {
let repo = FakeRepo::default();
@@ -226,8 +350,12 @@ async fn both_recording_paths_persist_the_same_counting_policy() {
let service = UsageServiceImpl::new(repo.clone()).with_enforcement(enforcement);
let event = UsageContext::new(feature, actor.clone()).into_event(
"anthropic/test-model".into(),
- 100,
- 200,
+ UsageAmount::Tokens {
+ input: 100,
+ output: 200,
+ cache_read: 0,
+ cache_write: 0,
+ },
);
let expected = enforcement == AiUsageEnforcement::Enabled
&& actor == real_user
@@ -262,7 +390,15 @@ async fn default_disabled_writes_do_not_change_previously_counted_usage() {
let enabled = UsageServiceImpl::new(repo.clone()).with_enforcement(AiUsageEnforcement::Enabled);
let disabled = UsageServiceImpl::new(repo.clone());
let actor = MacroUserIdStr::try_from("macro|[email protected]".to_owned()).unwrap();
- let event = UsageContext::new(AiFeature::Chat, actor).into_event("model".into(), 100, 200);
+ let event = UsageContext::new(AiFeature::Chat, actor).into_event(
+ "model".into(),
+ UsageAmount::Tokens {
+ input: 100,
+ output: 200,
+ cache_read: 0,
+ cache_write: 0,
+ },
+ );
enabled.record_now(event.clone()).await.unwrap();
disabled.record_now(event).await.unwrap();
assert_eq!(*repo.counting.lock().unwrap(), vec![true, false]);
@@ -295,7 +431,9 @@ async fn admin_policy_protects_usage_and_price_changes() {
"whisper-1".into(),
ModelPricing::Tokens {
input: 0.0,
- output: 0.0
+ output: 0.0,
+ cache_read: None,
+ cache_write: None,
}
)
.await,
@@ -309,6 +447,8 @@ async fn admin_policy_protects_usage_and_price_changes() {
ModelPricing::Tokens {
input: 0.0,
output: 0.0,
+ cache_read: None,
+ cache_write: None,
},
)
.await
@@ -336,3 +476,80 @@ async fn rejects_invalid_prices_before_writing() {
}
assert!(repo.pricing.lock().unwrap().is_none());
}
+
+#[tokio::test]
+async fn records_cache_tokens_priced_at_the_model_cache_rates() {
+ let repo = FakeRepo::default();
+ *repo.pricing.lock().unwrap() = Some(ModelPricing::Tokens {
+ input: 5.0,
+ output: 25.0,
+ cache_read: Some(0.5),
+ cache_write: Some(6.25),
+ });
+ let event = UsageContext::new(AiFeature::AgentSession, user()).into_event(
+ "anthropic/claude-opus-5".into(),
+ UsageAmount::Tokens {
+ input: 1_000_000,
+ output: 0,
+ cache_read: 1_000_000,
+ cache_write: 1_000_000,
+ },
+ );
+
+ UsageServiceImpl::new(repo.clone())
+ .record_now(event)
+ .await
+ .unwrap();
+
+ let rows = repo.rows.lock().unwrap();
+ assert_eq!(rows.len(), 1);
+ assert_eq!(rows[0].cost.model, "claude-opus-5");
+ assert_eq!(
+ rows[0].cost.amount,
+ UsageAmount::Tokens {
+ input: 1_000_000,
+ output: 0,
+ cache_read: 1_000_000,
+ cache_write: 1_000_000,
+ }
+ );
+ assert!((rows[0].cost.price.unwrap().total - 11.75).abs() < 1e-4);
+}
+
+#[tokio::test]
+async fn cache_tokens_without_a_cache_rate_record_an_unpriced_row() {
+ let repo = FakeRepo::default();
+ *repo.pricing.lock().unwrap() = Some(ModelPricing::Tokens {
+ input: 0.35,
+ output: 1.5,
+ cache_read: None,
+ cache_write: None,
+ });
+ let event = UsageContext::new(AiFeature::AgentSession, user()).into_event(
+ "fireworks/muse-glimmer-30b".into(),
+ UsageAmount::Tokens {
+ input: 1_000,
+ output: 100,
+ cache_read: 9_000,
+ cache_write: 0,
+ },
+ );
+
+ UsageServiceImpl::new(repo.clone())
+ .record_now(event)
+ .await
+ .unwrap();
+
+ let rows = repo.rows.lock().unwrap();
+ assert_eq!(rows.len(), 1);
+ assert_eq!(
+ rows[0].cost.amount,
+ UsageAmount::Tokens {
+ input: 1_000,
+ output: 100,
+ cache_read: 9_000,
+ cache_write: 0,
+ }
+ );
+ assert!(rows[0].cost.price.is_none());
+}
diff --git a/crates/ai_usage/src/inbound/axum_router.rs b/crates/ai_usage/src/inbound/axum_router.rs
index e258ece809..839234294c 100644
--- a/crates/ai_usage/src/inbound/axum_router.rs
+++ b/crates/ai_usage/src/inbound/axum_router.rs
@@ -45,12 +45,20 @@ pub struct SetPricingRequest {
pub price_per_mil_in: Option<f32>,
/// New price per million output tokens (USD). Required for token pricing.
pub price_per_mil_out: Option<f32>,
+ /// Price per million cache-read input tokens (USD). Omit it when the model
+ /// has no published rate: calls that report cache reads then stay unpriced.
+ pub price_per_mil_cache_read: Option<f32>,
+ /// Price per million cache-write input tokens (USD). Omit it when the model
+ /// has no published rate: calls that report cache writes then stay unpriced.
+ pub price_per_mil_cache_write: Option<f32>,
/// Price per minute of audio (USD), or null for token-only pricing.
pub price_per_audio_minute: Option<f32>,
}
impl SetPricingRequest {
fn pricing(&self) -> Result<ModelPricing, &'static str> {
+ let cache_read = self.price_per_mil_cache_read;
+ let cache_write = self.price_per_mil_cache_write;
match (
self.price_per_mil_in,
self.price_per_mil_out,
@@ -58,11 +66,18 @@ impl SetPricingRequest {
) {
(input, output, Some(per_minute))
if input.is_none_or(|price| price == 0.0)
- && output.is_none_or(|price| price == 0.0) =>
+ && output.is_none_or(|price| price == 0.0)
+ && cache_read.is_none()
+ && cache_write.is_none() =>
{
Ok(ModelPricing::Audio { per_minute })
}
- (Some(input), Some(output), None) => Ok(ModelPricing::Tokens { input, output }),
+ (Some(input), Some(output), None) => Ok(ModelPricing::Tokens {
+ input,
+ output,
+ cache_read,
+ cache_write,
+ }),
(_, _, Some(_)) => Err("choose token pricing or audio pricing"),
_ => Err("provide both token prices or an audio price"),
}
diff --git a/crates/ai_usage/src/inbound/axum_router/test.rs b/crates/ai_usage/src/inbound/axum_router/test.rs
index 4612874144..c828c10cb9 100644
--- a/crates/ai_usage/src/inbound/axum_router/test.rs
+++ b/crates/ai_usage/src/inbound/axum_router/test.rs
@@ -31,7 +31,12 @@ fn token_prices_remain_explicit_including_free_models() {
"price_per_mil_in": input,
"price_per_mil_out": output,
})),
- Ok(ModelPricing::Tokens { input, output })
+ Ok(ModelPricing::Tokens {
+ input,
+ output,
+ cache_read: None,
+ cache_write: None
+ })
);
}
}
@@ -65,3 +70,48 @@ fn mixed_billing_units_are_rejected() {
);
}
}
+
+#[test]
+fn cache_rates_are_optional_and_kept_when_given() {
+ assert_eq!(
+ pricing(json!({
+ "model": "claude-opus-5",
+ "price_per_mil_in": 5.0,
+ "price_per_mil_out": 25.0,
+ "price_per_mil_cache_read": 0.5,
+ "price_per_mil_cache_write": 6.25,
+ })),
+ Ok(ModelPricing::Tokens {
+ input: 5.0,
+ output: 25.0,
+ cache_read: Some(0.5),
+ cache_write: Some(6.25),
+ })
+ );
+ assert_eq!(
+ pricing(json!({
+ "model": "gpt-5.5",
+ "price_per_mil_in": 5.0,
+ "price_per_mil_out": 30.0,
+ "price_per_mil_cache_read": 0.5,
+ })),
+ Ok(ModelPricing::Tokens {
+ input: 5.0,
+ output: 30.0,
+ cache_read: Some(0.5),
+ cache_write: None,
+ })
+ );
+}
+
+#[test]
+fn cache_rates_cannot_accompany_audio_pricing() {
+ assert_eq!(
+ pricing(json!({
+ "model": "whisper-1",
+ "price_per_audio_minute": 0.006,
+ "price_per_mil_cache_read": 0.5,
+ })),
+ Err("choose token pricing or audio pricing")
+ );
+}
diff --git a/crates/ai_usage/src/inbound/models.rs b/crates/ai_usage/src/inbound/models.rs
index d2ab36dbe9..b9b510bdf5 100644
--- a/crates/ai_usage/src/inbound/models.rs
+++ b/crates/ai_usage/src/inbound/models.rs
@@ -15,6 +15,12 @@ pub struct Price {
pub price_per_million_in: f32,
/// Price per million output tokens (USD); zero for audio billing.
pub price_per_million_out: f32,
+ /// Price per million cache-read input tokens (USD), absent when the model
+ /// has no published rate and for audio billing.
+ pub price_per_million_cache_read: Option<f32>,
+ /// Price per million cache-write input tokens (USD), absent when the model
+ /// has no published rate and for audio billing.
+ pub price_per_million_cache_write: Option<f32>,
/// Price per audio minute (USD), absent for token billing.
pub price_per_audio_minute: Option<f32>,
/// Total cost (USD).
@@ -23,13 +29,20 @@ pub struct Price {
impl From<domain::Price> for Price {
fn from(price: domain::Price) -> Self {
- let (input, output, audio) = match price.pricing {
- ModelPricing::Tokens { input, output } => (input, output, None),
- ModelPricing::Audio { per_minute } => (0.0, 0.0, Some(per_minute)),
+ let (input, output, cache_read, cache_write, audio) = match price.pricing {
+ ModelPricing::Tokens {
+ input,
+ output,
+ cache_read,
+ cache_write,
+ } => (input, output, cache_read, cache_write, None),
+ ModelPricing::Audio { per_minute } => (0.0, 0.0, None, None, Some(per_minute)),
};
Self {
price_per_million_in: input,
price_per_million_out: output,
+ price_per_million_cache_read: cache_read,
+ price_per_million_cache_write: cache_write,
price_per_audio_minute: audio,
total: price.total,
}
@@ -39,10 +52,14 @@ impl From<domain::Price> for Price {
/// Measured usage for one invocation.
#[derive(Serialize, ToSchema)]
pub struct Usage {
- /// Input tokens; zero for audio billing.
+ /// Uncached input tokens; zero for audio billing.
pub input_tokens: u64,
/// Output tokens; zero for audio billing.
pub output_tokens: u64,
+ /// Input tokens read from a prompt cache; zero for audio billing.
+ pub cache_read_input_tokens: u64,
+ /// Input tokens written to a prompt cache; zero for audio billing.
+ pub cache_write_input_tokens: u64,
/// Audio duration in seconds, absent for token billing.
pub audio_seconds: Option<f64>,
/// Provider model identifier.
@@ -55,14 +72,22 @@ pub struct Usage {
impl From<domain::Usage> for Usage {
fn from(usage: domain::Usage) -> Self {
- let (input_tokens, output_tokens, audio_seconds) = match usage.amount {
- UsageAmount::Tokens { input, output } => (input, output, None),
- UsageAmount::Audio { duration } => (0, 0, Some(duration.as_secs_f64())),
- };
+ let (input_tokens, output_tokens, cache_read_input_tokens, cache_write_input_tokens, audio) =
+ match usage.amount {
+ UsageAmount::Tokens {
+ input,
+ output,
+ cache_read,
+ cache_write,
+ } => (input, output, cache_read, cache_write, None),
+ UsageAmount::Audio { duration } => (0, 0, 0, 0, Some(duration.as_secs_f64())),
+ };
Self {
input_tokens,
output_tokens,
- audio_seconds,
+ cache_read_input_tokens,
+ cache_write_input_tokens,
+ audio_seconds: audio,
model: usage.model,
price: usage.price.map(Into::into),
created_at: usage.created_at,
diff --git a/crates/ai_usage/src/inbound/models/test.rs b/crates/ai_usage/src/inbound/models/test.rs
index 7b96c2bc76..ee6d8fb940 100644
--- a/crates/ai_usage/src/inbound/models/test.rs
+++ b/crates/ai_usage/src/inbound/models/test.rs
@@ -8,6 +8,8 @@ fn token_usage_keeps_the_existing_http_shape() {
let amount = UsageAmount::Tokens {
input: 1_000_000,
output: 2_000_000,
+ cache_read: 0,
+ cache_write: 0,
};
let usage = Usage::from(domain::Usage {
amount,
@@ -16,6 +18,8 @@ fn token_usage_keeps_the_existing_http_shape() {
ModelPricing::Tokens {
input: 1.0,
output: 2.0,
+ cache_read: None,
+ cache_write: None,
},
amount,
),
@@ -26,12 +30,16 @@ fn token_usage_keeps_the_existing_http_shape() {
json!({
"input_tokens": 1_000_000,
"output_tokens": 2_000_000,
+ "cache_read_input_tokens": 0,
+ "cache_write_input_tokens": 0,
"audio_seconds": null,
"model": "text-model",
"created_at": "2026-09-18T00:00:00Z",
"price": {
"price_per_million_in": 1.0,
"price_per_million_out": 2.0,
+ "price_per_million_cache_read": null,
+ "price_per_million_cache_write": null,
"price_per_audio_minute": null,
"total": 5.0
}
@@ -39,6 +47,51 @@ fn token_usage_keeps_the_existing_http_shape() {
);
}
+#[test]
+fn cached_token_usage_exposes_cache_tokens_and_rates() {
+ let created_at = "2026-10-05T00:00:00Z".parse().unwrap();
+ let amount = UsageAmount::Tokens {
+ input: 1_000_000,
+ output: 1_000_000,
+ cache_read: 1_000_000,
+ cache_write: 1_000_000,
+ };
+ let usage = Usage::from(domain::Usage {
+ amount,
+ model: "claude-opus-5".into(),
+ price: domain::Price::compute(
+ ModelPricing::Tokens {
+ input: 5.0,
+ output: 25.0,
+ cache_read: Some(0.5),
+ cache_write: Some(6.25),
+ },
+ amount,
+ ),
+ created_at,
+ });
+ assert_eq!(
+ serde_json::to_value(usage).unwrap(),
+ json!({
+ "input_tokens": 1_000_000,
+ "output_tokens": 1_000_000,
+ "cache_read_input_tokens": 1_000_000,
+ "cache_write_input_tokens": 1_000_000,
+ "audio_seconds": null,
+ "model": "claude-opus-5",
+ "created_at": "2026-10-05T00:00:00Z",
+ "price": {
+ "price_per_million_in": 5.0,
+ "price_per_million_out": 25.0,
+ "price_per_million_cache_read": 0.5,
+ "price_per_million_cache_write": 6.25,
+ "price_per_audio_minute": null,
+ "total": 36.75
+ }
+ })
+ );
+}
+
#[test]
fn audio_usage_exposes_duration_and_rate_without_inventing_tokens() {
let amount = UsageAmount::Audio {
diff --git a/crates/ai_usage/src/outbound/pg_financial_usage_repo/test.rs b/crates/ai_usage/src/outbound/pg_financial_usage_repo/test.rs
index 0d9a0b832f..9946a861c7 100644
--- a/crates/ai_usage/src/outbound/pg_financial_usage_repo/test.rs
+++ b/crates/ai_usage/src/outbound/pg_financial_usage_repo/test.rs
@@ -370,10 +370,14 @@ async fn legacy_analytics_writes_and_repricing_leave_financial_evidence_unchange
let pricing = ModelPricing::Tokens {
input: 1.0,
output: 2.0,
+ cache_read: None,
+ cache_write: None,
};
let amount = UsageAmount::Tokens {
input: 1_000_000,
output: 0,
+ cache_read: 0,
+ cache_write: 0,
};
analytics
.insert_usage(
@@ -398,6 +402,8 @@ async fn legacy_analytics_writes_and_repricing_leave_financial_evidence_unchange
ModelPricing::Tokens {
input: 99.0,
output: 99.0,
+ cache_read: None,
+ cache_write: None,
},
)
.await
diff --git a/crates/ai_usage/src/outbound/pg_usage_repo.rs b/crates/ai_usage/src/outbound/pg_usage_repo.rs
index fd4c3bfee7..eec5187660 100644
--- a/crates/ai_usage/src/outbound/pg_usage_repo.rs
+++ b/crates/ai_usage/src/outbound/pg_usage_repo.rs
@@ -26,35 +26,67 @@ impl PgUsageRepo {
impl UsageRepo for PgUsageRepo {
async fn insert_usage(&self, usage: &CompletionUsage, count_usage: bool) -> Result<()> {
let id = macro_uuid::generate_uuid_v7();
- let (input_tokens, output_tokens, audio_seconds) = match usage.cost.amount {
- UsageAmount::Tokens { input, output } => (
- i64::try_from(input).unwrap_or(i64::MAX),
- i64::try_from(output).unwrap_or(i64::MAX),
- None,
- ),
- UsageAmount::Audio { duration } => (0, 0, Some(duration.as_secs_f64())),
- };
- let (per_in, per_out, per_audio_minute, total) = match usage.cost.price {
- Some(Price {
- pricing: ModelPricing::Tokens { input, output },
- total,
- }) => (Some(input), Some(output), None, Some(total)),
- Some(Price {
- pricing: ModelPricing::Audio { per_minute },
- total,
- }) => (Some(0.0), Some(0.0), Some(per_minute), Some(total)),
- None => (None, None, None, None),
- };
+ let (input_tokens, output_tokens, cache_read_tokens, cache_write_tokens, audio_seconds) =
+ match usage.cost.amount {
+ UsageAmount::Tokens {
+ input,
+ output,
+ cache_read,
+ cache_write,
+ } => (
+ token_column(input),
+ token_column(output),
+ token_column(cache_read),
+ token_column(cache_write),
+ None,
+ ),
+ UsageAmount::Audio { duration } => (0, 0, 0, 0, Some(duration.as_secs_f64())),
+ };
+ let (per_in, per_out, per_cache_read, per_cache_write, per_audio_minute, total) =
+ match usage.cost.price {
+ Some(Price {
+ pricing:
+ ModelPricing::Tokens {
+ input,
+ output,
+ cache_read,
+ cache_write,
+ },
+ total,
+ }) => (
+ Some(input),
+ Some(output),
+ cache_read,
+ cache_write,
+ None,
+ Some(total),
+ ),
+ Some(Price {
+ pricing: ModelPricing::Audio { per_minute },
+ total,
+ }) => (
+ Some(0.0),
+ Some(0.0),
+ None,
+ None,
+ Some(per_minute),
+ Some(total),
+ ),
+ None => (None, None, None, None, None, None),
+ };
sqlx::query!(
r#"
INSERT INTO ai_usage (
id, feature, user_id, entity, model,
- input_tokens, output_tokens, audio_seconds,
- price_per_million_in, price_per_million_out, price_per_audio_minute, total,
+ input_tokens, output_tokens,
+ cache_read_input_tokens, cache_write_input_tokens, audio_seconds,
+ price_per_million_in, price_per_million_out,
+ price_per_million_cache_read, price_per_million_cache_write,
+ price_per_audio_minute, total,
count_usage
)
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17)
"#,
id,
usage.feature.to_string(),
@@ -63,9 +95,13 @@ impl UsageRepo for PgUsageRepo {
usage.cost.model,
input_tokens,
output_tokens,
+ cache_read_tokens,
+ cache_write_tokens,
audio_seconds,
per_in,
per_out,
+ per_cache_read,
+ per_cache_write,
per_audio_minute,
total,
count_usage,
@@ -79,7 +115,12 @@ impl UsageRepo for PgUsageRepo {
async fn get_pricing(&self, model: &str) -> Result<Option<ModelPricing>> {
let row = sqlx::query!(
r#"
- SELECT price_per_million_in, price_per_million_out, price_per_audio_minute
+ SELECT
+ price_per_million_in,
+ price_per_million_out,
+ price_per_million_cache_read,
+ price_per_million_cache_write,
+ price_per_audio_minute
FROM ai_pricing
WHERE model = $1
"#,
@@ -93,30 +134,45 @@ impl UsageRepo for PgUsageRepo {
None => ModelPricing::Tokens {
input: r.price_per_million_in,
output: r.price_per_million_out,
+ cache_read: r.price_per_million_cache_read,
+ cache_write: r.price_per_million_cache_write,
},
}))
}
async fn set_pricing(&self, model: &str, pricing: ModelPricing) -> Result<()> {
- let (per_in, per_out, per_audio_minute) = match pricing {
- ModelPricing::Tokens { input, output } => (input, output, None),
- ModelPricing::Audio { per_minute } => (0.0, 0.0, Some(per_minute)),
+ let (per_in, per_out, per_cache_read, per_cache_write, per_audio_minute) = match pricing {
+ ModelPricing::Tokens {
+ input,
+ output,
+ cache_read,
+ cache_write,
+ } => (input, output, cache_read, cache_write, None),
+ ModelPricing::Audio { per_minute } => (0.0, 0.0, None, None, Some(per_minute)),
};
let mut tx = self.inner.begin().await?;
sqlx::query!(
r#"
- INSERT INTO ai_pricing (model, price_per_million_in, price_per_million_out, price_per_audio_minute)
- VALUES ($1, $2, $3, $4)
+ INSERT INTO ai_pricing (
+ model, price_per_million_in, price_per_million_out,
+ price_per_million_cache_read, price_per_million_cache_write,
+ price_per_audio_minute
+ )
+ VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (model) DO UPDATE
SET price_per_million_in = EXCLUDED.price_per_million_in,
price_per_million_out = EXCLUDED.price_per_million_out,
+ price_per_million_cache_read = EXCLUDED.price_per_million_cache_read,
+ price_per_million_cache_write = EXCLUDED.price_per_million_cache_write,
price_per_audio_minute = EXCLUDED.price_per_audio_minute,
updated_at = NOW()
"#,
model,
per_in,
per_out,
+ per_cache_read,
+ per_cache_write,
per_audio_minute,
)
.execute(&mut *tx)
@@ -128,16 +184,26 @@ impl UsageRepo for PgUsageRepo {
UPDATE ai_usage
SET price_per_million_in = $2::real,
price_per_million_out = $3::real,
- price_per_audio_minute = $4::real,
- total = CASE WHEN (audio_seconds IS NULL) <> ($4::real IS NULL) THEN NULL
- ELSE (input_tokens::real / 1000000.0::real) * $2::real
+ price_per_million_cache_read = $4::real,
+ price_per_million_cache_write = $5::real,
+ price_per_audio_minute = $6::real,
+ total = CASE
+ WHEN (audio_seconds IS NULL) <> ($6::real IS NULL) THEN NULL
+ WHEN cache_read_input_tokens > 0 AND $4::real IS NULL THEN NULL
+ WHEN cache_write_input_tokens > 0 AND $5::real IS NULL THEN NULL
+ ELSE (input_tokens::real / 1000000.0::real) * $2::real
+ (output_tokens::real / 1000000.0::real) * $3::real
- + (COALESCE(audio_seconds, 0) / 60.0 * COALESCE($4::real, 0))::real END
+ + (cache_read_input_tokens::real / 1000000.0::real) * COALESCE($4::real, 0)
+ + (cache_write_input_tokens::real / 1000000.0::real) * COALESCE($5::real, 0)
+ + (COALESCE(audio_seconds, 0) / 60.0 * COALESCE($6::real, 0))::real
+ END
WHERE model = $1
"#,
model,
per_in,
per_out,
+ per_cache_read,
+ per_cache_write,
per_audio_minute,
)
.execute(&mut *tx)
@@ -164,9 +230,13 @@ impl UsageRepo for PgUsageRepo {
model,
input_tokens,
output_tokens,
+ cache_read_input_tokens,
+ cache_write_input_tokens,
audio_seconds,
price_per_million_in,
price_per_million_out,
+ price_per_million_cache_read,
+ price_per_million_cache_write,
price_per_audio_minute,
total,
created_at
@@ -201,6 +271,8 @@ impl UsageRepo for PgUsageRepo {
None => ModelPricing::Tokens {
input: per_in,
output: per_out,
+ cache_read: r.price_per_million_cache_read,
+ cache_write: r.price_per_million_cache_write,
},
},
total,
@@ -221,6 +293,8 @@ impl UsageRepo for PgUsageRepo {
None => UsageAmount::Tokens {
input: r.input_tokens.max(0) as u64,
output: r.output_tokens.max(0) as u64,
+ cache_read: r.cache_read_input_tokens.max(0) as u64,
+ cache_write: r.cache_write_input_tokens.max(0) as u64,
},
},
model: r.model,
@@ -233,6 +307,11 @@ impl UsageRepo for PgUsageRepo {
}
}
+/// Token counts are `BIGINT`; a count past `i64::MAX` saturates.
+fn token_column(tokens: u64) -> i64 {
+ i64::try_from(tokens).unwrap_or(i64::MAX)
+}
+
// The domain error stays sqlx-free; this adapter owns the mapping so `?`
// works on sqlx results throughout the crate's outbound code. The raw sqlx
// error travels inside the report.
diff --git a/crates/ai_usage/src/outbound/pg_usage_repo/test.rs b/crates/ai_usage/src/outbound/pg_usage_repo/test.rs
index 02902a7f61..e42ffcb1a8 100644
--- a/crates/ai_usage/src/outbound/pg_usage_repo/test.rs
+++ b/crates/ai_usage/src/outbound/pg_usage_repo/test.rs
@@ -10,7 +10,12 @@ fn completion(feature: AiFeature, model: &str, input: u64, output: u64) -> Compl
user: SYSTEM_USER_ID.clone(),
entity: None,
cost: Usage {
- amount: UsageAmount::Tokens { input, output },
+ amount: UsageAmount::Tokens {
+ input,
+ output,
+ cache_read: 0,
+ cache_write: 0,
+ },
model: model.to_string(),
price: None,
created_at: Utc::now(),
@@ -26,7 +31,9 @@ async fn seeded_pricing_is_available(pool: PgPool) {
price,
Some(ModelPricing::Tokens {
input: 5.0,
- output: 25.0
+ output: 25.0,
+ cache_read: Some(0.5),
+ cache_write: Some(6.25),
})
);
let price = repo.get_pricing("claude-opus-5").await.unwrap();
@@ -34,7 +41,9 @@ async fn seeded_pricing_is_available(pool: PgPool) {
price,
Some(ModelPricing::Tokens {
input: 5.0,
- output: 25.0
+ output: 25.0,
+ cache_read: Some(0.5),
+ cache_write: Some(6.25),
})
);
assert_eq!(
@@ -50,7 +59,9 @@ async fn seeded_pricing_is_available(pool: PgPool) {
price,
ModelPricing::Tokens {
input: 0.30,
- output: 30.0
+ output: 30.0,
+ cache_read: None,
+ cache_write: None,
}
);
let cost = crate::Price::compute(
@@ -58,6 +69,8 @@ async fn seeded_pricing_is_available(pool: PgPool) {
UsageAmount::Tokens {
input: 100,
output: 1290,
+ cache_read: 0,
+ cache_write: 0,
},
)
.unwrap();
@@ -118,6 +131,8 @@ async fn set_pricing_recomputes_existing_rows(pool: PgPool) {
ModelPricing::Tokens {
input: 2.0,
output: 8.0,
+ cache_read: None,
+ cache_write: None,
},
)
.await
@@ -130,7 +145,9 @@ async fn set_pricing_recomputes_existing_rows(pool: PgPool) {
repo.get_pricing("brand-new-model").await.unwrap(),
Some(ModelPricing::Tokens {
input: 2.0,
- output: 8.0
+ output: 8.0,
+ cache_read: None,
+ cache_write: None,
})
);
}
@@ -165,11 +182,15 @@ async fn counting_decisions_survive_repricing_and_remain_visible_to_analytics(po
ModelPricing::Tokens {
input: 2.0,
output: 8.0,
+ cache_read: None,
+ cache_write: None,
},
ModelPricing::Audio { per_minute: 0.006 },
ModelPricing::Tokens {
input: 4.0,
output: 16.0,
+ cache_read: None,
+ cache_write: None,
},
] {
repo.set_pricing(model, pricing).await.unwrap();
@@ -222,21 +243,27 @@ async fn pricing_resolves_provider_qualified_ids(pool: PgPool) {
repo.get_pricing("anthropic/claude-opus-5").await.unwrap(),
Some(ModelPricing::Tokens {
input: 5.0,
- output: 25.0
+ output: 25.0,
+ cache_read: Some(0.5),
+ cache_write: Some(6.25),
})
);
assert_eq!(
repo.get_pricing("claude-sonnet-5").await.unwrap(),
Some(ModelPricing::Tokens {
input: 2.0,
- output: 10.0
+ output: 10.0,
+ cache_read: Some(0.2),
+ cache_write: Some(2.5),
})
);
assert_eq!(
repo.get_pricing("openai/gpt-6-astra").await.unwrap(),
Some(ModelPricing::Tokens {
input: 10.0,
- output: 50.0
+ output: 50.0,
+ cache_read: Some(1.0),
+ cache_write: Some(12.5),
})
);
}
@@ -278,6 +305,8 @@ async fn duration_usage_roundtrips_and_reprices_without_affecting_tokens(pool: P
None => ModelPricing::Tokens {
input: 0.0,
output: 0.0,
+ cache_read: None,
+ cache_write: None,
},
},
)
@@ -305,7 +334,9 @@ async fn duration_usage_roundtrips_and_reprices_without_affecting_tokens(pool: P
chat[0].cost.amount,
UsageAmount::Tokens {
input: 1_000_000,
- output: 0
+ output: 0,
+ cache_read: 0,
+ cache_write: 0,
}
);
assert!(
@@ -333,6 +364,8 @@ async fn repricing_cannot_apply_audio_rates_to_token_usage(pool: PgPool) {
ModelPricing::Tokens {
input: 2.0,
output: 8.0,
+ cache_read: None,
+ cache_write: None,
},
)
.await
@@ -340,3 +373,119 @@ async fn repricing_cannot_apply_audio_rates_to_token_usage(pool: PgPool) {
let rows = repo.query_usage(&UsageApiParams::default()).await.unwrap();
assert_eq!(rows[0].cost.price.unwrap().total, 2.0);
}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn cached_token_usage_roundtrips_with_its_cache_rates(pool: PgPool) {
+ let repo = PgUsageRepo::new(pool);
+ let amount = UsageAmount::Tokens {
+ input: 2_000,
+ output: 1_000,
+ cache_read: 140_000,
+ cache_write: 6_000,
+ };
+ let row = CompletionUsage {
+ feature: AiFeature::AgentSession,
+ user: SYSTEM_USER_ID.clone(),
+ entity: None,
+ cost: Usage {
+ amount,
+ model: "claude-opus-5".to_string(),
+ price: Price::compute(
+ repo.get_pricing("claude-opus-5").await.unwrap().unwrap(),
+ amount,
+ ),
+ created_at: Utc::now(),
+ },
+ };
+ repo.insert_usage(&row, true).await.unwrap();
+
+ let rows = repo.query_usage(&UsageApiParams::default()).await.unwrap();
+ assert_eq!(rows.len(), 1);
+ assert_eq!(
+ rows[0].cost.amount,
+ UsageAmount::Tokens {
+ input: 2_000,
+ output: 1_000,
+ cache_read: 140_000,
+ cache_write: 6_000,
+ }
+ );
+ let price = rows[0].cost.price.unwrap();
+ assert_eq!(
+ price.pricing,
+ ModelPricing::Tokens {
+ input: 5.0,
+ output: 25.0,
+ cache_read: Some(0.5),
+ cache_write: Some(6.25),
+ }
+ );
+ assert!((price.total - 0.1425).abs() < 1e-6);
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn repricing_bills_cache_tokens_and_unprices_them_without_a_cache_rate(pool: PgPool) {
+ let repo = PgUsageRepo::new(pool);
+ repo.insert_usage(
+ &CompletionUsage {
+ feature: AiFeature::AgentSession,
+ user: SYSTEM_USER_ID.clone(),
+ entity: None,
+ cost: Usage {
+ amount: UsageAmount::Tokens {
+ input: 1_000_000,
+ output: 0,
+ cache_read: 1_000_000,
+ cache_write: 1_000_000,
+ },
+ model: "cache-repricing-model".to_string(),
+ price: None,
+ created_at: Utc::now(),
+ },
+ },
+ true,
+ )
+ .await
+ .unwrap();
+
+ repo.set_pricing(
+ "cache-repricing-model",
+ ModelPricing::Tokens {
+ input: 2.0,
+ output: 8.0,
+ cache_read: Some(0.2),
+ cache_write: Some(2.5),
+ },
+ )
+ .await
+ .unwrap();
+ let rows = repo.query_usage(&UsageApiParams::default()).await.unwrap();
+ let price = rows[0].cost.price.unwrap();
+ assert_eq!(
+ price.pricing,
+ ModelPricing::Tokens {
+ input: 2.0,
+ output: 8.0,
+ cache_read: Some(0.2),
+ cache_write: Some(2.5),
+ }
+ );
+ assert!((price.total - 4.7).abs() < 1e-4);
+
+ repo.set_pricing(
+ "cache-repricing-model",
+ ModelPricing::Tokens {
+ input: 2.0,
+ output: 8.0,
+ cache_read: Some(0.2),
+ cache_write: None,
+ },
+ )
+ .await
+ .unwrap();
+ let rows = repo.query_usage(&UsageApiParams::default()).await.unwrap();
+ assert!(
+ rows[0].cost.price.is_none(),
+ "cache writes without a write rate must stay unpriced"
+ );
+}
diff --git a/crates/documents/src/domain/ai_editing.rs b/crates/documents/src/domain/ai_editing.rs
index ac233c5133..eee4276f75 100644
--- a/crates/documents/src/domain/ai_editing.rs
+++ b/crates/documents/src/domain/ai_editing.rs
@@ -2,7 +2,7 @@
use super::ports::editing::{EditMode, EditResult, EditingWorkerService, EditorName};
use ai_billing::domain::admission::{AiAdmissionError, AiAdmissionService};
-use ai_usage::{AiFeature, UsageContext, UsageRecorder};
+use ai_usage::{AiFeature, UsageAmount, UsageContext, UsageRecorder};
use entity_access::domain::models::{EditAccessLevel, EntityAccessReceipt};
use macro_sync_service_jwt::DocumentPermissionToken;
use macro_user_id::user_id::MacroUserIdStr;
@@ -78,10 +78,16 @@ impl<W: EditingWorkerService> AiEditingService<W> {
let usage = UsageContext::new(AiFeature::AiEditing, caller.clone())
.with_entity(macro_uuid::string_to_uuid(document_id).ok());
for model in &result.usage {
+ // The worker reports one input total per model with no cache
+ // breakdown, so any cache tokens in it bill at the input rate.
self.recorder.record(usage.clone().into_event(
model.model.clone(),
- u64::from(model.input_tokens),
- u64::from(model.output_tokens),
+ UsageAmount::Tokens {
+ input: u64::from(model.input_tokens),
+ output: u64::from(model.output_tokens),
+ cache_read: 0,
+ cache_write: 0,
+ },
));
}
Ok(result)
diff --git a/crates/documents/src/domain/ai_editing/test.rs b/crates/documents/src/domain/ai_editing/test.rs
index 2e1b0a4257..50e9b1eeaf 100644
--- a/crates/documents/src/domain/ai_editing/test.rs
+++ b/crates/documents/src/domain/ai_editing/test.rs
@@ -133,7 +133,9 @@ async fn admitted_edit_keeps_worker_arguments_and_each_models_usage() {
events[0].amount,
UsageAmount::Tokens {
input: 12,
- output: 3
+ output: 3,
+ cache_read: 0,
+ cache_write: 0,
}
);
assert_eq!(events[1].model, "model-b");
diff --git a/crates/image_generation/src/domain/service/test.rs b/crates/image_generation/src/domain/service/test.rs
index 423976c4f3..2d13e0cc7a 100644
--- a/crates/image_generation/src/domain/service/test.rs
+++ b/crates/image_generation/src/domain/service/test.rs
@@ -73,11 +73,15 @@ impl ImageGenerator for FakeGenerator {
recorder: &dyn UsageRecorder,
) -> Result<GeneratedImage, ImageGenerationError> {
self.requests.lock().unwrap().push(request.clone());
- recorder.record(
- usage
- .clone()
- .into_event("gemini-2.5-flash-image".to_string(), 8, 1290),
- );
+ recorder.record(usage.clone().into_event(
+ "gemini-2.5-flash-image".to_string(),
+ ai_usage::UsageAmount::Tokens {
+ input: 8,
+ output: 1290,
+ cache_read: 0,
+ cache_write: 0,
+ },
+ ));
self.result
.clone()
.map_err(|reason| ImageGenerationError::Refused(reason.to_string()))
@@ -540,7 +544,9 @@ async fn attributes_usage_to_the_user_even_when_upload_fails() {
events[0].amount,
ai_usage::UsageAmount::Tokens {
input: 8,
- output: 1290
+ output: 1290,
+ cache_read: 0,
+ cache_write: 0,
}
);
}
diff --git a/crates/image_generation/src/outbound/gemini.rs b/crates/image_generation/src/outbound/gemini.rs
index 1d7ca17507..1862c0a870 100644
--- a/crates/image_generation/src/outbound/gemini.rs
+++ b/crates/image_generation/src/outbound/gemini.rs
@@ -3,7 +3,7 @@
use std::time::Duration;
-use ai_usage::{UsageContext, UsageRecorder};
+use ai_usage::{UsageAmount, UsageContext, UsageRecorder};
use anyhow::Context as _;
use base64::Engine as _;
use serde::Deserialize;
@@ -119,15 +119,21 @@ impl ImageGenerator for GeminiImageGenerator {
.context("unexpected Gemini response shape")
.map_err(ImageGenerationError::Provider)?;
if let Some(metadata) = &parsed.usage_metadata {
+ // The image model publishes no cached-input rate, so implicit
+ // cache reads stay inside the prompt count at the input rate.
recorder.record(usage.clone().into_event(
self.model.clone(),
- metadata.prompt_token_count,
- metadata.candidates_token_count.unwrap_or_else(|| {
- metadata
- .total_token_count
- .unwrap_or(metadata.prompt_token_count)
- .saturating_sub(metadata.prompt_token_count)
- }),
+ UsageAmount::Tokens {
+ input: metadata.prompt_token_count,
+ output: metadata.candidates_token_count.unwrap_or_else(|| {
+ metadata
+ .total_token_count
+ .unwrap_or(metadata.prompt_token_count)
+ .saturating_sub(metadata.prompt_token_count)
+ }),
+ cache_read: 0,
+ cache_write: 0,
+ },
));
} else {
tracing::warn!(model = %self.model, "Gemini image response omitted usage metadata");
diff --git a/crates/image_generation/src/outbound/gemini/test.rs b/crates/image_generation/src/outbound/gemini/test.rs
index fd46787a20..2c64e8c40d 100644
--- a/crates/image_generation/src/outbound/gemini/test.rs
+++ b/crates/image_generation/src/outbound/gemini/test.rs
@@ -362,7 +362,9 @@ async fn records_provider_counts_before_image_decoding_or_refusal() {
events[0].amount,
ai_usage::UsageAmount::Tokens {
input: 8,
- output: 1290
+ output: 1290,
+ cache_read: 0,
+ cache_write: 0,
}
);
}
@@ -395,7 +397,9 @@ async fn blocked_prompt_records_reported_input_without_inventing_output() {
events[0].amount,
ai_usage::UsageAmount::Tokens {
input: 12,
- output: 0
+ output: 0,
+ cache_read: 0,
+ cache_write: 0,
}
);
}
@@ -461,7 +465,9 @@ async fn uses_total_minus_prompt_when_candidate_count_is_absent() {
events[0].amount,
ai_usage::UsageAmount::Tokens {
input: 8,
- output: 1290
+ output: 1290,
+ cache_read: 0,
+ cache_write: 0,
}
);
}
diff --git a/crates/macro_db_client/migrations/20261005221204_ai_prompt_cache_pricing.sql b/crates/macro_db_client/migrations/20261005221204_ai_prompt_cache_pricing.sql
new file mode 100644
index 0000000000..f53eeec270
--- /dev/null
+++ b/crates/macro_db_client/migrations/20261005221204_ai_prompt_cache_pricing.sql
@@ -0,0 +1,113 @@
+-- Price prompt-cache reads and writes.
+--
+-- ai_usage.input_tokens now holds only UNCACHED input. Cache reads and cache
+-- writes are their own dimensions with their own rates; the recorder
+-- normalizes each provider so no token lands in two columns.
+--
+-- A NULL cache rate means "no published rate": a call that reports tokens in
+-- that dimension stays unpriced (total NULL), exactly like a model with no
+-- ai_pricing row. It is never priced at zero or at the input rate.
+--
+-- Deployed writers omit every new column: their rows keep zero cache tokens
+-- and NULL cache rates, which is what they measured.
+ALTER TABLE ai_pricing
+ ADD COLUMN price_per_million_cache_read REAL
+ CHECK (price_per_million_cache_read >= 0 AND price_per_million_cache_read < 'Infinity'::real),
+ ADD COLUMN price_per_million_cache_write REAL
+ CHECK (price_per_million_cache_write >= 0 AND price_per_million_cache_write < 'Infinity'::real);
+
+ALTER TABLE ai_usage
+ ADD COLUMN cache_read_input_tokens BIGINT NOT NULL DEFAULT 0,
+ ADD COLUMN cache_write_input_tokens BIGINT NOT NULL DEFAULT 0,
+ ADD COLUMN price_per_million_cache_read REAL
+ CHECK (price_per_million_cache_read >= 0 AND price_per_million_cache_read < 'Infinity'::real),
+ ADD COLUMN price_per_million_cache_write REAL
+ CHECK (price_per_million_cache_write >= 0 AND price_per_million_cache_write < 'Infinity'::real);
+
+-- Claude Sonnet 5.5 is already used by chat but had no price row, so its usage
+-- recorded a NULL total. $2 in / $10 out per the Claude pricing page below.
+INSERT INTO ai_pricing (model, price_per_million_in, price_per_million_out) VALUES
+ ('claude-sonnet-5-5', 2.0, 10.0)
+ON CONFLICT (model) DO NOTHING;
+
+-- Cache rates in USD per million tokens, checked 2026-10-05. Only rows that
+-- exist are updated; models absent here keep NULL cache rates.
+--
+-- Anthropic: https://platform.claude.com/docs/en/about-claude/pricing
+-- "5m cache writes" and "Cache hits and refreshes" columns. Writes are the
+-- 5-minute TTL (1.25x input), the only TTL the agent sends; usage does not
+-- split 5m from 1h writes, so 1h caching must not be enabled without a
+-- separate dimension. Fable 5.1 reads are 0.025x input ($0.25).
+-- claude-3-7-sonnet, claude-3-5-sonnet, claude-3-opus and claude-3-haiku are
+-- no longer listed, so they get no cache rates.
+--
+-- OpenAI: https://developers.openai.com/api/docs/pricing ("cached input" and
+-- "cache writes" columns, short context) and
+-- https://developers.openai.com/api/docs/guides/prompt-caching ("Cache writes
+-- cost 1.25x the standard, uncached input-token rate" for GPT-5.6 and later;
+-- earlier models have no cache-write charge, so no write rate).
+-- gpt-5.6 and gpt-5.6-mini are no longer listed by id; their rates apply the
+-- documented GPT-5.6 rule (reads 0.1x, writes 1.25x) to their seeded input
+-- rate. gpt-5-mini is seeded at the gpt-5.4-mini tier, so it takes that
+-- tier's cached rate. The -pro models offer no cached input.
+--
+-- Cerebras: https://inference-docs.cerebras.ai/capabilities/prompt-caching
+-- "Input tokens, whether served from the cache or processed fresh, are
+-- billed at the standard input token rate", so reads cost the input rate.
+--
+-- Fireworks: https://docs.fireworks.ai/serverless/pricing (input / cached
+-- input / output). deepseek-v4-pro-0813 and muse-glimmer-30b are not in the
+-- published table, so they get no cache rates.
+--
+-- Google: https://ai.google.dev/gemini-api/docs/pricing. Gemini 3.8 Flash
+-- context-caching reads are $0.075 through 2026-12-31 and $0.15 from
+-- 2027-01-01 (apply that with set_pricing alongside the input increase).
+-- Implicit caching has no write charge. Gemini 2.5 Flash Image lists no
+-- cached rate.
+UPDATE ai_pricing AS p
+SET price_per_million_cache_read = r.cache_read,
+ price_per_million_cache_write = r.cache_write,
+ updated_at = NOW()
+FROM (VALUES
+ -- Anthropic
+ ('claude-fable-5-1', 0.25::real, 12.50::real),
+ ('claude-fable-5', 1.00::real, 12.50::real),
+ ('claude-mythos-5', 1.00::real, 12.50::real),
+ ('claude-opus-5', 0.50::real, 6.25::real),
+ ('claude-opus-4-8', 0.50::real, 6.25::real),
+ ('claude-opus-4-7', 0.50::real, 6.25::real),
+ ('claude-opus-4-6', 0.50::real, 6.25::real),
+ ('claude-opus-4-5', 0.50::real, 6.25::real),
+ ('claude-opus-4-1', 1.50::real, 18.75::real),
+ ('claude-opus-4-0', 1.50::real, 18.75::real),
+ ('claude-sonnet-5-5', 0.20::real, 2.50::real),
+ ('claude-sonnet-5', 0.20::real, 2.50::real),
+ ('claude-sonnet-4-6', 0.30::real, 3.75::real),
+ ('claude-sonnet-4-5', 0.30::real, 3.75::real),
+ ('claude-sonnet-4-0', 0.30::real, 3.75::real),
+ ('claude-haiku-4-5', 0.10::real, 1.25::real),
+ ('claude-3-5-haiku', 0.08::real, 1.00::real),
+ -- OpenAI
+ ('gpt-6-astra', 1.00::real, 12.50::real),
+ ('gpt-5.6', 0.50::real, 6.25::real),
+ ('gpt-5.6-mini', 0.075::real, 0.9375::real),
+ ('gpt-5.5', 0.50::real, NULL::real),
+ ('gpt-5.4', 0.25::real, NULL::real),
+ ('gpt-5.4-mini', 0.075::real, NULL::real),
+ ('gpt-5.4-nano', 0.02::real, NULL::real),
+ ('gpt-5.3-codex', 0.175::real, NULL::real),
+ ('gpt-5-mini', 0.075::real, NULL::real),
+ -- Cerebras
+ ('gpt-oss-120b', 0.35::real, NULL::real),
+ ('zai-glm-4.7', 2.25::real, NULL::real),
+ -- Fireworks
+ ('kimi-k3', 0.30::real, NULL::real),
+ ('glm-5p3', 0.26::real, NULL::real),
+ ('glm-5p3-flash', 0.03::real, NULL::real),
+ ('qwen3p8-max', 0.25::real, NULL::real),
+ ('minimax-m3', 0.06::real, NULL::real),
+ ('nemotron-lightning-3p5-30b-a3b', 0.01::real, NULL::real),
+ -- Google
+ ('gemini-3.8-flash', 0.075::real, NULL::real)
+) AS r(model, cache_read, cache_write)
+WHERE p.model = r.model;
diff --git a/packages/sdk/generated/cognition/types.gen.ts b/packages/sdk/generated/cognition/types.gen.ts
index bd20b33c32..bf38d39714 100644
--- a/packages/sdk/generated/cognition/types.gen.ts
+++ b/packages/sdk/generated/cognition/types.gen.ts
@@ -1110,6 +1110,16 @@ export type Price = {
* Price per audio minute (USD), absent for token billing.
*/
price_per_audio_minute?: number | null;
+ /**
+ * Price per million cache-read input tokens (USD), absent when the model
+ * has no published rate and for audio billing.
+ */
+ price_per_million_cache_read?: number | null;
+ /**
+ * Price per million cache-write input tokens (USD), absent when the model
+ * has no published rate and for audio billing.
+ */
+ price_per_million_cache_write?: number | null;
/**
* Price per million input tokens (USD); zero for audio billing.
*/
@@ -1300,6 +1310,16 @@ export type SetPricingRequest = {
* Price per minute of audio (USD), or null for token-only pricing.
*/
price_per_audio_minute?: number | null;
+ /**
+ * Price per million cache-read input tokens (USD). Omit it when the model
+ * has no published rate: calls that report cache reads then stay unpriced.
+ */
+ price_per_mil_cache_read?: number | null;
+ /**
+ * Price per million cache-write input tokens (USD). Omit it when the model
+ * has no published rate: calls that report cache writes then stay unpriced.
+ */
+ price_per_mil_cache_write?: number | null;
/**
* New price per million input tokens (USD). Required for token pricing.
*/
@@ -1652,12 +1672,20 @@ export type Usage = {
* Audio duration in seconds, absent for token billing.
*/
audio_seconds?: number | null;
+ /**
+ * Input tokens read from a prompt cache; zero for audio billing.
+ */
+ cache_read_input_tokens: number;
+ /**
+ * Input tokens written to a prompt cache; zero for audio billing.
+ */
+ cache_write_input_tokens: number;
/**
* Recording timestamp.
*/
created_at: string;
/**
- * Input tokens; zero for audio billing.
+ * Uncached input tokens; zero for audio billing.
*/
input_tokens: number;
/**
diff --git a/packages/sdk/specs/cognition.json b/packages/sdk/specs/cognition.json
index 404f2e14b7..634f37e3da 100644
--- a/packages/sdk/specs/cognition.json
+++ b/packages/sdk/specs/cognition.json
@@ -4280,6 +4280,16 @@
"format": "float",
"description": "Price per audio minute (USD), absent for token billing."
},
+ "price_per_million_cache_read": {
+ "type": ["number", "null"],
+ "format": "float",
+ "description": "Price per million cache-read input tokens (USD), absent when the model\nhas no published rate and for audio billing."
+ },
+ "price_per_million_cache_write": {
+ "type": ["number", "null"],
+ "format": "float",
+ "description": "Price per million cache-write input tokens (USD), absent when the model\nhas no published rate and for audio billing."
+ },
"price_per_million_in": {
"type": "number",
"format": "float",
@@ -4513,6 +4523,16 @@
"format": "float",
"description": "Price per minute of audio (USD), or null for token-only pricing."
},
+ "price_per_mil_cache_read": {
+ "type": ["number", "null"],
+ "format": "float",
+ "description": "Price per million cache-read input tokens (USD). Omit it when the model\nhas no published rate: calls that report cache reads then stay unpriced."
+ },
+ "price_per_mil_cache_write": {
+ "type": ["number", "null"],
+ "format": "float",
+ "description": "Price per million cache-write input tokens (USD). Omit it when the model\nhas no published rate: calls that report cache writes then stay unpriced."
+ },
"price_per_mil_in": {
"type": ["number", "null"],
"format": "float",
@@ -5103,13 +5123,32 @@
"Usage": {
"type": "object",
"description": "Measured usage for one invocation.",
- "required": ["input_tokens", "output_tokens", "model", "created_at"],
+ "required": [
+ "input_tokens",
+ "output_tokens",
+ "cache_read_input_tokens",
+ "cache_write_input_tokens",
+ "model",
+ "created_at"
+ ],
"properties": {
"audio_seconds": {
"type": ["number", "null"],
"format": "double",
"description": "Audio duration in seconds, absent for token billing."
},
+ "cache_read_input_tokens": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Input tokens read from a prompt cache; zero for audio billing.",
+ "minimum": 0
+ },
+ "cache_write_input_tokens": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Input tokens written to a prompt cache; zero for audio billing.",
+ "minimum": 0
+ },
"created_at": {
"type": "string",
"format": "date-time",
@@ -5118,7 +5157,7 @@
"input_tokens": {
"type": "integer",
"format": "int64",
- "description": "Input tokens; zero for audio billing.",
+ "description": "Uncached input tokens; zero for audio billing.",
"minimum": 0
},
"model": {