Backstop
Tutorials

Quickstart

Get to a working budget guardrail in under 60 seconds.

Get to a working budget guardrail in under 60 seconds.

1. Install

pip install "backstop-ai"           # OpenAI only
pip install "backstop-ai[anthropic]"  # OpenAI + Anthropic

0.6.0 is published — on PyPI as backstop-ai, on npm as backstop-ai, and as the v0.6.0 GitHub Release. MIT licensed. The default branch carries unreleased work past that tag, including the spend ledger, so install from source to track main:

git clone https://github.com/RavaniRoshan/backstop.git
cd backstop && pip install -e ".[anthropic]"

2. Wrap your client

from openai import OpenAI
from backstop import Backstop

client = Backstop.wrap(OpenAI(), budget=50_000)

That's it. The wrapped client behaves identically to the original — until the budget is hit.

3. Catch the error

from backstop.exceptions import BudgetExceededError

try:
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": "Hello"}],
    )
except BudgetExceededError:
    print("Budget exhausted — agent loop stopped.")

BudgetExceededError is a subclass of openai.OpenAIError (and anthropic.AnthropicError for Anthropic clients), so existing error-handling code continues to work.

4. Verify offline (no API key needed)

backstop verify

This proves enforcement works end-to-end using a local mock transport. Should complete in under 5 seconds with 8/8 PASS.

5. See the guardrail in action

backstop demo

Runs a 10-iteration agent loop, unprotected vs wrapped, side by side. No API key needed.


Next steps


— Edit this page on GitHub

On this page