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.

fix(ai-billing): exclude free AI features from paid usage (#6997)

Will Hutchinsonf06438e99c02
diff --git a/.sqlx/query-a51667f575cf4e79bfb47146b9657f8069857ea4052183dd59b7f0dbb78ed1af.json b/.sqlx/query-868810c2bc77f71ff76184c0490b6e8bb39d9a2874441f48bd64947cca7280f1.json
similarity index 83%
rename from .sqlx/query-a51667f575cf4e79bfb47146b9657f8069857ea4052183dd59b7f0dbb78ed1af.json
rename to .sqlx/query-868810c2bc77f71ff76184c0490b6e8bb39d9a2874441f48bd64947cca7280f1.json
index 1972a750ef..51fa430929 100644
--- a/.sqlx/query-a51667f575cf4e79bfb47146b9657f8069857ea4052183dd59b7f0dbb78ed1af.json
+++ b/.sqlx/query-868810c2bc77f71ff76184c0490b6e8bb39d9a2874441f48bd64947cca7280f1.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 feature <> $6\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 )\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 feature <> ALL($6)\n GROUP BY user_id\n ",
"describe": {
"columns": [
{
@@ -21,7 +21,7 @@
"Timestamptz",
"Float8",
"Float8",
- "Text"
+ "TextArray"
]
},
"nullable": [
@@ -29,5 +29,5 @@
null
]
},
- "hash": "a51667f575cf4e79bfb47146b9657f8069857ea4052183dd59b7f0dbb78ed1af"
+ "hash": "868810c2bc77f71ff76184c0490b6e8bb39d9a2874441f48bd64947cca7280f1"
}
diff --git a/crates/ai_billing/src/domain/models.rs b/crates/ai_billing/src/domain/models.rs
index 89c2a1a17b..161f932b28 100644
--- a/crates/ai_billing/src/domain/models.rs
+++ b/crates/ai_billing/src/domain/models.rs
@@ -1,5 +1,6 @@
//! Plans, the margin math, billing periods, and the API-facing snapshot.
+use ai_usage::AiFeature;
use chrono::{DateTime, Datelike, Months, TimeZone, Utc};
use macro_user_id::user_id::MacroUserIdStr;
use macro_uuid::Uuid;
@@ -15,6 +16,15 @@ pub const TARGET_GROSS_MARGIN_BPS: i64 = 6_000;
const BPS_PER_UNIT: i64 = 10_000;
+/// Features whose provider costs are recorded but never consume allowances,
+/// prepaid credits, or overage. Dictation is the Whispr transcription feature.
+pub const NON_BILLABLE_AI_FEATURES: [AiFeature; 4] = [
+ AiFeature::Memory,
+ AiFeature::AiProjection,
+ AiFeature::CallSummary,
+ AiFeature::Dictation,
+];
+
/// Convert a provider cost in USD to Macro's list rate in whole cents,
/// rounding up so fractional cents never accrue in the customer's favour.
pub fn list_rate_cents(provider_cost_usd: f64) -> i64 {
diff --git a/crates/ai_billing/src/domain/ports.rs b/crates/ai_billing/src/domain/ports.rs
index a08dcc6635..aa6e02305c 100644
--- a/crates/ai_billing/src/domain/ports.rs
+++ b/crates/ai_billing/src/domain/ports.rs
@@ -38,8 +38,9 @@ pub trait EntitlementSource: Send + Sync + 'static {
pub trait UsageReader: Send + Sync + 'static {
/// List-rate usage for each of `users` within `period`.
///
- /// AI projections remain in `ai_usage` for cost tracking but do not
- /// consume a user's allowance, credits, or overage.
+ /// [`NON_BILLABLE_AI_FEATURES`](super::models::NON_BILLABLE_AI_FEATURES)
+ /// remain in `ai_usage` for cost tracking but do not consume a user's
+ /// allowance, credits, or overage.
fn list_rate_usage_cents_by_user(
&self,
users: &[MacroUserIdStr<'static>],
diff --git a/crates/ai_billing/src/outbound/pg_usage_reader.rs b/crates/ai_billing/src/outbound/pg_usage_reader.rs
index f01f687f0f..a6de5bed91 100644
--- a/crates/ai_billing/src/outbound/pg_usage_reader.rs
+++ b/crates/ai_billing/src/outbound/pg_usage_reader.rs
@@ -3,8 +3,8 @@
#[cfg(test)]
mod test;
+use crate::domain::models::NON_BILLABLE_AI_FEATURES;
use crate::domain::{BillingError, BillingPeriod, Result, SeatUsage, UsageReader, list_rate_cents};
-use ai_usage::AiFeature;
use macro_user_id::user_id::MacroUserIdStr;
use sqlx::PgPool;
@@ -41,6 +41,7 @@ impl UsageReader for PgUsageReader {
return Ok(Vec::new());
}
let ids: Vec<String> = users.iter().map(|u| u.as_ref().to_string()).collect();
+ let non_billable_features = NON_BILLABLE_AI_FEATURES.map(|feature| feature.to_string());
let rows = sqlx::query!(
r#"
SELECT user_id, COALESCE(SUM(
@@ -54,7 +55,7 @@ impl UsageReader for PgUsageReader {
WHERE user_id = ANY($1)
AND created_at >= $2
AND created_at < $3
- AND feature <> $6
+ AND feature <> ALL($6)
GROUP BY user_id
"#,
&ids,
@@ -62,7 +63,7 @@ impl UsageReader for PgUsageReader {
period.end,
FALLBACK_PRICE_PER_MILLION_IN,
FALLBACK_PRICE_PER_MILLION_OUT,
- AiFeature::AiProjection.to_string(),
+ &non_billable_features,
)
.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 3196c80b48..9b6dae525f 100644
--- a/crates/ai_billing/src/outbound/pg_usage_reader/test.rs
+++ b/crates/ai_billing/src/outbound/pg_usage_reader/test.rs
@@ -1,7 +1,7 @@
use super::*;
use ai_usage::outbound::PgUsageRepo;
use ai_usage::{
- CompletionUsage, ModelPricing, Price, Usage, UsageAmount, UsageApiParams, UsageRepo,
+ AiFeature, CompletionUsage, ModelPricing, Price, Usage, UsageAmount, UsageApiParams, UsageRepo,
};
use chrono::{Duration, Utc};
use macro_db_migrator::MACRO_DB_MIGRATIONS;
@@ -33,53 +33,122 @@ fn completion(
}
}
+fn free_completions(user: MacroUserIdStr<'static>) -> [CompletionUsage; 4] {
+ let mut dictation = completion(user.clone(), AiFeature::Dictation, 8.0);
+ dictation.cost.model = "test-audio-model".to_string();
+ dictation.cost.amount = UsageAmount::Audio {
+ duration: std::time::Duration::from_secs(60),
+ };
+ dictation.cost.price = Some(Price {
+ pricing: ModelPricing::Audio { per_minute: 8.0 },
+ total: 8.0,
+ });
+ [
+ completion(user.clone(), AiFeature::Memory, 8.0),
+ completion(user.clone(), AiFeature::AiProjection, 8.0),
+ completion(user, AiFeature::CallSummary, 8.0),
+ dictation,
+ ]
+}
+
+fn current_period() -> BillingPeriod {
+ let now = Utc::now();
+ BillingPeriod {
+ start: now - Duration::minutes(1),
+ end: now + Duration::minutes(1),
+ }
+}
+
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
-async fn ai_projection_usage_is_recorded_but_not_metered(pool: PgPool) {
- let user =
- MacroUserIdStr::try_from("macro|[email protected]".to_string()).unwrap();
+async fn free_features_are_recorded_but_only_chat_and_editing_are_metered(pool: PgPool) {
+ let user = MacroUserIdStr::try_from("macro|[email protected]".to_string()).unwrap();
let usage_repo = PgUsageRepo::new(pool.clone());
- usage_repo
- .insert_usage(&completion(user.clone(), AiFeature::Chat, 4.0))
- .await
- .unwrap();
- usage_repo
- .insert_usage(&completion(user.clone(), AiFeature::AiProjection, 8.0))
- .await
- .unwrap();
+ for usage in free_completions(user.clone()).into_iter().chain([
+ completion(user.clone(), AiFeature::Chat, 4.0),
+ completion(user.clone(), AiFeature::AiEditing, 2.0),
+ ]) {
+ usage_repo.insert_usage(&usage).await.unwrap();
+ }
- let mut stored_features: Vec<_> = usage_repo
+ let stored_usage = usage_repo
.query_usage(&UsageApiParams {
include_users: vec![user.clone()],
..Default::default()
})
.await
- .unwrap()
- .into_iter()
- .map(|usage| usage.feature)
- .collect();
+ .unwrap();
+ assert_eq!(stored_usage.len(), 6);
+ assert!(
+ stored_usage
+ .iter()
+ .all(|usage| { usage.cost.price.is_some_and(|price| price.total > 0.0) })
+ );
+ let mut stored_features: Vec<_> = stored_usage.iter().map(|usage| usage.feature).collect();
stored_features.sort();
+ let mut expected_features = vec![
+ AiFeature::Chat,
+ AiFeature::Memory,
+ AiFeature::CallSummary,
+ AiFeature::AiProjection,
+ AiFeature::AiEditing,
+ AiFeature::Dictation,
+ ];
+ expected_features.sort();
+ assert_eq!(stored_features, expected_features);
+
+ let usage = PgUsageReader::new(pool)
+ .list_rate_usage_cents_by_user(&[user.clone()], current_period())
+ .await
+ .unwrap();
+
assert_eq!(
- stored_features,
- vec![AiFeature::Chat, AiFeature::AiProjection]
+ usage,
+ vec![SeatUsage {
+ user,
+ used_cents: 1_500,
+ }]
);
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn free_features_alone_do_not_produce_billable_usage(pool: PgPool) {
+ let user = MacroUserIdStr::try_from("macro|[email protected]".to_string()).unwrap();
+ let usage_repo = PgUsageRepo::new(pool.clone());
+ for usage in free_completions(user.clone()) {
+ usage_repo.insert_usage(&usage).await.unwrap();
+ }
- let now = Utc::now();
let usage = PgUsageReader::new(pool)
- .list_rate_usage_cents_by_user(
- &[user.clone()],
- BillingPeriod {
- start: now - Duration::minutes(1),
- end: now + Duration::minutes(1),
- },
- )
+ .list_rate_usage_cents_by_user(&[user], current_period())
+ .await
+ .unwrap();
+ assert!(usage.is_empty());
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn unpriced_free_features_are_excluded_from_fallback_billing(pool: PgPool) {
+ let user = MacroUserIdStr::try_from("macro|[email protected]".to_string()).unwrap();
+ let usage_repo = PgUsageRepo::new(pool.clone());
+ for mut usage in free_completions(user.clone()).into_iter().chain([
+ completion(user.clone(), AiFeature::Chat, 4.0),
+ completion(user.clone(), AiFeature::AiEditing, 2.0),
+ ]) {
+ usage.cost.price = None;
+ usage_repo.insert_usage(&usage).await.unwrap();
+ }
+
+ let usage = PgUsageReader::new(pool)
+ .list_rate_usage_cents_by_user(&[user.clone()], current_period())
.await
.unwrap();
+ // Chat and editing each fall back to $5 per million input tokens, with
+ // the 2.5x list-rate markup. Unpriced free features add nothing.
assert_eq!(
usage,
vec![SeatUsage {
user,
- used_cents: 1_000,
+ used_cents: 2_500,
}]
);
}