Skip to main content

Budget-Aware Agentic Routing — route LLM calls intelligently between cheap and powerful models with a hard budget cap.

Project description

Baar-Core

Semantic routing + a hard financial kill-switch for LLM agents.

Never get surprised by another OpenAI or Anthropic bill.

PyPI version License: MIT Python 3.10+ Scientific Validation

pip install baar-core

baar-core is the PyPI package name. Baar-Core is the project.


Why Baar-Core?

Production LLM agents have a dangerous habit:

  • Simple queries still get sent to expensive models.
  • One runaway loop turns your $0.10 budget into $8+ overnight.
  • The invoice lands before you know which step burned the budget.

Most routers optimize averages. Baar-Core ships a hard Zero-Call Financial Kill-Switch: enforce a strict USD cap, score complexity, route cheap vs capable — and if the next safe call would exceed what’s left, reject locally before a single provider request. $0 spent. Zero network calls.

What you get

  • Smart semantic routing — Easy work → cheap model; hard work → capable model.
  • Budget-constrained downgrade — If the big model would break the budget, fall back to the small one so the turn can still finish.
  • True zero-call kill-switch — Even the cheap model unaffordable? Fail fast — no completion call, no surprise line item.
  • Offline Safety — If your budget is $0, baar-core won't even attempt a DNS lookup for the LLM provider. It fails instantly in your local environment.

No surprise invoices. Stronger stance against runaway and adversarial “denial of wallet” patterns. Quality where it matters (reasoning, coding, agents) because hard tasks still reach the capable tier when the budget allows.


How it works

graph TD
    A[User task] --> B{Semantic complexity router}
    B -- Low complexity --> C[Cheap model]
    B -- High complexity --> D{Budget check}
    D -- Affordable --> E[Capable model]
    D -- Too expensive --> F[Downgrade to cheap]
    C --> G[Spend tracking]
    E --> G
    F --> G
    G --> H[Response]
  1. Complexity scoring — Fast signal for cheap vs expensive route.
  2. Budget-aware choice — Remaining budget checked before committing to the expensive path.
  3. Local rejection — Exhausted or unsafe to call? Stop before the wire.

Benchmarks

Mock benchmark (deterministic, calibrated policy)

Command:

baar-bench \
  --dataset all \
  --limit 200 \
  --budget 10 \
  --mock \
  --value-policy simple \
  --auto-calibrate-alpha \
  --target-reject-rate 0.05 \
  --alpha-source percentile \
  --max-reject-rate 0.5 \
  --small-exploration-rate 0.1 \
  --seed 42
Dataset Strategy Accuracy Total cost vs always-big
MMLU Always big 100.0% $1.990500
MMLU Baar-Core 91.5% $1.625000 60.9% cheaper
GSM8K Always big 100.0% $1.990500
GSM8K Baar-Core 90.0% $1.478000 60.4% cheaper
HumanEval Always big 100.0% $1.630500
HumanEval Baar-Core 92.7% $1.369500 48.1% cheaper

Live benchmark (small subset sanity check)

Command:

baar-bench \
  --dataset all \
  --limit 10 \
  --budget 2 \
  --value-policy none \
  --small-exploration-rate 0.0 \
  --seed 42
Dataset Strategy Accuracy Total cost vs always-big
MMLU Always big 50.0% $0.002337
MMLU Baar-Core 60.0% $0.000137 93.3% cheaper
GSM8K Always big 60.0% $0.027615
GSM8K Baar-Core 20.0% $0.002097 93.3% cheaper
HumanEval Always big 0.0% $0.032125
HumanEval Baar-Core 0.0% $0.002743 93.3% cheaper

Live results can vary significantly by provider/model quality, API reliability, and prompt behavior. Use live runs as environment-specific checks, and use mock runs for reproducible routing/cost trade-off iteration.


Quick start

from baar import BAARRouter

router = BAARRouter(budget=0.10)
print(router.chat("What is the capital of France?"))          # → usually cheap model
print(router.chat("Write an optimized CUDA matmul kernel."))  # → capable model if affordable

# Kill-switch: budget too low for any safe call → blocked before the API
tight = BAARRouter(budget=0.00001)
try:
    tight.chat("Any prompt")
except RuntimeError as e:
    print("Blocked safely:", e)  # zero completion calls, $0 spent

Works with any LiteLLM-supported provider (OpenAI, Anthropic, Groq, Together, Ollama, OpenRouter, …).


Resilience

baar-stress

Adversarial-style checks (complexity games, tight budget). Baar-Core is designed with OWASP LLM Top 10 style risks in mind — including unbounded consumption. Details: RESEARCH.md.


Telemetry Summary CLI

If you enable telemetry_jsonl_path on BAARRouter, summarize logs with:

baar-telemetry path/to/telemetry.jsonl

This prints reject rate, failover rate, total spend, and per-model spend distribution.


Configuration

Default complexity_threshold=0.80 routes more traffic to the cheap model than 0.65 did; the effective threshold also rises with budget utilization so BIG is harder to justify as spend accumulates. Tighten or loosen with complexity_threshold if your workload skews very easy or very hard.

router = BAARRouter(
    budget=0.10,
    small_model="gpt-4o-mini",
    big_model="gpt-4o",
    complexity_threshold=0.80,
)

License & research

MITLICENSE.

Architecture, validation notes, and security mapping: RESEARCH.md.

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

baar_core-0.2.4.tar.gz (32.7 kB view details)

Uploaded Source

Built Distribution

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

baar_core-0.2.4-py3-none-any.whl (20.5 kB view details)

Uploaded Python 3

File details

Details for the file baar_core-0.2.4.tar.gz.

File metadata

  • Download URL: baar_core-0.2.4.tar.gz
  • Upload date:
  • Size: 32.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for baar_core-0.2.4.tar.gz
Algorithm Hash digest
SHA256 9edd926cb3384777366574a0f7320282abc657aca24ba7abeeac914b5a9fabfd
MD5 4b4bf52a8b047583647d1a9f20e2b1f6
BLAKE2b-256 add465eff170a840e8cca12810fd6e4df192784a24a4e2654ec963c1b8c76eac

See more details on using hashes here.

File details

Details for the file baar_core-0.2.4-py3-none-any.whl.

File metadata

  • Download URL: baar_core-0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 20.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for baar_core-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 89390ade8f37870ca13d3f3ebd4cd1ba6f4364103fcfa18a765013398a708d0d
MD5 65a83b933d5e140fc47f8f9de8682a46
BLAKE2b-256 c08d9ba578af0ebbb0c31f198ba23efbd6d19cae36f68c94f5287ddca621c31c

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