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.
Free tier
Bastion is currently free to use, with a limit of 10,000 protected calls per month across your account. This resets automatically every month - no signup, no credit card required. A paid tier with higher limits is planned - join the waitlist below if you would like early access.
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.
Coming soon
Paid tiers with higher usage limits are planned. Join the waitlist: https://tally.so/r/7RglMA
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bastion_runtime-0.1.3.tar.gz.
File metadata
- Download URL: bastion_runtime-0.1.3.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d236cbe9143a1338623fcff35a494b01c341ddb0f755af1f0fb82263dd166a76
|
|
| MD5 |
e2f56aca2f63091bafe4ed5d7bed9a12
|
|
| BLAKE2b-256 |
36e143dbf9feaddbea5d3dad8c8674d187867cf1f7701ebce807e451526261cb
|
File details
Details for the file bastion_runtime-0.1.3-py3-none-any.whl.
File metadata
- Download URL: bastion_runtime-0.1.3-py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
44f6f999a6e357b646bda36772322138d91fc6de2988ea690b3288587cd3b0f3
|
|
| MD5 |
7231b7e61ecd539921bd5bea10d1b8d2
|
|
| BLAKE2b-256 |
6207fe523e895ab4a8497a73140bb54285a6cc87db52654cacd73bb3fb1f7695
|