AI agent cost tracking won't stop a payment that fits the budget
AI agent cost tracking shows what each agent and run spent. Rules on the tool call stop a second payment for one invoice, or the one that takes a session over its limit.
AI agent cost tracking shows what each agent and run spent. Rules on the tool call stop a second payment for one invoice, or the one that takes a session over its limit.
"Right now I just have a hard spending cap, but that won't catch a malicious-but-under-budget payment." That line opens a thread in r/AI_Agents about agents that pay for APIs and make purchases, and most of the replies agree with it. Paying one invoice twice can stay under any cap, and so can a refund to the wrong order. AI agent cost tracking has the same gap: it reports what each agent spent after the money is gone. Amiril's rules check each payment before the tool runs, against every earlier payment in the session.
The model bill covers the tokens behind each reply. The agent in this post also pays supplier invoices through a tool, and that money never shows up on the model bill.
Plenty of teams watch neither. Komodor's 2026 Agentic Operations Survey asked 1,000 engineering and IT leaders, and one in four, in the report's words, "has no visibility into, budget for, or cap on what their agents cost". In the same survey 68.4% spend $10,000 or more a month on AI agents. Its advice to practitioners includes tagging cost by agent and by run while the fleet is small, and setting caps at the platform layer instead of in each agent's code.
Most AI agent cost tracking tools only see the model bill. Helicone works out the cost of each model request it logs, and its custom rate limits can hold each user to a number of cents of that cost. A policy of 500;w=3600;u=cents;s=user holds each user to $5 of model calls an hour and answers the next request with a 429. A $400 refund sent to the wrong order costs a few cents of tokens, so it stays far under that limit.
One reply in the thread walks through how an under-budget payment goes wrong without any attacker. The payment call times out, the agent retries, and the provider gets the same payment twice. The fix offered there is an idempotency key, created and stored before the first attempt. That covers a retry with the same key. If the model comes back ten minutes later in a new run and decides the invoice is still unpaid, it makes a new key and pays again.
This Amiril rule blocks a second payment for one invoice from any run in the same session:
@id("one-payment-per-invoice")
@per("invoice_id")
@sentence("This invoice was already paid earlier in the session")
block (
agent == "payments",
tool == "issue_payment"
)
when {
issue_payment{}
};@per("invoice_id") narrows the pattern issue_payment{} to earlier payments carrying the same invoice_id as the call being checked, and it searches every run of the session. The agent further down passes the conversation id as its session id. So the first payment for INV-1041 runs, a second one is refused before issue_payment is called, and a payment for INV-1042 goes ahead.
The SDK keeps a copy of your published rules inside the agent's process and checks each call against it there. Publish a change and running agents have it within about a minute, with no redeploy. If Amiril can't be reached they keep enforcing the last rules they received. Set AMIRIL_DURABLE_PATH and the session's earlier payments are kept on disk as well, so a restart doesn't forget them.
A per-payment ceiling of 1,000 lets three payments of 900 through in one conversation, 2,700 in all. The rule below refuses the third. sum adds the amounts of earlier payments to the current one. A rule without @per reads only the current run, so @scope("session") widens it to every run in the session:
@id("session-payment-total")
@scope("session")
@sentence("This payment brings the session total to {session_total}, over 2000")
block (
agent == "payments",
tool == "issue_payment"
)
unless {
session_total = sum(issue_payment{}.args.amount, call.args.amount);
session_total <= 2000
};Here are both rules on a Python agent with no framework. The name passed to plain.Agent is the one the rules' heads use. Calling run.turn() before each payment puts every earlier payment, including those from the same reply, into the session total.
import amiril
from amiril import plain
import payables
amiril.init()
agent = plain.Agent("payments")
@agent.tool
def issue_payment(payee: str, invoice_id: str, amount: float) -> dict:
"""Pay a supplier invoice."""
return payables.pay(payee, invoice_id, amount)
def handle(conversation_id: str, message: str) -> None:
with agent.run(session_id=conversation_id) as run:
for tool_calls in model_replies(message): # your model call goes here
for call in tool_calls:
run.turn()
result = issue_payment(**call["args"])
send_to_model(call["id"], result)In one conversation the model pays INV-1041 for 400 and asks to pay it again, which the invoice rule refuses. It pays INV-1042 for 900. Then it asks for 800 against INV-1043, and gets this back in place of a result:
The call to issue_payment was not performed. Rule session-payment-total blocked it: This payment brings the session total to 2100, over 2000 (amount 800). Do not retry this call with different arguments or achieve the same outcome another way; tell the user it was not performed.
The refused 400 never counts toward the total, since a pattern only matches calls that succeeded. A timed-out payment fails the same way, which leaves its retry free to go through on the idempotency key, and once that retry succeeds a later run's payment for the invoice is refused. payables is your own code, and the SDK guide for agents without a framework shows how to connect it to your workspace.
The hard part is the 2000, because set too low it stops the agent paying ordinary invoices on its first busy day. A new rule starts in Shadow, where it checks every call, records what it would have done and lets the call run. Test on past calls replays any version of the rule over the last 24 hours, 7 days or 30 days of the agent's real tool calls and lists each call it would have fired on, so you see which sessions 2000 would have cut short before it blocks anything.
Amiril's AI agent cost tracking lives on the Agents page and on each run's trace. Each agent's card shows its cost over a window such as the last 7 days, beside the share of its runs that met their success condition. A run's trace page has its tokens and cost in the header. Runaway agent costs from a loop show up as one run whose trace holds most of the bill. A rule that marks every call after a run's 40th flags that loop while it's still going. The rule language guide has that call budget beside the invoice and session rules.
Amiril also reads the payments themselves. It learns what's normal for each agent from that agent's own history, and a run that breaks the pattern gets a rarity factor such as "issue_payment was called more than once, which happened in 3 of 640 other runs calling it." Runs that share a problem are grouped into an issue. If a rule could have stopped them, the issue carries a rule suggestion with the rule drafted and a line like "Would have blocked 4 of 5 runs". Clicking Create rule on the rule suggestion puts that draft in the rule editor, starting in Shadow like any new rule.
A refund of 900 against an order that cost 90 is the same kind of under-budget payment, and AI agent guardrails has a rule that checks it against the lookup_order total from earlier in the run. To run both rules on your own agent, start free.