Skip to content

Cost Tracking

CostTrackingService records token usage and computes estimated cost for every LLM call. Per-model pricing tables for 30+ models. Now cache-aware (as of 2026-07-10) — Anthropic prompt-cache reads are priced separately at their lower rate.

Per call:

  • Input tokens — prompt + context sent to the model
  • Output tokens — model response
  • Cache read — tokens served from the provider’s prompt cache (Anthropic)
  • Cache creation — first-time cache writes (Anthropic)
  • Estimated cost — 6-decimal precision

Cost is a weighted sum:

cost = uncached_input × input_rate
+ cache_read × cache_read_rate (Anthropic)
+ cache_creation × cache_creation_rate (Anthropic)
+ output × output_rate

All rates per million tokens.

  • Per message — token count under each response
  • Per conversation — cumulative cost for the current thread
  • Aggregate summary — breakdown by provider + model
{
"codebuddy.agent.costThreshold": 1000,
"codebuddy.agent.costThresholdWarningPercent": 80,
"codebuddy.agent.dailySpendCap": null
}
  • codebuddy.agent.costThreshold — maximum spend (USD) per conversation before CodeBuddy pauses and asks whether to continue. Default 1000.
  • codebuddy.agent.costThresholdWarningPercent — percentage of the threshold at which a warning is shown (1–99). Default 80.
  • codebuddy.agent.dailySpendCap — maximum total daily spend (USD) across all conversations. null disables it.

Per million tokens.

ProviderModelInputOutput
AnthropicClaude Sonnet 4$3.00$15.00
AnthropicClaude Opus 4$15.00$75.00
AnthropicClaude Haiku$0.25$1.25
OpenAIGPT-4o$2.50$10.00
OpenAIGPT-4o-mini$0.15$0.60
OpenAIo3-mini$1.10$4.40
GoogleGemini 2.5 Pro$1.25$10.00
GoogleGemini 2.5 Flash$0.15$0.60
GroqLlama 3.3 70B$0.59$0.79
DeepSeekDeepSeek Chat$0.27$1.10
DeepSeekDeepSeek Reasoner$0.55$2.19
QwenQwen Plus$0.80$2.00
xAIGrok$5.00$15.00
OllamaLocal modelsFreeFree

When a model isn’t in the pricing table, conservative fallback: $3.00 / $15.00 per million.

Anthropic cache rates live in the same table under separate columns (cacheReadPerMillion, cacheCreationPerMillion) — see cost-tracking.service.ts for exact numbers.

interface ICostSummary {
totals: {
inputTokens: number;
outputTokens: number;
cacheReadTokens?: number;
cacheCreationTokens?: number;
estimatedCost: number;
requestCount: number;
};
providers: Array<{
provider: string;
model: string;
inputTokens: number;
outputTokens: number;
cacheReadTokens?: number;
estimatedCost: number;
requestCount: number;
}>;
conversations: Array<{
threadId: string;
provider: string;
model: string;
inputTokens: number;
outputTokens: number;
estimatedCost: number;
requestCount: number;
}>;
}
  • Smaller models for simple tasks — GPT-4o-mini or Gemini Flash for renames, formatting, trivial edits.
  • Local models — Ollama for tasks that don’t need frontier intelligence.
  • Project rules — clear rules reduce iteration.
  • codebuddy.agent.costThreshold / dailySpendCap — per-conversation and daily USD safety nets.
  • Anthropic prompt caching — already active; hit rate ~99% on turn 2+. Long conversations become cheap.
  • Start fresh conversations for unrelated tasks — old context doesn’t help and costs tokens.
  • Providers — provider selection + failover chain
  • Caching §1 — Anthropic prompt caching detail
  • Telemetry — token counts persisted as OTel span attributes