{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreihx5f54f5ojdctdpys52crudvyywxz4ulmjurjz5i3kbweqobgbnm",
"uri": "at://did:plc:25rdn5elo5izoxrmtis34zuk/app.bsky.feed.post/3mpjem3uxpfs2"
},
"coverImage": {
"$type": "blob",
"ref": {
"$link": "bafkreigb7ewbhw5ejnj2povlon7az4r6obhvfjrd6arxhhevr5tyxbmqiu"
},
"mimeType": "image/webp",
"size": 78778
},
"path": "/arpa/how-llms-now-monitor-and-cut-their-own-token-spend-ibg",
"publishedAt": "2026-06-30T15:29:53.000Z",
"site": "https://dev.to",
"tags": [
"python",
"ai",
"agentskills",
"skillware",
"monitoring/token_limiter",
"docs/skills/token_limiter.md",
"Skillware Website",
"Skillware on GitHub",
"monitoring/token_limiter source",
"v0.4.0 release notes",
"Skill library",
"Agent loops guide"
],
"textContent": "You have seen this loop before.\n\nAn agent starts a “simple” task, say scrape listings, refactor a repo, research a market, or whatever. It fails, it retries, it re-reads context, it apologizes and tries all over again. Twenty minutes in and the dashboard shows six figures of tokens and zero useful outputs or deliverables.\n\nThe model did not misbehave on purpose. The **orchestrator** never had a hard budget gate with an ROI in mind.\n\nSkillware v0.4.0 ships a new skill for exactly that gap: monitoring/token_limiter. It lets you **monitor and limit any agent’s token budget in real time** — Gemini, Claude, OpenAI, DeepSeek, Ollama, custom Python loops, you name it. Same skill, same JSON, any runtime.\n\n## What Skillware is in a nutshell\n\nSkillware is an open registry of **installable agent capabilities**. Each skill is a bundle:\n\n * **`skill.py`** — deterministic Python (`execute()` returns JSON)\n * **`instructions.md`** — when the model should call the tool\n * **`manifest.yaml`** — schema, constitution, issuer\n * **Tests and docs** — shipped in the wheel\n\n\n\nYou load by ID, adapt for your provider, call `execute()` on tool use. The model decides _when_ , the skill decides _how_ , predictably, every time.\n\nThat split matters for budget control. You do not want the LLM guessing whether it is “allowed” to spend more tokens. You want a **small, auditable function** that answers: continue, warn, or stop.\n\n## Meet the `Token Limiter`\n\nThis skill is a **budget gate** , not a kill switch wired into OpenAI or Anthropic.\n\nAfter each model turn, your host loop passes cumulative usage. The skill returns one of three actions:\n\nAction | Meaning\n---|---\n`CONTINUE` | Under the soft threshold — keep going\n`WARN` | Approaching the limit (default 80%) — tighten scope\n`FORCE_TERMINATE` | Hard ceiling hit — **stop the loop**\n\nImportant nuance: the skill **does not** cancel API sessions or kill processes. It returns a structured decision. **Your orchestrator must act on it.** That is by design — Skillware skills stay portable and provider-neutral.\n\nNo skill-specific API keys. No network calls. Pure Python math on numbers you supply.\n\n## How it works in a real loop\n\nPicture a scrape task with a **100,000 token** ceiling.\n\n 1. Agent runs turn 1 → host adds usage → calls `token_limiter`\n 2. Turn 2, turn 3 — same pattern\n 3. At 85k tokens → `WARN`\n 4. At 105k → `FORCE_TERMINATE` → host breaks the loop and surfaces the reason\n\n\n\nMinimal integration:\n\n\n\n from skillware.core.loader import SkillLoader\n\n bundle = SkillLoader.load_skill(\"monitoring/token_limiter\")\n skill = bundle[\"module\"].TokenLimiterSkill()\n\n result = skill.execute({\n \"task_id\": \"scrape_listings_101\",\n \"current_token_count\": 125_000,\n \"max_allowed_tokens\": 100_000,\n \"model_id\": \"gpt-4o\",\n })\n\n if result[\"action\"] == \"FORCE_TERMINATE\":\n raise RuntimeError(result[\"reason\"])\n\n\nThe host tracks **cumulative** `current_token_count` from whatever provider you use — usage metadata from the API, a local tokenizer, or your own accounting layer. The skill does not read billing dashboards for you.\n\nOptional `model_id` maps to bundled list prices for **indicative USD** in the response. Handy for ops dashboards; not invoice-grade. Unknown models fall back to a blended rate with a warning in the payload.\n\nOptional `turn_id` makes retries idempotent: same turn, same counts, same decision — no double-penalty if your loop replays a step.\n\n## Architecture: Mind, Body, and a new category\n\nThe skill lives under a new **`monitoring/`** category — room for more observability skills later.\n\n * **`budget.py`** — pure evaluation logic (thresholds, cost estimate, ROI scaffold for v2)\n * **`skill.py`** — thin `BaseSkill` wrapper, in-memory turn cache\n * **`instructions.md`** — tells the agent: call this every turn; stop when you see `FORCE_TERMINATE`\n * **`data/model_pricing.json`** — indicative rates for common models\n\n\n\nv1 enforces **token limits only**. ROI fields (`expected_outcome`, `outcome_delivered`, `roi_value_usd`) are accepted as **scaffold for v2** — outcome-aware gates later, without breaking the v1 contract today.\n\nRunnable examples ship in the repo: local loop simulation (`token_limiter_loop.py`), plus Gemini and Claude harnesses. Install and try:\n\n\n\n pip install skillware\n\n\nCatalog page: docs/skills/token_limiter.md\n\n## Chain it with other skills\n\nBudget control pairs naturally with **`optimization/prompt_rewriter`** — compress bloated context _before_ the main call, then cap spend _during_ the loop. Less waste in, hard ceiling out.\n\nRunning agents against contracts or wallets? Screen first with **`finance/wallet_screening`** , execute with **`defi/evm_tx_handler`** , and keep **`token_limiter`** in the outer loop so a stuck DeFi agent cannot burn budget forever. Three skills, one NLP-driven pipeline, any supported model.\n\n## Conclusion\n\nAutonomous agents without token guardrails are expensive experiments. **`monitoring/token_limiter`** gives you a deterministic, testable answer to a simple question after every turn: _are we still within budget?_\n\nIt ships in **Skillware v0.4.0** today. Load it once, wire it into your loop, and stop paying for agents that retry themselves into oblivion.\n\n**Links**\n\n * Skillware Website\n * Skillware on GitHub\n * monitoring/token_limiter source\n * v0.4.0 release notes\n * Skill library\n * Agent loops guide\n\n\n\nQuestions, issues, or skill ideas welcome in the repo. If you are building agent infra, start with a budget gate — your finance team will thank you later.",
"title": "How LLMs Now Monitor and Cut Their Own Token Spend"
}