Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

feat(metering): count cache reads and writes as their own metrics

Petr Bauch4300e51d5218
diff --git a/core/switch_core/db/models.py b/core/switch_core/db/models.py
index d7b69dd85..59b3c646d 100644
--- a/core/switch_core/db/models.py
+++ b/core/switch_core/db/models.py
@@ -2310,10 +2310,15 @@ for _table, _triggers in (
class UsageMetric(StrEnum):
+ """What is counted. Cache reads and writes are kept apart from input
+ tokens because providers price them apart."""
+
MESSAGES = "messages"
TURNS = "turns"
INPUT_TOKENS = "input_tokens"
OUTPUT_TOKENS = "output_tokens"
+ CACHE_READ_TOKENS = "cache_read_tokens"
+ CACHE_WRITE_TOKENS = "cache_write_tokens"
class TenantUsage(TenantScoped, Base):
diff --git a/core/switch_core/migrations/versions/c7e4a91d3b20_tenant_usage.py b/core/switch_core/migrations/versions/c7e4a91d3b20_tenant_usage.py
index 046663ac9..44573b964 100644
--- a/core/switch_core/migrations/versions/c7e4a91d3b20_tenant_usage.py
+++ b/core/switch_core/migrations/versions/c7e4a91d3b20_tenant_usage.py
@@ -61,7 +61,7 @@ def upgrade() -> None:
"tenant_id", "metric", "bucket_start", "client_id", "model"
),
sa.CheckConstraint(
- "metric IN ('messages', 'turns', 'input_tokens', 'output_tokens')",
+ "metric IN ('messages', 'turns', 'input_tokens', 'output_tokens', 'cache_read_tokens', 'cache_write_tokens')",
name="ck_tenant_usage_metric",
),
sa.CheckConstraint("amount > 0", name="ck_tenant_usage_amount"),