Skip to main content

Module spend_watch

Module spend_watch 

Source
Expand description

What srv does as an agent’s spend on a model route reaches its caps (docs/specs/SPEC_ROUTE_AGENT_BRAKES_2026_11_10.md section 3.3). The gateway counts each response and refuses a request once a cap is used (model_gateway::cap); it tells this watcher of both, and the watcher, off the stream’s task:

  • 50% of the agent’s cap: one non-urgent note to its leader.
  • 80%: the agent’s pane gets a warning line (block meta agent:spend_warning).
  • 100% (or the route’s cap): the pane carries agent:spend_stopped, its turn in flight is interrupted, and the leader is told. The gateway has refused (or will refuse) the agent’s next request.

Each level is acted on once per agent per UTC day; the level already sent is kept with the day’s spend, so a restart does not send it again. A raised or reset cap, or the first response of a new day, takes the stop off.

Structs§

CapChange 🔒
The cap before and after a change.

Enums§

CapWrite 🔒
Event 🔒

Constants§

DAY_MS 🔒
REFRESH_EVERY 🔒
How often every active agent’s figure is written again, so a skipped one and a new pane catch up.
STOPPED_META_KEY
Block meta the pane’s stop card reads; absent when the agent is not stopped.
TODAY_EVERY_MS 🔒
The least time between two writes of TODAY_META_KEY for an agent, unless its level or cap changed.
TODAY_META_KEY
Block meta the chips read: what the agent has used today and its cap (day is days since the epoch, UTC).
WARNING_META_KEY
Block meta the pane’s warning line reads, from 80% of the cap.

Functions§

card 🔒
change_cap 🔒
Sets the agent’s daily cap (None: no cap). The alert level goes back to where the day now stands, and a stop the new cap no longer reaches is taken off the agent’s panes. The caller has checked who may do this and records it.
clear 🔒
Takes key off every open pane of the agent that carries it.
clear_cap 🔒
Takes the agent’s own cap away, so it has the default again, and does what change_cap does for the cap that gives.
drop_stale 🔒
forget_alert_off_route 🔒
An agent off a model route has had its stop and warning taken off its panes, so the alert level the gateway’s refusals check goes back to zero with them. Left at 100, an agent stopped at its cap that returns to a route the same UTC day is refused on its first request and gets no stop card, because the stop was already counted as sent.
handle 🔒
inbox 🔒
name_of 🔒
on_a_key 🔒
A Claude agent linked to an Anthropic key: the gateway carries its requests. An agent of another provider that holds such a link sends its own requests to the vendor, where nothing is counted or capped.
on_a_route 🔒
A cap, and a day of spend, belong to an agent on a model route or an API key: a login agent has plan limits instead.
on_settled 🔒
place_of 🔒
“OpenRouter (DeepSeek V4.1 Flash)”: where the agent’s spend goes.
publish_today 🔒
Writes the figure the chips show to the agent’s open panes: at most every TODAY_EVERY_MS unless force.
refresh_chips 🔒
Brings every agent’s panes up to date once a minute: the figure of an agent on a route that has spent today, and the removal of what no longer holds (see stale_keys).
refused 🔒
The gateway refused grant’s agent a request because hit is used.
reset_today 🔒
Zeroes the agent’s spend for today and takes its stop and warning off. Returns the dollars that were counted.
set_meta 🔒
settled 🔒
A response of grant’s agent has been counted.
spends_through_gateway 🔒
Whether the agent’s requests go through the gateway and are counted there: it is on a model route, or on an API key.
stale_keys 🔒
The spend keys on a pane’s meta that no longer hold. An agent that is not on a model route has no cap and no day of spend to show, so it carries none of them; on a route, a stop or warning written on an earlier UTC day belongs to a day the gateway has since reset, and nothing else would take it off an agent that stays idle through midnight.
start 🔒
Starts the watcher.
stop 🔒
view 🔒
What Agents list shows for an agent on a model route: what it has used today and its cap. For an agent on the DeepSeek Harness, which uses its own key and reports no spend, that nothing is measured. None for any other.
warn 🔒
write_cap 🔒