Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
ai_usage
What it is
Part of billing: a robust log of AI usage with cost computed from model id
and token counts (or audio duration, or provider credits a vendor such as
Tavily bills), feeding ai_billing's admission and ledger decisions, plus a
flexible admin query API.
Start here
src/lib.rsdoc comment: the layering (domain/outbound/inbound) and the split between best-effort analytics tracking and the awaited, fail-closedFinancialUsagecapability for billable traffic.src/domain/financial.rs+src/domain/financial_service.rs: immutable provider cost evidence and exact customer-money calculation.src/domain/tracking.rs:UsageTracking— the always-recorded log, used for analytics regardless of billing status.src/domain/counting.rs:is_billable_featureand theNON_BILLABLE_AI_FEATURESallowlist.src/outbound/pg_usage_repo.rs,pg_tracking_repo.rs,pg_financial_usage_repo.rs: the Postgres adapters.
Scope
- Owns: pricing a model invocation and logging usage/cost.
- Not here: plan allowances, credits, and settlement →
ai_billing(reads this crate's financial usage, never recomputes cost itself).
Boundaries
- Depends on: none notable outside the workspace-hack set.
- Used by:
agent,agent_session,agent_harness,agent_trigger,ai_billing,ai_tools,ai_projections,anthropic,call,documents,import,image_generation,memory,channel_bots,agent_inmem,dictation,task_dedup,jev.
Recipes
- Price a new model:
src/domain/financial.rs(rate entry) →src/config.rs(if a new Doppler-sourced rate) → test insrc/config/test.rs. - Mark a feature non-billable:
src/domain/counting.rs'sNON_BILLABLE_AI_FEATURES.
Checks
cargo test -p ai_usage(SQLX_OFFLINEunset).
Gotchas
UsageRecorder(analytics) is best-effort and never fails the originating call;FinancialUsage(billable) is awaited and fail-closed — don't conflate the two when adding a new recording path.- Read prices from the
ai_effective_pricingview, neverai_pricingdirectly: it appliesai_pricing_discount, a per-model input and output percent off (input's also covers cache, audio and provider-credit rates). Gemini 3.8 Flash is discounted to DeepSeek V4.1 Flash's $0.30 / $1.20. UsageContext::with_agent_api_keymarks usage an agent's own API key paid for: it is stored withagent_api_key_idand never counted. Set it only on calls that actually authenticate with that key.
Related
- Rules:
.agents/rules/config-and-service-urls.md