Skip to main content

AI budget guardrails for LLM applications

Project description

TetherAI

CI PyPI Python License

Financial circuit breaker for AI agents. Stop runaway LLM costs before they happen.

AI agents don't crash when they fail — they loop, hallucinate, and retry. A stuck CrewAI crew can silently burn hundreds of dollars in LLM API calls over a weekend. Traditional monitoring tools like Datadog will show you an API cost spike on Monday morning. TetherAI kills the agent before it spends your money.

Install

pip install tetherai-python

With CrewAI support:

pip install tetherai-python[crewai]

Quick Start

The @enforce_budget decorator wraps any function with budget guardrails:

from tetherai import tether, BudgetExceededError
import litellm


@tether.enforce_budget(max_usd=0.05)
def my_workflow():
    for i in range(100):
        response = litellm.completion(
            model="gpt-4o-mini",
            messages=[{"role": "user", "content": f"Count to {i}"}]
        )
        print(f"Call {i+1}: {response.choices[0].message.content[:50]}...")


try:
    my_workflow()
except BudgetExceededError as e:
    print(f"\n🛑 Stopped! Spent ${e.spent_usd:.4f} of ${e.budget_usd:.2f} budget")

Expected output:

Call 1: 0...
Call 2: 0, 1...
Call 3: 0, 1, 2...

🛑 Stopped! Spent $0.0501 of $0.05 budget

CrewAI Integration

For CrewAI crews, use protect_crew() to wrap budget enforcement around the entire crew:

from tetherai import protect_crew, BudgetExceededError
from crewai import Agent, Task, Crew, Process

researcher = Agent(
    role="Research Analyst",
    goal="Find comprehensive information on AI observability tools",
    backstory="You are a thorough research analyst known for detailed analysis.",
    verbose=True,
)

task = Task(
    description="Research the competitive landscape of AI observability tools",
    agent=researcher,
)

crew = Crew(agents=[researcher], tasks=[task], process=Process.sequential)

protected_crew = protect_crew(crew, max_usd=0.10)

try:
    result = protected_crew.kickoff()
except BudgetExceededError as e:
    print(f"Budget exceeded: ${e.spent_usd:.2f} of ${e.budget_usd:.2f}")

What Happens When Budget Is Exceeded

When the budget is exceeded, TetherAI raises BudgetExceededError with details about the run:

{
  "run_id": "run_abc123",
  "budget_usd": 0.10,
  "spent_usd": 0.13,
  "turns": 7,
  "spans": [
    {
      "span_type": "llm_call",
      "model": "gpt-4o-mini",
      "input_tokens": 1250,
      "output_tokens": 340,
      "cost_usd": 0.0019,
      "status": "ok"
    }
  ]
}

How It Works

TetherAI patches litellm.completion at runtime to intercept every LLM call your agent makes. Before each call, it counts input tokens locally using tiktoken and checks the projected cost against your budget. If the budget would be exceeded, the call is blocked and a BudgetExceededError is raised. After each successful call, actual token usage from the LLM response is recorded for accurate cost tracking.

Supported Frameworks

Framework Status Integration
CrewAI ✅ Supported protect_crew()
LiteLLM (direct) ✅ Supported @enforce_budget decorator
LangChain 🔜 Coming soon
smolagents 🔜 Coming soon

Contributing

We welcome contributions! Please see CONTRIBUTING.md for guidelines.

License

Apache 2.0

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

tetherai_python-0.1.31.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

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

tetherai_python-0.1.31-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

Details for the file tetherai_python-0.1.31.tar.gz.

File metadata

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

File hashes

Hashes for tetherai_python-0.1.31.tar.gz
Algorithm Hash digest
SHA256 30c1207c973a64621999c3371fdae1d0d3798be5141b790979a058dee01c3436
MD5 fe8ec649e373c8410fb53d2b1e6e2416
BLAKE2b-256 c7a67d4b0b470968c79b90a6b89a357b6eeb8841ba9f9be3a8817ae299b423f7

See more details on using hashes here.

Provenance

The following attestation bundles were made for tetherai_python-0.1.31.tar.gz:

Publisher: publish.yml on tetheraiio/tetherai-python

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

File details

Details for the file tetherai_python-0.1.31-py3-none-any.whl.

File metadata

File hashes

Hashes for tetherai_python-0.1.31-py3-none-any.whl
Algorithm Hash digest
SHA256 f89072ec5a18f938f41bf44e1d1f4acb762794dfe959f79f57f793fc57a97859
MD5 7f3f3c2a05292d53cb4903a1282c2579
BLAKE2b-256 5bc0a0149596dd13c109048519a2912032f8fca38f987b0826068aa52244075a

See more details on using hashes here.

Provenance

The following attestation bundles were made for tetherai_python-0.1.31-py3-none-any.whl:

Publisher: publish.yml on tetheraiio/tetherai-python

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