# `Spectre.Operation.Budget`
[🔗](https://github.com/elchemista/spectre/blob/0.4.0/lib/spectre/operation/budget.ex#L1)

Portable limits and consumption counters for one operational loop.

A missing limit means that dimension is not bounded. Budget checks happen
before a Runner is created and again after its committed result.

# `dimension`

```elixir
@type dimension() :: :steps | :attempts | :retries | :duration_ms | :pages | :cost
```

# `t`

```elixir
@type t() :: %Spectre.Operation.Budget{
  consumed: %{optional(atom()) =&gt; non_neg_integer() | number()},
  deadline_at: non_neg_integer() | nil,
  limits: %{optional(dimension()) =&gt; non_neg_integer() | number() | nil},
  resources: map(),
  started_at: non_neg_integer()
}
```

# `consume`

```elixir
@spec consume(t(), map() | keyword()) :: t()
```

Consumes one or more budget dimensions.

# `exhausted`

```elixir
@spec exhausted(t(), non_neg_integer()) :: nil | {dimension(), number(), number()}
```

Returns the first exhausted dimension, if any.

# `new`

```elixir
@spec new(t() | map() | keyword() | nil, non_neg_integer()) :: t()
```

Builds a validated budget from a struct, map, keyword list or `nil`.

`nil` yields an unbounded budget. Limits may be given at the top level or under
`:limits`; a `:duration_ms` limit derives `deadline_at` from `now` unless a
deadline is given explicitly. Raises `ArgumentError` when the result is invalid.

# `remaining`

```elixir
@spec remaining(t(), dimension()) :: number() | :infinity
```

Returns the remaining value for a bounded dimension.

# `validate`

```elixir
@spec validate(t()) :: :ok | {:error, term()}
```

Validates the budget's limits, consumption counters, timestamps and portability.

Returns `:ok`, or `{:error, reason}` naming the first invalid field.

# `validate!`

```elixir
@spec validate!(t()) :: t()
```

Same as `validate/1` but returns the budget, raising `ArgumentError` when invalid.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
