Cost Tracking

SwarmClaw tracks token usage and estimates costs for every message across all providers.

How It Works

Each message exchanged with an LLM provider includes token counts in the response metadata. SwarmClaw records:

  • Input tokens — tokens sent to the provider (system prompt + conversation history + user message)
  • Output tokens — tokens generated by the provider
  • Total tokens — sum of input and output
  • Estimated cost — calculated from the provider's pricing per token

Token counts are stored per-message in the session data and aggregated for reporting.

Usage API

Retrieve usage data via the REST API:

GET /api/usage

Query Parameters

ParameterDescription
fromStart date (ISO 8601). Default: 30 days ago
toEnd date (ISO 8601). Default: now
agentFilter by agent ID
providerFilter by provider name
sessionFilter by session ID

Response

{
  "totalInputTokens": 1250000,
  "totalOutputTokens": 340000,
  "totalTokens": 1590000,
  "estimatedCost": 12.45,
  "byAgent": { ... },
  "byProvider": { ... },
  "daily": [ ... ]
}

UsageBadge

The UsageBadge component in the chat header shows a live token count and estimated cost for the current session. It updates after each message.

Cost Estimation

Costs are estimated using built-in pricing tables for known providers (Anthropic, OpenAI). For custom providers and Ollama, cost is reported as $0 unless you configure custom pricing in the provider settings.