Skip to main content

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

Metadata

Release files for bastion-runtime 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bastion-runtime 0.1.3
File Size Uploaded
bastion_runtime-0.1.3.tar.gz 8.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bastion-runtime 0.1.3
File Interpreter ABI Platform
bastion_runtime-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 17.7 kB

Release files / bastion_runtime-0.1.3.tar.gz

Download URL bastion_runtime-0.1.3.tar.gz
Size 8.7 kB
Tags Source
SHA-256 checksum
How to use checksums
d236cbe9143a1338623fcff35a494b01c341ddb0f755af1f0fb82263dd166a76
BLAKE2b-256 checksum
How to use checksums
36e143dbf9feaddbea5d3dad8c8674d187867cf1f7701ebce807e451526261cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.6

Release files / bastion_runtime-0.1.3-py3-none-any.whl

Download URL bastion_runtime-0.1.3-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
44f6f999a6e357b646bda36772322138d91fc6de2988ea690b3288587cd3b0f3
BLAKE2b-256 checksum
How to use checksums
6207fe523e895ab4a8497a73140bb54285a6cc87db52654cacd73bb3fb1f7695
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.6

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page