Cost tracking is the C (Costs) pillar of the E-O-C-S observability framework implemented in Mesa de Ayuda IA. Every time a user submits a question,Documentation Index
Fetch the complete documentation index at: https://mintlify.com/geremyjampiersalasgarcia-eng/Caso_Practico_Semillero_IA/llms.txt
Use this file to discover all available pages before exploring further.
ChatService extracts usage_metadata.input_tokens and usage_metadata.output_tokens from each Gemini API response, calculates the corresponding USD cost, and persists a row in the audit_logs table in PostgreSQL alongside the detected intent category, agent name, latency, and sources used. This endpoint aggregates those rows over a configurable time window and returns both a total figure and a per-intent breakdown, giving sales managers a clear picture of which query types drive the most API spend.
Method and Path
Query Parameters
Number of days to look back from the current UTC timestamp. For example,
days=30 returns the aggregated cost for the last 30 days. Only rows in audit_logs where created_at >= now() - interval '<days> days' are included.Response
The query window that was used (mirrors the
days parameter sent in the request).The sum of all
cost_usd values in audit_logs within the requested period. Returns 0.0 when no records are found.An array of cost breakdowns grouped by
intent_category. Each entry contains:Example
How Costs Are Calculated
Every Gemini API call returns ausage_metadata object that includes input_tokens and output_tokens counts for that specific call. The ChatService in app/services/chat_service.py reads these values after each agent invocation and computes a cost_usd figure based on the Gemini model’s pricing rates. The resulting cost — along with the intent category, the list of agents used, response latency, and the sources retrieved — is written to a new row in the audit_logs table via the AuditLog SQLAlchemy model.
The metrics endpoint then queries audit_logs using SQLAlchemy’s aggregation functions (func.sum, func.count) grouped by intent_category, filtering to rows where created_at >= cutoff_date:
Cost records are only written when PostgreSQL is available. If the backend started with SQLite as the fallback database (because the PostgreSQL Docker container was not running),
audit_logs rows may be missing and this endpoint will return total_cost_usd: 0.0. Start PostgreSQL with docker-compose up -d postgres before running the backend to ensure full cost tracking is active.