Skip to main content

LLM cost tracking and budget enforcement. Zero config — with budget(max_usd=1.00): run_agent(). Works with LangGraph, CrewAI, raw OpenAI/Anthropic.

Project description

PyPI version Python versions CI

with budget(max_usd=1.00):
    run_my_agent()  # raises BudgetExceededError if spend exceeds $1.00

I spent $47 debugging a LangGraph retry loop. The agent kept failing, LangGraph kept retrying, and OpenAI kept charging — all while I slept. I built shekel so you don't have to learn that lesson yourself.


Install

pip install shekel[openai]      # OpenAI only
pip install shekel[anthropic]   # Anthropic only
pip install shekel[all]         # Both SDKs

Advanced: pip install shekel installs the core with no SDK deps (track-only mode).


Quick start

from shekel import budget, BudgetExceededError
import openai

client = openai.OpenAI()

try:
    with budget(max_usd=1.00, warn_at=0.8) as b:
        response = client.chat.completions.create(
            model="gpt-4o",
            messages=[{"role": "user", "content": "Hello!"}],
        )
    print(f"Spent: ${b.spent:.4f} of ${b.limit:.2f}")
except BudgetExceededError as e:
    print(e)

Track-only mode

No max_usd = track spend without enforcing a limit. Great for profiling agents.

with budget() as b:
    run_my_agent()

print(f"That run cost: ${b.spent:.4f}")

How it works

  • Monkey-patching: When you enter a budget() context, shekel wraps openai.ChatCompletions.create and anthropic.Messages.create at the class level. Your code calls the real SDK — shekel intercepts the response, reads the token counts, and calculates cost. On context exit, original methods are restored.
  • ContextVar isolation: Each budget() context tracks its own spend using Python's contextvars.ContextVar. Two concurrent agent runs never share a budget counter, even in async or multi-threaded code.
  • Zero config: No API keys, no external services, no config files. pip install + with budget(...) is all you need.

Model support

Model Input (per 1k) Output (per 1k)
gpt-4o $0.00250 $0.01000
gpt-4o-mini $0.000150 $0.000600
o1 $0.01500 $0.06000
o1-mini $0.00300 $0.01200
gpt-3.5-turbo $0.000500 $0.001500
claude-3-5-sonnet-20241022 $0.00300 $0.01500
claude-3-haiku-20240307 $0.000250 $0.001250
claude-3-opus-20240229 $0.01500 $0.07500
gemini-1.5-flash $0.0000750 $0.000300
gemini-1.5-pro $0.00125 $0.00500

Model not listed? Pass a price override:

with budget(max_usd=1.00, price_per_1k_tokens={"input": 0.001, "output": 0.003}):
    run_my_agent()

Works with LangGraph, CrewAI, and any framework

shekel is framework-agnostic. It intercepts at the SDK level, so it works with anything that calls OpenAI or Anthropic under the hood:

# LangGraph
with budget(max_usd=2.00, warn_at=0.8) as b:
    result = langgraph_app.invoke({"input": "..."})
print(f"Graph run cost: ${b.spent:.4f}")

# CrewAI
with budget(max_usd=5.00) as b:
    crew.kickoff()

# Raw SDK
with budget(max_usd=0.50) as b:
    for _ in range(100):
        client.chat.completions.create(...)  # stops when budget hit

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

shekel-0.1.1.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

shekel-0.1.1-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file shekel-0.1.1.tar.gz.

File metadata

  • Download URL: shekel-0.1.1.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for shekel-0.1.1.tar.gz
Algorithm Hash digest
SHA256 330afdc460a0d8c3d261234880ab7cc5ba80ee857460392fb48960246e1de7ee
MD5 e824442e088dc5ffaa2eec89035b4aa4
BLAKE2b-256 9a1cae4d8a7e8db83e482f589c8d2caece5ef9eb49b5688edec331b2aad0f02d

See more details on using hashes here.

Provenance

The following attestation bundles were made for shekel-0.1.1.tar.gz:

Publisher: publish.yml on arieradle/shekel

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file shekel-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: shekel-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for shekel-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e1e80f86bfbcb9aea44685c050e48a8ebe8b21c8b10b481ff78515e9f44a658c
MD5 e026a0921bc2d5624530a0e6aa4b3de3
BLAKE2b-256 5a334a799507fc5f40307385ca0d0121d2832ada1309c1b915e689f961cc8e07

See more details on using hashes here.

Provenance

The following attestation bundles were made for shekel-0.1.1-py3-none-any.whl:

Publisher: publish.yml on arieradle/shekel

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page