Track token consumption, report costs, and send usage telemetry to a central API. The scorpiox-usage binary posts per-turn input/output token counts, model info, and session metadata via HTTP.
scorpiox-usage is a standalone binary that sends token usage
data to a remote API endpoint after each AI provider response. It runs as
a fire-and-forget process — the main agent spawns it in the background so
usage reporting never blocks the conversation loop.
When USAGE_TRACKING=1 is set in scorpiox-env.txt,
the agent automatically invokes scorpiox-usage after every API
response with the input/output token counts, model name, provider, session ID,
project, and git branch. The binary constructs a JSON payload and POSTs it to
the configured USAGE_API_URL.
Dependencies: libcurl, libsxhttp, libsxutil.
Available on all platforms: linux-x64, linux-arm64, macos-arm64, windows-x64.
The agent (sx_agent.c) receives a response from the AI provider with token counts and stop reason.
If USAGE_TRACKING=1 in config, the agent spawns scorpiox-usage as a background process.
The binary assembles a JSON object with session, provider, model, input/output tokens, tier, project, and branch.
Uses libcurl to POST the JSON payload to USAGE_API_URL. Logs success or failure to stderr.
Process exits with code 0 on success. Non-zero exit codes indicate payload or network errors.
| Flag | Short | Description |
|---|---|---|
--session |
-s |
Session ID (required) |
--provider |
-p |
Provider: claude, gemini, openai (required) |
--model |
-m |
Model name (sonnet-4, opus-4, etc.) |
--input |
-i |
Input tokens |
--output |
-o |
Output tokens |
--tier |
-t |
Service tier (standard, priority) |
--project |
— | Project name (default: basename of cwd) |
--branch |
— | Git branch (default: auto-detect) |
# Typical call from the agent after an API response
scorpiox-usage --session abc123-def456 --provider claude --model sonnet-4 --input 12450 --output 3820 --tier standard --project my-app --branch main
Set these keys in scorpiox-env.txt (project-level or global).
The agent reads them at session start and passes relevant values when spawning
scorpiox-usage.
| Key | Default | Description |
|---|---|---|
USAGE_TRACKING |
0 |
Enable usage/telemetry tracking (0 or 1) |
USAGE_API_URL |
(empty) | API endpoint for usage tracking data |
EMIT_SESSION_TRACKING |
0 |
Enable session event emission (0 or 1) |
EMIT_SESSION_API_URL |
(empty) | API endpoint for session event data |
CONTEXT_WARN_THRESHOLD |
80 |
Percentage threshold to warn about context usage |
# Enable usage tracking
USAGE_TRACKING=1
USAGE_API_URL=https://telemetry.example.com/v1/usage
# Optional: also emit session events
EMIT_SESSION_TRACKING=1
EMIT_SESSION_API_URL=https://telemetry.example.com/v1/sessions
# Warn when context hits 80%
CONTEXT_WARN_THRESHOLD=80
The JSON payload POSTed to USAGE_API_URL contains these fields.
Content-Type is application/json.
{
"session": "abc123-def456",
"provider": "claude",
"model": "sonnet-4",
"input_tokens": 12450,
"output_tokens": 3820,
"tier": "standard",
"project": "my-app",
"branch": "main"
}scorpiox-usage -s test-session-001 -p claude -m opus-4 -i 50000 -o 15000 -t priority# Project and branch auto-detect from cwd and git
scorpiox-usage --session sess-77f2a --provider gemini --model gemini-2.5-pro --input 8200 --output 2100scorpiox-usage -s codex-run-42 -p openai -m codex-mini -i 6000 -o 1200 -t standard --project backend-api --branch feature/auth
The api_response hook fires after every AI provider response.
You can use it to build custom usage dashboards, trigger alerts on high
token consumption, or forward data to third-party analytics.
#!/bin/bash
# $4 contains JSON with in_tokens, out_tokens, stop_reason
IN=$( echo $4 | jq -r .in_tokens )
OUT=$( echo $4 | jq -r .out_tokens )
echo "$(date -Iseconds) in=$IN out=$OUT" >> /tmp/token-usage.log
The hook receives the session ID as $1, turn number as $2,
content count as $3, and a JSON blob as $4 containing
in_tokens, out_tokens, and stop_reason.
All errors are logged to stderr. The binary exits with code 1 on any failure. These are the error messages you may encounter:
-s or --session.claude, gemini, or openai.USAGE_API_URL is reachable and the endpoint is up.