agentleFS
MLOps-CoursesMLOps-Courses/agentops-open-course/skills/agent-token-budget/SKILL.md

agent-token-budget

Skill2 starsChanged 4 months agoMITView on GitHub

Bound an LLM agent's per-session token spend and attribute cost, so a runaway loop ends with an actionable message instead of an open-ended bill. Use when an agent's multi-step loop can spend without limit, when you need per-session token accounting, or when a delegation chain could multiply model calls.

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 1 is distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Every agent loop step is another model call, so cost and latency compound.

    line 8
  2. Very specific:

    Accumulate usage into session state: After each model response, add its input/output tokens to a running per-session total that persists …

    line 18

≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Agent Token Budget

Every agent loop step is another model call, so cost and latency compound. Give each conversation a hard token ceiling and attribute usage to the session — bounding reasoning work, not just dollars.

When to use

  • An agentic loop or delegation chain can call the model an unbounded number of times.
  • You need to answer "how many tokens did this session use?" and "what happens at the limit?".
  • You want token/cost signals in traces and metrics without hardcoding a vendor's prices.

Steps

  1. Accumulate usage into session state. After each model response, add its input/output tokens to a running per-session total that persists across turns (no temp: prefix if your session store honors one).
  2. Enforce a hard ceiling before the model call. In a before-model hook, if the session total has reached the limit, short-circuit with a clear message ("start a new session, or raise the limit") instead of a silent failure or an open-ended bill.
  3. Attribute, don't just count. Emit tokens as an OTel counter (graph throughput) and as span attributes (per-turn detail); compute cost from configurable per-1k prices that default to 0 for local models — never hardcode a provider's pricing.
  4. Make the budget cover the whole conversation. In multi-agent/delegation flows, keep the totals in shared session state so hops between sub-agents accumulate against one ceiling rather than each starting fresh.

Reference implementation

From the AgentOps Open Course (agent modules live under agents/python/src/agent/):

  • budget.py — record_token_usage, enforce_token_budget, estimate_cost; OTel counter + span attributes.
  • Course chapters 7.3. Costs and 3.7. Multi-Agent.

Verify

Set the ceiling to 1 token and send a turn: assert the model call is refused with the actionable message and the token counter reflects the usage; confirm a delegation chain hits one shared ceiling, not one per sub-agent.

Honest limits

A per-session budget bounds one conversation, not one client — a caller who rotates sessions bypasses it, so pair it with a per-client/tenant limit at the gateway. It bounds tokens, not dollars, unless you supply real prices.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.