Audience: Engineering leaders, AI platform teams, enterprises managing multi-team LLM spend
The irony
The most common way teams track their LLM spend is by asking an LLM-powered agent to check their LLM bill. That agent burns tokens to decide whether to check, how to format the result, and what counts as an alert. AINL eliminates that loop.
The compiled solution
examples/autonomous_ops/token_cost_tracker.lang runs hourly, pulls usage from the OpenRouter API, calculates rolling 7-day spend from the memory adapter, and fires structured alerts when daily or weekly thresholds are crossed — all without a single orchestration LLM call.
# token_cost_tracker.lang (core pattern)
S http cron
Cr L_tick "0 * * * *"
include "modules/common/token_cost_memory.ainl" as tokenmem
L_tick:
R http GET "https://openrouter.ai/api/v1/usage" headers ->resp
X day_cost (core.sum (map todays_usage (lambda u (u.cost or 0))))
# Persist today's summary (7-day TTL rolling window)
Set memory_kind "workflow.token_cost_state"
Set memory_payload { "date": today_date, "cost_usd": day_cost, ... }
Call tokenmem/WRITE ->_
X pct_used (core.div week_cost weekly_budget_usd)
If (core.gt day_cost daily_limit_usd) ->L_daily_alert ->L_check_weekly
L_daily_alert:
R queue Put "notify" { "module": "token_cost_tracker", "status": "alert", ... }
Ret "alerted"
The openclaw/bridge/wrappers/token_budget_alert.ainl and zeroclaw/bridge/wrappers/token_budget_alert.ainl wrappers provide the same pattern pre-wired for each host.
Enterprise-grade properties
- Rolling 7-day window via the memory adapter with TTL management — no separate database table to maintain
- Configurable thresholds at runtime (daily cap, weekly budget, alert percentage) — no redeploy needed to tune
- Structured alert envelope — consistent JSON shape for PagerDuty, Slack, or any downstream consumer
- Full execution tape — JSONL trace of every hourly run, queryable for audits or cost forecasting
- MCP-accessible — AI agents in your org can query cost state via
ainl-mcpwithout re-running the full workflow
Extend it
Add per-model breakdowns, per-team allocation tracking, Slack digest threads, or anomaly detection nodes — each as an additional graph node, compiled and validated before deployment.
pip install ainativelang
git clone https://github.com/sbhooley/ainativelang.git
ainl check examples/autonomous_ops/token_cost_tracker.lang --strict
ainl visualize examples/autonomous_ops/token_cost_tracker.lang --output cost_tracker.mmd
See also: How AINL saves money on monitoring · Built with AINL: 7.2× cheaper monitoring
