ObjectStackObjectStack

Cost

Cost protocol schemas

AI Cost Tracking Protocol

Monitor and control AI API costs with budgets, alerts, and analytics.

Provides cost optimization, budget enforcement, and financial reporting.

Source: packages/spec/src/ai/cost.zod.ts

TypeScript Usage

import { AIOperationCost, BillingPeriod, BudgetLimit, BudgetStatus, BudgetType, CostAlert, CostAlertType, CostAnalytics, CostBreakdownDimension, CostBreakdownEntry, CostEntry, CostMetricType, CostOptimizationRecommendation, CostQueryFilters, CostReport, TokenUsage } from '@objectstack/spec/ai';
import type { AIOperationCost, BillingPeriod, BudgetLimit, BudgetStatus, BudgetType, CostAlert, CostAlertType, CostAnalytics, CostBreakdownDimension, CostBreakdownEntry, CostEntry, CostMetricType, CostOptimizationRecommendation, CostQueryFilters, CostReport, TokenUsage } from '@objectstack/spec/ai';

// Validate data
const result = AIOperationCost.parse(data);

AIOperationCost

Properties

PropertyTypeRequiredDescription
operationIdstring
operationTypeEnum<'conversation' | 'orchestration' | 'prediction' | 'rag' | 'nlq'>
agentNamestringoptionalAgent that performed the operation
modelIdstring
tokensObject
costnumberCost in USD
timestampstring
metadataRecord<string, any>optional

BillingPeriod

Allowed Values

  • hourly
  • daily
  • weekly
  • monthly
  • quarterly
  • yearly
  • custom

BudgetLimit

Properties

PropertyTypeRequiredDescription
typeEnum<'global' | 'user' | 'agent' | 'object' | 'project' | 'department'>
scopestringoptionalScope identifier (userId, agentId, etc.)
maxCostnumberMaximum cost limit
currencystring
periodEnum<'hourly' | 'daily' | 'weekly' | 'monthly' | 'quarterly' | 'yearly' | 'custom'>
customPeriodDaysintegeroptionalCustom period in days
softLimitnumberoptionalSoft limit for warnings
warnThresholdsnumber[]optionalWarning thresholds (e.g., [0.5, 0.8, 0.95])
enforcedbooleanBlock requests when exceeded
gracePeriodSecondsintegerGrace period after limit exceeded
allowRolloverbooleanAllow unused budget to rollover
maxRolloverPercentagenumberoptionalMax rollover as % of limit
namestringoptionalBudget name
descriptionstringoptional
activeboolean
tagsstring[]optional

BudgetStatus

Properties

PropertyTypeRequiredDescription
budgetIdstring
typeEnum<'global' | 'user' | 'agent' | 'object' | 'project' | 'department'>
scopestringoptional
periodStartstringISO 8601 timestamp
periodEndstringISO 8601 timestamp
currentCostnumber
maxCostnumber
currencystring
percentageUsednumberUsage as percentage (can exceed 1.0 if over budget)
remainingCostnumberRemaining budget (can be negative if exceeded)
isExceededboolean
isWarningboolean
projectedCostnumberoptionalProjected cost for period
projectedOveragenumberoptionalProjected overage
lastUpdatedstringISO 8601 timestamp

BudgetType

Allowed Values

  • global
  • user
  • agent
  • object
  • project
  • department

CostAlert

Properties

PropertyTypeRequiredDescription
idstring
timestampstringISO 8601 timestamp
typeEnum<'threshold_warning' | 'threshold_critical' | 'limit_exceeded' | 'anomaly_detected' | 'projection_exceeded'>
severityEnum<'info' | 'warning' | 'critical'>
budgetIdstringoptional
budgetTypeEnum<'global' | 'user' | 'agent' | 'object' | 'project' | 'department'>optional
scopestringoptional
messagestringAlert message
currentCostnumber
maxCostnumberoptional
thresholdnumberoptional
currencystring
recommendationsstring[]optional
acknowledgedboolean
acknowledgedBystringoptional
acknowledgedAtstringoptional
resolvedboolean
metadataRecord<string, any>optional

CostAlertType

Allowed Values

  • threshold_warning
  • threshold_critical
  • limit_exceeded
  • anomaly_detected
  • projection_exceeded

CostAnalytics

Properties

PropertyTypeRequiredDescription
periodStartstringISO 8601 timestamp
periodEndstringISO 8601 timestamp
totalCostnumber
totalRequestsinteger
totalTokensintegeroptional
currencystring
averageCostPerRequestnumber
averageCostPerTokennumberoptional
averageRequestsPerDaynumber
costTrendEnum<'increasing' | 'decreasing' | 'stable'>optional
trendPercentagenumberoptional% change vs previous period
byModelObject[]optional
byProviderObject[]optional
byUserObject[]optional
byAgentObject[]optional
byOperationObject[]optional
byDateObject[]optional
topModelsObject[]optional
topUsersObject[]optional
topAgentsObject[]optional
tokensPerDollarnumberoptional
requestsPerDollarnumberoptional

CostBreakdownDimension

Allowed Values

  • model
  • provider
  • user
  • agent
  • object
  • operation
  • date
  • hour
  • tag

CostBreakdownEntry

Properties

PropertyTypeRequiredDescription
dimensionEnum<'model' | 'provider' | 'user' | 'agent' | 'object' | 'operation' | 'date' | 'hour' | 'tag'>
valuestringDimension value (e.g., model ID, user ID)
totalCostnumber
requestCountinteger
totalTokensintegeroptional
percentageOfTotalnumber
periodStartstringoptional
periodEndstringoptional

CostEntry

Properties

PropertyTypeRequiredDescription
idstringUnique cost entry ID
timestampstringISO 8601 timestamp
modelIdstringAI model used
providerstringAI provider (e.g., "openai", "anthropic")
operationstringOperation type (e.g., "chat_completion", "embedding")
tokensObjectoptionalStandardized token usage
requestCountinteger
promptCostnumberoptionalCost of prompt tokens
completionCostnumberoptionalCost of completion tokens
totalCostnumberTotal cost in base currency
currencystring
sessionIdstringoptionalConversation session ID
userIdstringoptionalUser who triggered the request
agentIdstringoptionalAI agent ID
objectstringoptionalRelated object (e.g., "case", "project")
recordIdstringoptionalRelated record ID
tagsstring[]optional
metadataRecord<string, any>optional

CostMetricType

Allowed Values

  • token
  • request
  • character
  • second
  • image
  • embedding

CostOptimizationRecommendation

Properties

PropertyTypeRequiredDescription
idstring
typeEnum<'switch_model' | 'reduce_tokens' | 'batch_requests' | 'cache_results' | 'adjust_parameters' | 'limit_usage'>
titlestring
descriptionstring
estimatedSavingsnumberoptional
savingsPercentagenumberoptional
priorityEnum<'low' | 'medium' | 'high'>
effortEnum<'low' | 'medium' | 'high'>
actionableboolean
actionStepsstring[]optional
targetModelstringoptional
alternativeModelstringoptional
affectedUsersstring[]optional
statusEnum<'pending' | 'accepted' | 'rejected' | 'implemented'>
implementedAtstringoptional

CostQueryFilters

Properties

PropertyTypeRequiredDescription
startDatestringoptionalISO 8601 timestamp
endDatestringoptionalISO 8601 timestamp
modelIdsstring[]optional
providersstring[]optional
userIdsstring[]optional
agentIdsstring[]optional
operationsstring[]optional
sessionIdsstring[]optional
minCostnumberoptional
maxCostnumberoptional
tagsstring[]optional
groupByEnum<'model' | 'provider' | 'user' | 'agent' | 'object' | 'operation' | 'date' | 'hour' | 'tag'>[]optional
orderByEnum<'timestamp' | 'cost' | 'tokens'>
orderDirectionEnum<'asc' | 'desc'>
limitintegeroptional
offsetintegeroptional

CostReport

Properties

PropertyTypeRequiredDescription
idstring
namestring
generatedAtstringISO 8601 timestamp
periodStartstringISO 8601 timestamp
periodEndstringISO 8601 timestamp
periodEnum<'hourly' | 'daily' | 'weekly' | 'monthly' | 'quarterly' | 'yearly' | 'custom'>
analyticsObject
budgetsObject[]optional
alertsObject[]optional
activeAlertCountinteger
recommendationsObject[]optional
previousPeriodCostnumberoptional
costChangenumberoptionalChange vs previous period
costChangePercentagenumberoptional
forecastedCostnumberoptional
forecastedBudgetStatusEnum<'under' | 'at' | 'over'>optional
formatEnum<'summary' | 'detailed' | 'executive'>
currencystring

TokenUsage

Properties

PropertyTypeRequiredDescription
promptintegerInput tokens
completionintegerOutput tokens
totalintegerTotal tokens

On this page