Skip to main content

Usage tracking API

The usage tracking system records per-event token consumption and tool execution metrics. You can query aggregated data by agent, model, or time period.
The usage tracker records events to a PostgreSQL database whenever an AI completion or tool call is made. The endpoints below are read-only query APIs for retrieving aggregated usage data. When no real usage data exists yet, the cost dashboard returns sample data with a isMockData: true flag.

Test usage logging (deprecated)

This endpoint was removed in the March 2026 security audit and is no longer available. Requests to this path return 404. Use the cost dashboard endpoint to verify that usage tracking is working.
A debug endpoint that fires a single test event through the usage logging pipeline.

Cost dashboard

Returns aggregated cost, token, and call data for the cost dashboard. Breaks down spending by agent, model, and day. Requires session authentication. When no usage data is available, the endpoint returns realistic sample data with isMockData set to true and a message field explaining that usage tracking is not yet configured.

Query parameters

Response

Response fields

Errors


The remaining endpoints on this page are planned for a future release. They are not yet available. Usage events are recorded internally but these query APIs have not been deployed. This section documents the intended specification for reference.
All query endpoints below require bearer token authentication and are served by the backend API.

Usage summary

Returns daily aggregated token usage and cost data across all agents and models.

Query parameters

Response

Response fields

Usage by agent

Returns token usage grouped by model for a specific agent.

Path parameters

Query parameters

Response

Response fields

Usage by model

Returns token usage aggregated by model across all agents.

Query parameters

Response

Response fields

Daily totals

Returns total token usage and cost per day.

Query parameters

Response

Response fields

Tool stats

Returns aggregated tool execution statistics, optionally filtered by agent.

Query parameters

Response

Response fields

Usage event schema

Each usage event is stored as a row in the usage_logs table (Prisma model name: UsageLog). Events are recorded automatically whenever an AI completion is made (for example, from the demo chat endpoint or agent chat). Cost is calculated at write time using per-model pricing rates.
The usage_logs table is created via a dedicated SQL migration (20260323000000_add_usage_logs) rather than through Prisma’s schema push. This ensures the table is created reliably across deployments without requiring Prisma to manage the migration state.

Server-side logging

Every usage event emits a structured log line to the server console when it is recorded. This makes it possible to verify that the usage tracking pipeline is working without querying the database. Success log format:
For example:
If the database write fails, an error is logged instead:
Because usage recording is fire-and-forget (it never blocks the API response), these log lines are the primary way to diagnose write failures. Monitor your server logs for [UsageLogger] Failed to log usage entries if usage data appears to be missing from the cost dashboard.

Cost dashboard logging

The cost dashboard endpoint emits its own diagnostic log lines prefixed with [Cost API]. These help you verify that the aggregation query is running and returning data. Query result log:
For example:
If the Prisma query fails, the endpoint logs the error and falls back to sample data:
If the entire handler fails, a general error is logged and a 500 response is returned:
When accessing the table through the Prisma ORM, use the camelCase field names (userId, agentId, inputTokens, etc.). When writing raw SQL queries, use the snake_case column names shown above (user_id, agent_id, input_tokens, etc.).
The table is indexed on user_id, agent_id, created_at, and the composite pairs (user_id, created_at) and (agent_id, created_at) for efficient time-range queries.
The usage event schema was updated in the March 2026 release. The previous fields session_id, session_key, cache_read_tokens, cache_write_tokens, and total_tokens are no longer recorded. Use input_tokens + output_tokens to calculate total tokens.

Tool event schema

Each tool event records:

Errors

All usage tracking endpoints return the following error codes: