Skip to main content

A lightweight runtime protection layer for AI agents - stops loops, retry storms, and runaway spend before they reach the model provider.

Project description

Bastion

A lightweight runtime protection layer for AI agents. Drop it in front of your OpenAI calls and it automatically stops the three most common ways autonomous agents waste money or break production - before they happen, not after you see the bill.

The problem

Autonomous AI agents fail in specific, predictable ways:

  • Reasoning loops - an agent gets stuck rephrasing the same broken question over and over, burning real API calls that produce zero value
  • Retry storms - a third-party API hiccups, and a poorly-built agent responds by firing dozens or hundreds of retries in seconds
  • Runaway spend - one broken session or user can rack up a huge bill, and most providers only offer organization-wide monthly caps - meaning one bad actor takes down your entire account, locking out every other user

Bastion catches all three, in real time, per session - so one broken part never takes down everything else.

Usage - one line swap

This is all you need to get started. Install the package, then swap two lines in your existing code.

Install:

pip install bastion-runtime

Your code currently looks like this (shown for comparison only, do not paste this part):

from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(model="gpt-4o-mini", messages=[])

Change it to this (paste this part):

from bastion import Bastion
client = Bastion()
response = client.chat.completions.create(
    session_id="user-123",
    model="gpt-4o-mini",
    messages=[],
)

session_id identifies which user, agent, or workflow a call belongs to. Bastion tracks each session independently - if one session gets blocked, every other session keeps working normally.

Everything below this point is optional. Bastion works fully with the defaults shown above - only read further if you want to handle errors specifically or change any settings.

What it catches

Semantic Loop Detector - converts recent messages into embeddings and measures actual similarity. If a session asks a near-duplicate question 3 times in a row, even reworded, it kills that session.

Retry Storm Detector - if a session fires more than 10 calls in a 1-second window, Bastion forces an immediate cooldown.

Micro-Budget Circuit Breaker - tracks spend and call count for each individual session separately. If one session goes over its limit, only that session gets blocked. Every other user or agent in your app carries on working normally. Completely unaffected.

Handling blocks (optional)

If you want to catch and respond to Bastion blocking something specifically, paste this instead of a plain try/except:

from bastion import LoopDetected, RetryStormDetected, BudgetExceeded

try:
    response = client.chat.completions.create(session_id="user-123")
except LoopDetected as e:
    pass
except RetryStormDetected as e:
    pass
except BudgetExceeded as e:
    pass

Configuration (optional)

Every setting below already has a sensible default. You only need this if you want to change something specific - for example, a stricter loop detector or a higher budget cap:

from bastion import Bastion, BastionConfig

config = BastionConfig(
    loop_similarity_threshold=0.75,
    retry_max_calls=10,
    max_cost_usd_per_session=2.00,
    reset_period_seconds=2592000,
)
client = Bastion(config=config)

Running tests

pip install pytest
pytest test_bastion.py -v

Tests use a fake embedding function so they run instantly, with no API key required.

License

This project is licensed under the Business Source License 1.1 - free to use, modify, and self-host, but not to resell as a competing commercial service. See LICENSE for details.

Status

Core protection logic is built and tested, including live verification against the OpenAI API. Currently OpenAI-only; other providers planned.

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

bastion_runtime-0.1.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

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

bastion_runtime-0.1.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file bastion_runtime-0.1.0.tar.gz.

File metadata

  • Download URL: bastion_runtime-0.1.0.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for bastion_runtime-0.1.0.tar.gz
Algorithm Hash digest
SHA256 87c092c5681d381877c0e79ab743d1bf9f78c1158aee8622987c3649febd7237
MD5 8f4bc596dc4a6e01bf4ca6a719c9a8a5
BLAKE2b-256 7ce471fcdabad38c192363ebd3260f584bb18b117ba73cdc38193fe27b28ed23

See more details on using hashes here.

File details

Details for the file bastion_runtime-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for bastion_runtime-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a993d00b4a9bbc457362c4aafb0655223bca235dbf21b35c422b1668340f8ee5
MD5 20336683cb7bebc3ab4733b56515baa2
BLAKE2b-256 ab7432bb74332e102f74f7fae2f5ce843e4f1cb12b71464377378021c586c203

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