Get release news

Spend caps for priced tools

A budget cap the agent can talk its way around is not a cap. Constle meters priced MCP tool calls that pass through the gate, outside the agent, and stops the run when a cap is crossed. Read the scope first: it is narrower than "everything the agent spends".

On this page

Note

Release status

The current source and its signed releases aren't public yet. More in Project status.

Warning

What is metered, and what is not

Constle meters tool calls that pass through the gate to MCP servers that declare a pricing block, and nothing else. Calls the agent makes directly to a model API over allowed_hosts are not metered: they go to hosts you allow, not through the gate. Traffic to hosts you allow isn't gated. If an allowed host shares a server with an MCP server, a request can reach that MCP server past the gate. Such a request is not metered either. This is limitation 2. A cap declared without a priced MCP server measures nothing, and constle validate says so.

What you'll getLink to this section

  • A per-run cap: among calls that pass through the gate, the one that crosses it is the last one through, and the run stops with a spending_limit_reached event.
  • A per-day cap on calls that pass through the gate, which needs an identity, holds across runs, is keyed by the agent's identity so a rename cannot reset it, and refuses to start a run once the day's budget is spent.
  • An optional early warning in the audit log, and, with a per-day cap, a daily ledger of each metered charge.

1. Declare what a tool call costsLink to this section

agent.yamlyaml
mcp:
  servers:
    - id: web-search
      url: "https://mcp-search.example.com/mcp"
      tools: ["search"]
      pricing:
        meters:
          - usage_path: "result.usage.input_tokens"
            usd_per_unit: "0.00000300"
          - usage_path: "result.usage.output_tokens"
            usd_per_unit: "0.00001500"

For a priced server, the gate reads the response to each of its tool calls that pass through the gate, and charges it. usage_path is a dot path into the reply, where a number indexes a list. usd_per_unit is an exact decimal with at most 8 decimal places. A response's cost is the sum over all its meters. You declare the prices, and nothing is guessed per provider. Spending is a meter, not a bill: the figure is the usage the server reports times the price you declare, so a server that under-reports lowers it.

Pricing is server-wide. A reply that carries a result but not a declared usage value is a metering failure, and it stops the run: a server that could leave out its usage could zero its own count. To mix free and priced tools from one upstream, declare its URL twice under two ids with disjoint tools lists.

2. Set a per-run capLink to this section

yaml
spending:
  max_per_run_usd: "0.50"

When the run's metered total for calls that pass through the gate exceeds the cap, the gate stops forwarding calls and the run is stopped, recording a spending_limit_reached event naming the cap and then terminated_by_limit. Amounts are exact decimals, like "0.50". Nothing is rounded, and a cap of "0" is refused: leave the field out instead.

3. Add a per-day capLink to this section

yaml
identity:
  name: research-agent
  did: did:key:z6Mk...4doK          # from `constle identity create research-agent`; required for the daily cap

spending:
  max_per_run_usd: "0.50"
  max_per_day_usd: "5.00"
  alerts:
    warn_at_pct_of_daily: 80        # a one-time warning in the audit log, never a block

The DID above is a shortened placeholder: paste the full one that constle identity create prints. With a daily cap, each identity has its own daily ledger under ~/.constle/spending/, locked so concurrent runs add up. It is keyed by the DID because a name can be changed. A run whose day is already spent is refused before the sandbox starts, recorded as spending_limit_reached with action: run_refused. If the ledger can't be read, the run doesn't start. Creating the identity: Give each AI agent a verifiable identity.

max_per_month_usd parses and is never enforced. Declaring it prints ⚠️ warning: declared, not enforced by this runtime: spending.max_per_month_usd.

4. Validate and read the warningsLink to this section

shell
constle validate agent.yaml

For an agent with these caps, the priced web-search server and allowed_hosts: [api.groq.com], validation reminds you of the scope (abridged to that warning):

Output
⚠️  warning: spending limits are enforced ONLY for priced MCP servers (web-search);
   traffic to network.allowed_hosts (api.groq.com)
   is NOT metered and does not count toward the limits.

Without any priced server, it says the caps are declared but NOT enforced. Every case: Spend caps and metering.

5. Run, and check what was chargedLink to this section

shell
constle run agent.yaml
grep spending_limit_reached ~/.constle/logs/research-agent-$(date -u +%F).jsonl

How much can a run overshoot?Link to this section

Metering is after the fact: a response's cost is known only once it has arrived. Among calls that pass through the gate, the one that crosses the cap is still incurred and still counted. Enforcement stops what comes next. A run's priced calls go to the server one at a time, so one run overshoots by at most the one call that crossed. Concurrent runs of one identity can each overshoot the daily cap by one call. Set caps with one expensive call of headroom.

What this does not coverLink to this section

Spending Covered?
Priced MCP tool calls that pass through the gate Yes, per run and per day.
Direct calls to a model API through allowed_hosts No: they go to hosts you allow, not through the gate.
A request that reaches a priced MCP server past the gate, through an allowed host that shares its server No. Traffic to hosts you allow isn't gated. If an allowed host shares a server with an MCP server, a request can reach that MCP server past the gate.
Methods listed under methods on a priced server (resources/read, prompts/get, …) No: forwarded and logged, not metered.
A priced MCP server's result that omits its usage value The run is stopped (fail closed).
Anything per month No: max_per_month_usd is declared only.
Calls answered with a JSON-RPC error, or failed transports Not charged.

For model API spend, use a usage limit on the key with your provider if it offers one, keep the key's scope small, and set limits.max_duration_seconds so a looping agent stops on time. The reason Constle does not meter that traffic is a privacy choice, explained in Spend caps and metering.

FAQLink to this section

Does Constle count LLM tokens?Link to this section

Only when a priced MCP server reports them, on calls that pass through the gate, where the gate reads the usage values you point it at. Tokens the agent spends by calling a model API directly are not counted: that traffic goes to hosts you allow, not through the gate.

Can the agent reset the daily cap?Link to this section

Not from inside the sandbox: the ledger is on the host. It is keyed by the DID, so renaming the agent in the Agentfile does not reset it either.

What does the agent see when a cap trips?Link to this section

Among calls that pass through the gate, the one that crosses the cap is the last one through: any further request to the gate gets a 403 saying the run is being stopped, and the run is then stopped. Later runs of the same identity that day are refused before they start if the daily cap is spent.

Why are prices strings and not numbers?Link to this section

A YAML float cannot represent decimal money exactly, and a rounding error in a spending cap is a security bug. Money is counted in whole units of 10⁻⁸ dollars, so totals never drift.