npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

llm-execution-budget

v0.2.0

Published

Minimal control-plane primitive for limiting LLM execution costs.

Readme

LLM Execution Budget SDK

Minimal control-plane primitive for limiting LLM execution costs.

Install

npm install llm-budget-sdk

API

import { createBudget, guardedResponse, isBudgetError } from "llm-budget-sdk";

createBudget(limits, now?)

Creates a budget tracker.

const budget = createBudget({
  executionId: "task-123",       // optional, included in errors
  maxSteps: 10,                  // max LLM calls (attempts count)
  maxToolCalls: 50,              // max tool invocations
  timeoutMs: 30_000,             // wall clock limit
  maxOutputTokens: 4096,         // per-call output cap
  maxTokens: 100_000,            // total tokens; checked between calls; may overshoot by one call
  tokenAccountingMode: "fail-open", // or "fail-closed"
});

guardedResponse(budget, params, fn)

Wraps one LLM call. Enforces limits, clamps output tokens, tracks usage.

const response = await guardedResponse(
  budget,
  { model: "gpt-4", messages: [...] },
  (p) => openai.responses.create(p)
);

budget.recordToolCall()

Manually record a tool invocation. Call this each time your agent executes a tool.

budget.recordToolCall();

isBudgetError(e)

Type guard for budget errors.

try {
  await guardedResponse(budget, params, fn);
} catch (e) {
  if (isBudgetError(e)) {
    console.log(e.reason);   // "TIMEOUT" | "STEP_LIMIT" | "TOOL_LIMIT" | "TOKEN_LIMIT" | "USAGE_UNAVAILABLE"
    console.log(e.snapshot); // full state at time of error
  }
}

Limits

| Limit | Enforced | Behavior | |-------|----------|----------| | maxSteps | Before call | Throws STEP_LIMIT if exceeded | | maxToolCalls | Before recordToolCall | Throws TOOL_LIMIT if exceeded | | timeoutMs | Before call/recordToolCall | Throws TIMEOUT if elapsed ≥ timeout | | maxOutputTokens | Per call | Clamps params.max_output_tokens | | maxTokens | Between calls | Marks terminated after call, throws TOKEN_LIMIT on next boundary |

Precedence when multiple limits apply: TIMEOUT → STEP_LIMIT → TOOL_LIMIT → TOKEN_LIMIT

Token Accounting

The SDK reads usage.total_tokens (or prompt_tokens + completion_tokens) from responses.

If usage data is missing:

| Mode | Behavior | |------|----------| | "fail-open" (default) | Sets tokenAccountingReliable = false, disables maxTokens enforcement. Other limits still apply. | | "fail-closed" | Throws USAGE_UNAVAILABLE immediately. |

⚠️ Warning: In fail-open mode, a provider that omits usage data will bypass your token budget entirely. Use fail-closed if token limits are critical.

The snapshot.tokenAccountingReliable field tells you whether token enforcement was active.

Error Shape

class BudgetError extends Error {
  reason: "TIMEOUT" | "STEP_LIMIT" | "TOOL_LIMIT" | "TOKEN_LIMIT" | "USAGE_UNAVAILABLE";
  executionId?: string;
  snapshot: {
    stepsUsed: number;
    maxSteps: number;
    toolCallsUsed: number;
    maxToolCalls: number;
    tokensUsed: number;
    maxTokens: number;
    overshoot?: number;          // only for TOKEN_LIMIT
    elapsedMs: number;
    timeoutMs: number;
    tokenAccountingReliable: boolean;
  };
}

Example

import { createBudget, guardedResponse, isBudgetError } from "llm-budget-sdk";
import OpenAI from "openai";

const openai = new OpenAI();

const budget = createBudget({
  maxSteps: 5,
  maxToolCalls: 20,
  timeoutMs: 60_000,
  maxOutputTokens: 2048,
  maxTokens: 50_000,
  tokenAccountingMode: "fail-closed", // strict mode
});

async function agentLoop() {
  while (true) {
    try {
      const response = await guardedResponse(
        budget,
        { model: "gpt-4", messages: [...] },
        (p) => openai.responses.create(p)
      );

      for (const toolCall of response.tool_calls ?? []) {
        budget.recordToolCall();
        // execute tool...
      }

      if (response.done) break;
    } catch (e) {
      if (isBudgetError(e)) {
        console.log(`Budget exceeded: ${e.reason}`, e.snapshot);
        break;
      }
      throw e;
    }
  }
}

Test

npm test

License

MIT