Skip to main content

Overview

The Tokens API provides comprehensive token usage tracking and cost analysis across all agents, models, and sessions. Track consumption patterns, analyze costs per agent, and export data for billing and optimization.

Authentication

All token endpoints require authentication via:
  • Session Cookie: mc-session (set after login)
  • API Key: x-api-key header
Minimum role requirements:
  • GET: Viewer role
  • POST: Operator role

Query Token Usage

GET /api/tokens

Query token usage with multiple action modes for different use cases.
Authorization: Viewer role required

Query Parameters

string
default:"list"
Query action to perform
string
default:"all"
Time window for data filtering
string
default:"json"
Export format (only for action=export)

Action: List

List Token Usage Records

Retrieve recent token usage records with pagination.

Request

Response Fields

array
Array of token usage records (max 100)
integer
Total number of records matching filters
string
Applied timeframe filter

Example Request

Example Response


Action: Stats

Get Aggregated Statistics

Retrieve aggregated token usage and cost statistics.

Request

Response Fields

object
Overall statistics
object
Statistics broken down by model (key: model name, value: TokenStats)
object
Statistics broken down by session (key: session ID, value: TokenStats)
object
Statistics broken down by agent (key: agent name, value: TokenStats)
string
Applied timeframe filter
integer
Number of records analyzed

Example Request

cURL

Example Response


Action: Agent Costs

Per-Agent Cost Breakdown

Detailed cost analysis per agent with model breakdown and daily timelines.

Request

Response Fields

object
Cost breakdown per agent (key: agent name)
string
Applied timeframe filter
integer
Number of records analyzed

Example Request

cURL

Example Response


Action: Export

Export Usage Data

Export complete token usage data in JSON or CSV format.

Request

Response

Returns file download with appropriate Content-Disposition header. JSON Export:
  • Content-Type: application/json
  • Includes: usage records, summary stats, model stats, session stats
CSV Export:
  • Content-Type: text/csv
  • Columns: timestamp, agentName, model, sessionId, operation, inputTokens, outputTokens, totalTokens, cost, duration

Example Request


Hourly Usage Trends

Get hourly token usage and cost trends for the last 24 hours.

Request

Response Fields

Array of hourly data points
string
Applied timeframe (always includes recent 24h)

Example Response


Record Token Usage

POST /api/tokens

Manually record token usage for a session.
Authorization: Operator role required

Request Body

string
required
Model identifier (e.g., “claude-sonnet-4”)
string
required
Session identifier
integer
required
Number of input tokens
integer
required
Number of output tokens
string
default:"chat_completion"
Operation type
number
Request duration in milliseconds

Response

boolean
Whether recording was successful
object
Created TokenUsageRecord object

Example Request

Example Response


Model Pricing

Supported Models & Pricing

Pricing per 1,000 tokens for supported models.

Pricing Table

Pricing is automatically calculated based on model names. If an exact match isn’t found, Mission Control attempts partial matching (e.g., “claude-sonnet” matches “anthropic/claude-sonnet-4”). Unknown models default to $1.00 per 1K tokens.

Data Sources

Token Data Sources

How Mission Control aggregates token usage data.

Priority Order

  1. Token Usage Database (Primary)
    • Stored in token_usage SQLite table
    • Most authoritative source
    • Includes heartbeat data from agents
  2. JSON File Storage (Secondary)
    • Manually recorded usage via POST endpoint
    • File location: configured in tokensPath
    • Limited to 10,000 most recent records
  3. Gateway Sessions (Fallback)
    • Derived from active OpenClaw sessions
    • Used when no persistent data exists
    • Real-time but less comprehensive

Best Practices

Optimization Tips

Recommendations for token usage monitoring and cost optimization.

Cost Monitoring

  1. Set up regular exports - Export data weekly for billing analysis
  2. Monitor agent-costs - Identify high-cost agents for optimization
  3. Track model usage - Use cheaper models (Haiku) for simple tasks
  4. Review trends - Identify usage spikes and anomalies

Performance

  • Use timeframe parameter to limit data volume
  • Export CSV for large datasets (more efficient than JSON)
  • Cache stats responses for dashboards
  • Use agent-costs for detailed analysis, stats for quick overview

Data Retention

  • JSON file storage limited to 10,000 records
  • Database storage unlimited (manage via SQL if needed)
  • Consider periodic exports for long-term archival