Back to Docs

Cost Management

Track, optimize, and control your AI spending. Understand how CoFounder handles cost tracking across providers, enforce budgets, route to cheaper models, and set up alerts.

How Cost Tracking Works

CoFounder calculates costs by counting input and output tokens for each API call and multiplying by the model's per-token pricing. Costs are tracked automatically when you use the OpenClaw guard, the dashboard, or the adapters package. The system maintains a running total per budget period (request, hour, day, or month) and can enforce limits at each level.

import { createOpenClawSkill } from '@waymakerai/aicofounder-openclaw';

const skill = createOpenClawSkill({
  model: 'claude-sonnet-4-20250514',
  budget: {
    limit: 100,              // $100 per period
    period: 'day',           // 'request' | 'hour' | 'day' | 'month'
    warningThreshold: 0.8,   // Warn at 80% usage
    onExceeded: 'block',     // 'block' | 'warn' | 'downgrade'
  },
});

// Every guard check automatically tracks cost
const result = await skill.hooks.beforeMessage(
  { role: 'user', content: 'Explain quantum computing' },
  { user: { id: 'user-123' } }
);

// Cost is available on the guard result
console.log(result.guardResult.cost);
// {
//   model: 'claude-sonnet-4-20250514',
//   provider: 'anthropic',
//   inputTokens: 12,
//   outputTokens: 0,     // Input guard, no output yet
//   inputCost: 0.000036,
//   outputCost: 0,
//   totalCost: 0.000036,
//   budgetRemaining: 99.99,
//   budgetWarning: false,
// }

// Get cost report
const costReport = skill.getCostReport();
console.log(costReport.totalSpent);      // $45.23
console.log(costReport.budgetLimit);     // $100.00
console.log(costReport.budgetRemaining); // $54.77
console.log(costReport.period);          // 'day'
console.log(costReport.byModel);
// { 'claude-sonnet-4-20250514': 40.00, 'gpt-4o': 5.23 }

Supported Models & Pricing

CoFounder maintains a built-in pricing table for all major models. Pricing is automatically updated. You can also configure custom pricing for self-hosted or fine-tuned models.

ModelInput / 1M tokensOutput / 1M tokensContext Window
Claude Sonnet 4$3.00$15.00200K
Claude Opus 4$15.00$75.00200K
Claude Haiku 3.5$0.80$4.00200K
GPT-4o$2.50$10.00128K
GPT-4o mini$0.15$0.60128K
GPT-4 Turbo$10.00$30.00128K
Gemini 1.5 Pro$1.25$5.002M
Gemini 1.5 Flash$0.075$0.301M
Llama 3.1 405B$3.00$3.00128K
Llama 3.1 70B$0.70$0.90128K
Mistral Large$2.00$6.00128K

Prices are approximate and may change. CoFounder tracks actual costs reported by each provider API.

Budget Enforcement Strategies

CoFounder supports three actions when a budget is exceeded. Configure this through the OpenClaw skill or the policies system.

Block

Reject the request entirely. Returns a budget exceeded error to the caller. Best for hard spending limits.

Warn

Allow the request but log a warning and trigger alert notifications. Best for soft limits where you want visibility without interrupting users.

Downgrade

Automatically switch to a cheaper model. For example, downgrade from Claude Sonnet to Claude Haiku when the daily budget is exceeded. Maintains availability while controlling cost.

// Budget enforcement through policies
// policies/cost-controls.yml
rules:
  cost:
    enabled: true
    maxCostPerRequest: 0.50      # Block any single request over $0.50
    maxCostPerDay: 100.00        # Daily limit
    maxCostPerMonth: 2000.00     # Monthly limit
    maxTokensPerRequest: 50000   # Token limit per request
    maxCompletionTokens: 4096    # Output token limit

// Programmatic budget enforcement
import { PolicyEvaluator, PolicyLoader } from '@waymakerai/aicofounder-policies';

const loader = new PolicyLoader();
const policy = await loader.loadFile('./policies/cost-controls.yml');
const evaluator = new PolicyEvaluator(policy);

// Check if a request is within budget
const result = evaluator.evaluate({
  cost: 0.03,                    // Estimated cost
  tokens: 2500,                  // Estimated tokens
  dailyCost: 85.00,              // Accumulated daily spend
  monthlyCost: 1450.00,          // Accumulated monthly spend
});

if (!result.passed) {
  const costViolations = result.violations.filter(v => v.category === 'cost');
  console.log(costViolations);
  // [{ rule: 'maxCostPerDay', severity: 'high', message: 'Daily cost limit exceeded' }]
}

Model Routing for Cost Optimization

CoFounder's model router analyzes the complexity of each request and routes it to the most cost-effective model that can handle it. Simple queries go to cheaper models while complex reasoning tasks use premium models.

import { ModelRouter } from '@waymakerai/aicofounder-core';

const router = new ModelRouter({
  models: [
    {
      id: 'claude-haiku',
      model: 'claude-3-5-haiku-20241022',
      provider: 'anthropic',
      costPerInputToken: 0.0000008,
      costPerOutputToken: 0.000004,
      capabilities: ['simple-chat', 'classification', 'extraction'],
      maxComplexity: 'low',
    },
    {
      id: 'claude-sonnet',
      model: 'claude-sonnet-4-20250514',
      provider: 'anthropic',
      costPerInputToken: 0.000003,
      costPerOutputToken: 0.000015,
      capabilities: ['chat', 'reasoning', 'coding', 'analysis'],
      maxComplexity: 'high',
    },
    {
      id: 'claude-opus',
      model: 'claude-opus-4-20250514',
      provider: 'anthropic',
      costPerInputToken: 0.000015,
      costPerOutputToken: 0.000075,
      capabilities: ['complex-reasoning', 'research', 'creative'],
      maxComplexity: 'very-high',
    },
  ],
  strategy: 'cost-optimized',    // 'cost-optimized' | 'quality-first' | 'balanced'
  fallback: 'claude-sonnet',     // Default if routing fails
});

// Route a request to the cheapest suitable model
const route = await router.route({
  messages: [{ role: 'user', content: 'What is 2 + 2?' }],
  requiredCapabilities: ['simple-chat'],
});

console.log(route.selectedModel);  // 'claude-haiku' (cheapest for simple queries)
console.log(route.estimatedCost);  // $0.0001
console.log(route.reason);         // 'Simple query routed to cheapest model'

// Complex query routes to more capable model
const complexRoute = await router.route({
  messages: [{ role: 'user', content: 'Analyze this 50-page legal document...' }],
  requiredCapabilities: ['complex-reasoning', 'analysis'],
});

console.log(complexRoute.selectedModel); // 'claude-sonnet' or 'claude-opus'

Caching Strategies

Caching identical or semantically similar requests can reduce costs by 40-70%. CoFounder supports exact-match caching and semantic caching (where similar questions return cached responses if the similarity score is above a threshold).

import { configure } from '@waymakerai/aicofounder-core';

configure({
  cache: {
    enabled: true,
    ttl: '1h',                   // Cache duration
    storage: 'redis',            // 'memory' | 'redis'
    redisUrl: process.env.REDIS_URL,

    // Exact match caching
    exactMatch: true,

    // Semantic caching - returns cached result for similar queries
    semanticMatch: {
      enabled: true,
      threshold: 0.92,           // Minimum similarity score
      embeddingModel: 'text-embedding-3-small',
    },

    // Cache key configuration
    keyStrategy: 'content-hash', // 'content-hash' | 'full-request'
    includeModel: true,          // Different models get different cache entries
    includeTemperature: true,    // Different temperatures get different entries

    // Cache warming
    warmup: [
      // Pre-cache common queries
      { query: 'What are your business hours?', model: 'claude-haiku' },
      { query: 'How do I reset my password?', model: 'claude-haiku' },
    ],
  },
});

// Cache hit statistics
const stats = await getCacheStats();
console.log(stats.hits);         // 12,000
console.log(stats.misses);       // 8,000
console.log(stats.hitRate);      // 0.60 (60% cache hit rate)
console.log(stats.costSaved);    // $450.00 saved from cache hits

Cost Reports & Analytics

The dashboard provides detailed cost analytics across multiple dimensions. Query by time period, model, provider, user, or custom tags.

import { createDashboard } from '@waymakerai/aicofounder-dashboard';

const dashboard = createDashboard({ storage: 'file', storagePath: './data/events.json' });

// Cost metrics by period
const costs = await dashboard.getCostMetrics({ period: 'month' });
console.log(costs.total);               // $2,345.67
console.log(costs.projectedMonthly);    // $3,100.00
console.log(costs.trend);               // 'increasing' | 'decreasing' | 'stable'

// Cost by model
console.log(costs.byModel);
// {
//   'claude-sonnet-4-20250514': 1500.00,
//   'gpt-4o': 600.00,
//   'claude-3-5-haiku-20241022': 245.67,
// }

// Cost by provider
console.log(costs.byProvider);
// { anthropic: 1745.67, openai: 600.00 }

// Cost over time
console.log(costs.byPeriod);
// [
//   { period: '2025-01-01', cost: 75.00 },
//   { period: '2025-01-02', cost: 82.00 },
//   ...
// ]

// Export for finance team
const csvReport = await dashboard.export({
  format: 'csv',
  from: Date.now() - 30 * 86400000,  // Last 30 days
  type: 'cost',
});

const jsonReport = await dashboard.export({
  format: 'json',
  from: Date.now() - 30 * 86400000,
  type: 'cost',
});

Budget Alerts

Configure proactive alerts for budget thresholds, cost spikes, and anomalies. Alerts can be sent to Slack, email, PagerDuty, or any webhook endpoint.

import { createDashboard } from '@waymakerai/aicofounder-dashboard';

const dashboard = createDashboard({
  storage: 'file',
  storagePath: './data/events.json',
  alerts: [
    {
      type: 'budget',
      enabled: true,
      thresholds: {
        daily: 50,       // Alert at $50/day
        monthly: 1000,   // Alert at $1000/month
      },
    },
    {
      type: 'anomaly',
      enabled: true,
      thresholds: {
        stddev: 3,       // Alert on 3x standard deviation in hourly spend
      },
    },
  ],
});

// Listen for alerts
dashboard.onAlert((alert) => {
  console.log(`[${alert.level}] ${alert.type}: ${alert.message}`);
  // [warning] budget: Daily spend at 82% of $50 limit ($41.00)
  // [critical] budget: Monthly budget exceeded: $1,023.45 / $1,000.00
  // [critical] anomaly: Hourly cost spike: $45.00 (avg: $8.00)

  // Send notifications
  if (alert.level === 'critical') {
    sendSlackAlert(alert);
    sendPagerDutyAlert(alert);
  } else {
    sendSlackAlert(alert);
  }
});

// Check active alerts
const activeAlerts = await dashboard.getActiveAlerts();
for (const alert of activeAlerts) {
  console.log(`${alert.id}: ${alert.message} (since ${new Date(alert.timestamp)})`);
}

// Acknowledge an alert
await dashboard.acknowledgeAlert(activeAlerts[0].id);

// OpenClaw budget warning threshold
// When budget hits 80%, the guard result includes a warning:
const guardResult = await skill.hooks.beforeMessage(message, context);
if (guardResult.guardResult.cost?.budgetWarning) {
  console.log(`Budget warning: ${guardResult.guardResult.cost.budgetRemaining} remaining`);
}

Cost Optimization Results

70%
Avg. Savings
With caching + routing
60%
Cache Hit Rate
Typical after warmup
45%
Model Routing
Savings from auto-routing
9+
Providers
Supported with pricing