> ## Documentation Index
> Fetch the complete documentation index at: https://docs.pylva.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Budget Enforcement

> Use SDK-side budget rules to warn or block calls before customer spend runs away.

Budget rules cap spend for a customer or pooled account over a daily, weekly, or monthly period.

## Enforcement modes

| Mode      | Behavior                                                                           |
| --------- | ---------------------------------------------------------------------------------- |
| Warn      | The provider call proceeds. Pylva records and alerts when spend crosses the limit. |
| Hard stop | The SDK throws before the provider call, so the provider is not billed.            |

## TypeScript handling

```ts theme={null}
import { init, PylvaBudgetExceeded } from "@pylva/sdk";

init({ apiKey: process.env.PYLVA_API_KEY! });

try {
  return await openai.chat.completions.create({ model, messages });
} catch (err) {
  if (err instanceof PylvaBudgetExceeded) {
    return cachedOrSmallerResponse();
  }
  throw err;
}
```

## Python handling

```python theme={null}
from pylva import PylvaBudgetExceeded

try:
    response = openai_client.chat.completions.create(...)
except PylvaBudgetExceeded:
    response = cached_or_smaller_response()
```

## How the SDK stays current

The SDK keeps a local accumulator for active budget rules so it can make a fast pre-call decision. Pylva then reconciles that local view with backend totals. Backend totals are authoritative.

If pricing data is unavailable or the backend cannot be reached, the SDK passes through rather than blocking the host application because of a Pylva outage.

## Recommended fallback behavior

When a hard stop fires, return a product-specific fallback:

* Cached answer.
* Smaller model path.
* Queued job.
* "Try again later" response.
* Human handoff.

Avoid retry loops that immediately reissue the same expensive call.
