Token counting and cost tracking for LLM operations
Provides utilities to estimate token usage and associated costs for different LLM providers.
Types
CostTracker = ref object totalInputTokens*: int totalOutputTokens*: int totalCost*: float costByModel*: TableRef[string, float] tokensByModel*: TableRef[string, tuple[input: int, output: int]]
- Tracks cumulative costs for LLM operations
TokenCounter = object
- Counts tokens for different model families
Vars
globalCostTracker = newCostTracker()
Procs
proc estimateTokens(text: string): int {....raises: [], tags: [], forbids: [].}
- Estimates token count for text (rough approximation) Uses ~4 characters per token as a rule of thumb
proc getSummary(tracker: CostTracker): JsonNode {....raises: [KeyError], tags: [], forbids: [].}
- Returns a JSON summary of costs and usage
proc newCostTracker(): CostTracker {....raises: [], tags: [], forbids: [].}
- Creates a new cost tracker
proc reset(tracker: CostTracker) {....raises: [], tags: [], forbids: [].}
- Resets all tracked costs and tokens
proc trackUsage(tracker: CostTracker; model: string; inputTokens: int; outputTokens: int) {....raises: [KeyError], tags: [], forbids: [].}
- Records token usage and calculates cost