Skip to content

createCostTrackingMiddleware

createCostTrackingMiddleware(config): Middleware

Defined in: packages/middleware/src/cost-tracking.ts:456

Create cost tracking middleware

CostTrackingConfig = {}

Cost tracking configuration

Middleware

Middleware function

import { createCostTrackingMiddleware } from 'ai.matey';
const costTracking = createCostTrackingMiddleware({
logCosts: true,
onCost: (cost) => {
console.log(`Request cost: $${cost.totalCost.toFixed(6)}`);
}
});
bridge.use(costTracking);
const costTracking = createCostTrackingMiddleware({
providers: {
'anthropic': {
inputCostPer1M: 3.0,
outputCostPer1M: 15.0
}
},
models: [
{
model: /gpt-4-turbo/,
pricing: {
inputCostPer1M: 10.0,
outputCostPer1M: 30.0
}
}
]
});
const costTracking = createCostTrackingMiddleware({
requestThreshold: 0.10, // Warn if request costs > $0.10
hourlyThreshold: 10.00, // Warn if hourly cost > $10
dailyThreshold: 100.00, // Warn if daily cost > $100
onThresholdExceeded: (cost, threshold) => {
console.warn(`Cost threshold exceeded: $${cost.totalCost} > $${threshold}`);
}
});