Priority Admission Control
Queue requests when the concurrency limit is reached and admit them in priority order, without discarding background work.
Under high load, Backstop's admission control queues requests and admits
them in priority order. You assign a priority to each request via the
X-Backstop-Priority header — no per-call code changes needed.
There are exactly three priorities — critical, default and background
(backstop.Priority). There is no normal, and the gate does not shed: it
never cancels or discards a lower-priority waiter.
The three priority levels
Backstop defines three priorities via the Priority enum:
from backstop import Priority
Priority.CRITICAL # user-facing, must not be delayed
Priority.DEFAULT # standard traffic
Priority.BACKGROUND # batch jobs, analytics, indexingMarking requests by priority
Set the X-Backstop-Priority header on any SDK call:
from openai import OpenAI
from backstop import Backstop
client = Backstop.wrap(OpenAI(), budget=100_000)
# Critical — user-facing request, admitted ahead of the waiters
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Generate a response"}],
extra_headers={"X-Backstop-Priority": "critical"},
)
# Background — batch summarization, admitted last
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Summarize last week's logs"}],
extra_headers={"X-Backstop-Priority": "background"},
)How admission control works
When too many requests are in-flight (hitting the concurrency ceiling), Backstop queues incoming requests. The admission gate processes the queue with this rule:
- Critical requests are selected ahead of waiting
defaultandbackgroundones. - Default requests wait behind any queued critical requests.
- Background requests wait behind both, and are not discarded when the queue grows. They wait their turn.
Anti-starvation runs the other way. starvation_after_seconds (default 1.0)
releases the oldest ticket in any queue once it has waited that long, even if
a higher priority is also waiting — so a background request that has aged can be
admitted ahead of a critical one that arrived later. Set it to 0 to disable
starvation protection entirely.
What actually stops a request is elsewhere, and it is worth being precise about which mechanism does what:
| mechanism | what it does | when |
|---|---|---|
starvation_after_seconds | releases the oldest waiter so nothing waits forever | on its own timer |
queue_timeout | bounds how long any request will wait before it gets an error instead | on its own timer |
| token budget | raises BudgetExceededError before dispatch — nothing is spent | the budget ceiling is reached |
| circuit breaker | raises CircuitBreakerOpenError before dispatch — fail fast | the circuit is open |
from openai import OpenAI
from backstop import Backstop, BackstopConfig
client = Backstop.wrap(
OpenAI(),
budget=50_000,
config=BackstopConfig(
initial_concurrency=4, # allow 4 concurrent calls
max_concurrency=16, # scale up to 16 under light load
starvation_after_seconds=1.0, # release the oldest waiter after 1s
queue_timeout=10.0, # error rather than wait past 10s
),
)Priority-aware fallback chains
You can also route different priorities to different fallback chains:
from backstop import BackstopConfig, Priority
config = BackstopConfig(
fallback_chain_for_priority={
Priority.CRITICAL: [
{"model": "gpt-4o"},
{"model": "gpt-4o-mini"}, # degraded fallback for critical traffic
],
Priority.BACKGROUND: [
{"model": "gpt-3.5-turbo"}, # cheaper fallback for batch
],
},
)Complete example
A full priority admission demo is in
examples/background_priority.py:
from openai import OpenAI
from backstop import Backstop, BackstopConfig
client = Backstop.wrap(
OpenAI(),
budget=100_000,
config=BackstopConfig(initial_concurrency=8),
)
def user_facing_request(prompt: str):
return client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
extra_headers={"X-Backstop-Priority": "critical"},
)
def background_job(prompt: str):
return client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
extra_headers={"X-Backstop-Priority": "background"},
)Verification
backstop verify includes an isolation check that proves per-agent budgets are
independent — critical for ensuring priority routing doesn't leak budgets:
- [PASS] per-agent isolation: agent A blocked 5/5 at its own cap while agent B kept serving — budgets are independent.— Edit this page on GitHub
Per-Tenant Budgets with FastAPI
Apply independent token budgets to each tenant using the with_budget context manager inside a FastAPI request handler.
Roll Out Enforcement in Shadow Mode
Observe what Backstop would block before actually blocking anything — safe rollout for budget, circuit, and rate-limit policies.