symfonic.agent.fastapi.budget_dep¶
budget_dep ¶
FastAPI circuit-breaker dependency: reject LLM calls when tenant is broke.
Gate 2 of v5.5.0. Wires
:class:symfonic.core.observability.TokenBudgetTracker into the request
cycle so a tenant that blew through its daily or monthly ceiling never
reaches agent.run() at all. When no tracker is registered the
dependency is a no-op — pre-existing deployments keep working unchanged.
Admin callers bypass the breaker. Admin status is signalled two ways:
request.state.is_admin— set by the scaffolded tenant-auth verifier after JWT decode. Preferred.X-Admin-Override: 1header — only trusted when the registeredset_tenant_auth_verifierhook has already authenticated the caller AND setrequest.state.is_admin = True(defence in depth).
The dependency deliberately reads tenant id directly from the
X-Tenant-ID header rather than re-running :func:get_tenant_scope
because FastAPI caches Depends per request — we want this check to
run BEFORE auth-expensive scope construction when possible. When used
together (as the router does), the scope check still fires first because
the router declares it earlier in the signature.
check_budget_before_llm
async
¶
Raise 429 when the tenant is over its daily/monthly budget.
No-op when
- no :class:
TokenBudgetTrackerhas been registered viaset_budget_tracker(); OR - the request is flagged as admin.
Raises:
| Type | Description |
|---|---|
HTTPException 429
|
|