Copied

Stop runaway agents before they drain your budget

budget-agent sits between your agent and the LLM. Hard limits on cost, tokens, and steps — enforced pre-flight and post-step.

agent.ts
1import { AgentBudget, BudgetError } from '@painitehq/ai-budget'
2 
3const agent = new AgentBudget({
4 apiKey: process.env.OPENROUTER_API_KEY,
5 limits: {
6 maxCostUSD: 0.10,
7 maxSteps: 15,
8 maxTotalTokens: 50_000,
9 maxWallTimeMs: 30_000
10 },
11});
12 
13const response = await agent.step({
14 model: 'anthropic/claude-opus-4-8',
15 messages: messages
16});
17 
18console.log(agent.getUsage())
19// { steps: 12, totalCostUSD: 0.0847, totalInputTokens: 24300, totalOutputTokens: 8200, elapsedMs: 45200 }
budget guard active$0.00 / $0.05 · 0 / 10 steps · 0ms

Features

Most agent frameworks let loops run. PainiteHQ does not.

Pre-flight

Catch overruns before they happen

Pre-flight checks estimate output cost before the API call fires. Already over budget — the call never lands.

Enforcement

Hard limits. Not suggestions.

Ceilings on cost, input tokens, output tokens, steps, wall time. Any one exceeded — agent stops, step rolled back, no partial charges.

Adaptive routing

Downgrade as budget depletes

Define a fallback chain. As spend crosses thresholds the router moves to a cheaper model — no code changes, no manual intervention.

Circuit breaker

Loops detected and killed

The circuit breaker watches for repetition and stagnation across steps. If the agent is spinning, it stops before the spin costs you.

Provider-agnostic

Works with any provider

OpenRouter, OpenAI, Together AI, Fireworks, Ollama, raw fetch. Bring your own executor. No provider bundled, no model defaulted.

Checkpoints

Survive restarts

Checkpoints persist agent state. Resume mid-run after a crash or deploy without replaying calls already billed.

Most agent frameworks treat every loop the same — a step to execute, a token to spend, a call to log. They are built to run agents, not to stop them.

Budget-agent by PainiteHQ is different. Budget enforcement runs across every step — checking limits before the call fires and after it lands, rolling back overages, routing to cheaper models as spend climbs.

FAQ

Have questions? Find answers.

Agent workflows can make hundreds of LLM calls across tools, retries, and loops. budget-agent adds hard limits on cost, tokens, steps, and runtime so a single workflow cannot exceed the budget you define.

Execution stops immediately. The SDK records actual usage, rolls the step back from the tracker, and throws a BudgetError by default. You can also provide a custom onExceeded handler to log, alert, switch models, or take another action.

Yes. budget-agent works with OpenRouter out of the box and supports OpenAI, Anthropic, Together AI, Fireworks, Ollama, LocalAI, and any OpenAI-compatible API. You can also bring your own executor for custom providers.

Each step is checked twice. A pre-flight check estimates the cost before the request is sent. A post-step check verifies actual usage after the response returns. If a limit is exceeded, execution is stopped before the workflow can continue.

Yes. Checkpoints allow agent state and usage data to persist between runs. Resume from a saved checkpoint without replaying previously completed steps or losing budget history.

Still have questions? Contact us