Skip to content

Track AI API costs via centralized query service #67

Description

@mbuckbee

Summary

Route AI API calls through a centralized service that calculates and tracks token costs per request. Instead of each component calling AI providers directly and discarding token counts, funnel calls through a query service that:

  1. Calls the underlying provider (OpenRouter, Gemini, etc.)
  2. Looks up the model in a cost table
  3. Calculates cost from input/output token counts and model pricing
  4. Returns the response with cost_in_dollars attached

Motivation

Currently AI API calls happen directly with no cost visibility. This makes it impossible to understand per-video generation costs or identify expensive operations.

Approach

  • Add a general-purpose query method to a central AI service that wraps any provider
  • Track input_tokens, output_tokens, cost_in_dollars, and model_slug on the relevant record
  • Accumulate costs across multiple AI calls within a single operation
  • Persist totals after successful completion

Reference

Implemented in Knowatoa's AiQueryService.query + TaskExecutable concern as a working pattern.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions