Skip to main content

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.

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, 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

GET /api/v1/metrics/costs

Query Parameters

days
integer
default:"7"
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

period_days
integer
The query window that was used (mirrors the days parameter sent in the request).
total_cost_usd
float
The sum of all cost_usd values in audit_logs within the requested period. Returns 0.0 when no records are found.
by_intent
object[]
An array of cost breakdowns grouped by intent_category. Each entry contains:

Example

curl "http://localhost:8000/api/v1/metrics/costs"

How Costs Are Calculated

Every Gemini API call returns a usage_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:
# From app/api/v1/endpoints/metrics.py
cutoff_date = datetime.datetime.utcnow() - datetime.timedelta(days=days)

total_cost = db.query(func.sum(AuditLog.cost_usd)).filter(
    AuditLog.created_at >= cutoff_date
).scalar() or 0.0

cost_by_intent = db.query(
    AuditLog.intent_category,
    func.sum(AuditLog.cost_usd).label("total_cost"),
    func.count(AuditLog.id).label("total_requests")
).filter(
    AuditLog.created_at >= cutoff_date
).group_by(
    AuditLog.intent_category
).all()
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.

Build docs developers (and LLMs) love