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.6.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.6-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tetherai_python-0.1.6.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for tetherai_python-0.1.6.tar.gz
Algorithm Hash digest
SHA256 9da34c8500f9bb9772a4c955837d9b0255a48e0ac62d88726b543c17bd9f0b4f
MD5 9e197331590736d27cdc02ff0cc0f7fd
BLAKE2b-256 29db4a8b1870de82f70192ec5c14221b1037cec5b9f9ed7ef55141d6a9ed0489

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tetherai_python-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 46a3b80d132c82207d628e9cb237dc1dad82f4edebdfdec05a467d2f4b6f25f9
MD5 23262ebf679a897a152b518ddbe55742
BLAKE2b-256 856dca4d0742f6de70052225c69e825d7bdce1e49b5c1b9e703c87b8fd2ceb58

See more details on using hashes here.

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