Skip to content
FluxMeter
中文

Writing

A runaway agent against a $0.30 budget: a reproducible local test

Local mock test: a runaway agent on a $0.30 budget, with and without FluxMeter. 9 calls, then HTTP 402. Not a billing suite; no zero-overspend guarantee.

2026-11-24 · Budgets · 10 min read

Every number in this post comes from a local mock on one machine. No real model provider was called and none of it is production data. The point is a test you can rerun yourself, not a benchmark.

Short answer. In a local mock test with a $0.30 budget and $0.03 per call, an agent loop with no budget control made 814–815 calls (about $24.4) in a 3-second window and was still running when the timer stopped it. Through the FluxMeter Gateway, the same budget admitted 9 calls ($0.27), and the 10th call got HTTP 402 `budget_exceeded` 0.15–0.21 seconds after the loop started, with $0.00 of debt in the ledger. This is not a zero-overspend guarantee: the stop is only as accurate as the per-call cost estimate, and one of our own runs went over.

FluxMeter is open-source, self-hosted LLM token metering with customer attribution and an optional budget check/reserve step. It is not a billing suite and does not replace one.

The setup

Cost per call. The mock upstream answers every request with 4,000 prompt tokens and 2,000 completion tokens on `gpt-4o`. At the catalog rates in the repo ($2.50 per million input tokens, $10 per million output tokens) that is $0.03 per call.

Budget. One customer with a $0.30 balance, so exact arithmetic allows ten calls.

Run A, no FluxMeter. 12 parallel workers call the mock directly for a fixed 3 seconds.

Run B, FluxMeter Gateway. One serial loop sends the same request through the Gateway. Before forwarding each call, the Gateway places a $0.03 hold on the customer's budget. When the balance minus holds cannot cover the next hold, it returns 402 instead of calling upstream.

Two things to read carefully:

1. The runs are not symmetric. A is parallel and capped at 3 seconds; B is serial. A's dollar figure depends on the cap and on the machine. What the test shows is that A has no stopping condition at all, not that a real agent burns $24 in 3 seconds.

2. Why 9 calls and not 10. After nine $0.03 holds, the remaining balance is 0.02999… in floating-point arithmetic, so the tenth hold is refused. The stop lands one call earlier than exact arithmetic would.

Reproduce it

The demo was merged into the public repo in PR #9. You need Java 17, Docker Compose and Python 3.10+. No provider key.

git clone https://github.com/10kshuaizhang/fluxmeter.git
cd fluxmeter
make demo          # start the stack and verify one metered event
make demo-runaway  # same warm stack: unguarded loop vs Gateway 402

`make demo-runaway` points the Gateway at a local mock for the duration of the run and restores the default upstream when it exits. One of our runs printed:

 [A] no FluxMeter             814 calls in  3.06s  — still burning (3.0s cap)
 [B] FluxMeter Gateway          9 admitted, stop in  0.21s  — 402 budget_exceeded

                       A  no FluxMeter         B  FluxMeter Gateway
 Calls admitted        814                     9
 Spend USD             $24.42                  $0.27
 Budget                $0.30                   $0.30
 Overspend / debt      $24.12                  $0.00
 Time to stop          did not stop            0.21s

 Ledger customer=runaway-48a850df  events=9  cost_usd=0.27  debt_usd=0.0  held_usd=0.0  (settled)

The call count for A will differ on your machine. In three runs we saw 814–815 calls for A and stop times of 0.15s, 0.17s and 0.21s for B.

Without a proxy: the Python SDK, and the bug we shipped

The Gateway is a proxy. If you don't want one in the request path, the Python SDK wraps your OpenAI client instead. Until version 2.0.2, that path did not stop this loop, and we want to be specific about why.

What went wrong (SDK 2.0.1, local mock, $0.30 budget). For non-streaming calls, `wrap()` ran a budget check before each request but placed no hold. The balance only went down when Flink settled the usage window, about 10 seconds later. Until then, every check saw the full $0.30.

Tight loop: 1,835 calls, $55.05 spent, first refusal after 15.5 seconds.

One call per second, closer to a real agent: 28 calls, $0.84 (2.8× the budget), first refusal after 28.5 seconds.

Our documented reserve flow had a related leak. Releasing the hold right after `/ingest`, as the API reference then said to do, let a tight loop through 2,822 calls ($84.66) in 17.2 seconds, because the hold disappeared before Flink had deducted anything.

One thing did work: the ledger. The 1,835-call run was recorded as 1,835 events, $55.05 of cost and $54.75 of debt, exactly. The accounting was right; the admission was not.

The fix (SDK 2.0.2, engine 4.8.5). `wrap()` now opens a named hold before non-streaming calls as well as streaming ones, and attaches the reservation id when it reports usage. Flink releases the hold when it deducts the actual cost, and a reconcile call after ingest no longer releases it early. 2.0.2 is on PyPI.

After the fix (same mock, `fluxmeter==2.0.2` from PyPI):

Tight loop: 9 calls, $0.27, refused at 0.1 seconds, debt $0.00.

One call per second: 9 calls, $0.27, refused at 9.2 seconds, debt $0.00.

To rerun it, start the stack with `make demo`, then:

pip install "fluxmeter>=2.0.2" openai httpx
from openai import OpenAI
from fluxmeter import FluxMeter, wrap, BudgetExceededError

# Excerpt. The full script (mock upstream, $0.30 budget setup, loop) is at
# https://github.com/10kshuaizhang/fluxmeter/blob/main/examples/wrap_runaway.py
cust = "runaway-demo"  # the full script creates this customer's $0.30 budget first

client = wrap(
    OpenAI(base_url="http://127.0.0.1:9911/v1", api_key="sk-mock"),  # local mock upstream
    FluxMeter(api_url="http://localhost:8000"),
    customer_id=cust,
    fail_open=False,          # default is True: if FluxMeter is unreachable, calls go through
    estimated_cost_usd=0.03,  # default is 0.05; set it to what a call really costs
)

Full script, including the mock upstream and budget setup: https://github.com/10kshuaizhang/fluxmeter/blob/main/examples/wrap_runaway.py.

Two defaults matter here. `fail_open` is `True` unless you change it, and the default estimate is $0.05 whatever your real call costs. Both are reasonable defaults for availability, and both are wrong for a test like this.

When the estimate is too low: $0.42 on a $0.30 budget

Before the demo existed, we ran the same kind of loop through the Gateway with a different mock. The Gateway estimates each hold from `max_tokens` plus a default input size of 512 tokens. Our mock reported 4,000 prompt tokens per call, far more than the estimate.

What happened (fact): 14 calls were admitted, the 15th got a 402 at 0.3 seconds, and $0.42 was spent against $0.30. The ledger recorded $0.12 of debt.

Why (our arithmetic): with `max_tokens: 2000`, each hold was about $0.0213 while each call actually cost $0.03. Fourteen holds fit under $0.30, so fourteen $0.03 calls went through.

`make demo-runaway` avoids this by setting the Gateway's default input estimate to 4,000 tokens so that holds match the mock exactly. That makes the demo clean. Real traffic does not come with matching estimates, so treat the $0.42 run as the more realistic one.

What can still go over

The 402 in the demo is a budget stop based on estimates. It is not a promise that spend never exceeds the budget. Three risks remain:

1. Underestimated cost. A hold covers what you estimated, not what the provider later reports. If calls cost more than their holds, overspend per call is the difference, as in the $0.42 run. Set estimates from real request sizes and keep `max_tokens` bounded.

2. Concurrent calls on one wallet. Each hold is opened atomically, so two requests cannot both take the last slot. But each in-flight call carries its own estimation error, so N concurrent calls can each overshoot. Inference: we have not run a concurrent multi-agent test; the runs above are serial or a single loop.

3. Hold expiry. A hold that is never settled is released when it expires (15 minutes by default if no expiry is set). A call that runs past that, or whose usage report never arrives, frees budget room before its real cost is known. Inference from the code; not exercised in these runs.

Also out of scope here: streaming cut-off, real provider latency, multiple Gateway or API instances, and production load. A budget with no customer configured is allowed by default (`no_budget_configured`), so a missing budget is not a stop.

How this compares, plainly

LiteLLM. Its budgets documentation says budget reservation is enabled by default: it estimates the request's maximum cost, reserves it, rejects the request before the provider sees it if the reservation would exceed the budget, and replaces the reservation with the actual cost afterwards. A `fail_closed_budget_enforcement` setting (off by default) rejects requests with a 503 when current spend cannot be verified. The same page notes that budgets need a database. So refusing a call before it reaches the provider is not something FluxMeter does and LiteLLM doesn't.

OpenMeter. Its metered entitlements return `hasAccess`, `balance`, `usage` and `overage`, and with `isSoftLimit: false` access is denied once the balance reaches zero (quickstart). We did not find a per-request hold or reserve step in the docs we checked, so whether OpenMeter holds budget the way the test above does is unconfirmed. See also our OpenMeter comparison.

Where FluxMeter differs, in three points only:

1. SDK integration without running a proxy. The `wrap()` path above stops the loop with no proxy in the request path. (The headline demo uses the Gateway, which is a proxy; both paths exist.)

2. Per-customer attribution you can audit. In these runs, the ledger's event count, cost and debt matched the calls one for one, including the 1,835-call failure.

3. Export into the billing stack you already have. Usage can be exported to Lago, Stripe, Orb or Metronome (how). FluxMeter does not replace them.

Further reading

FluxMeter is open-source, self-hosted LLM token metering with customer attribution and an optional budget check/reserve step. It is not a billing suite and does not replace one.