judgevet.adapters.outbound.spend
Status: draft.
judgevet.adapters.outbound.spend
Meter adapter attempts against an opt-in SpendCap.
metered and ametered claim a slot on the cap before each physical attempt
and settle the reported input tokens after it. The SpendCap class lives in
judgevet.domain.spend and is imported here, so
from judgevet.adapters.outbound.spend import SpendCap keeps working. The cap
semantics and their citations are documented on that module.
Examples:
from judgevet.adapters.outbound.spend import SpendCap
from judgevet.domain.errors import JevBudgetExceededError
cap = SpendCap(max_attempts=1)
cap.claim()
try:
cap.claim()
except JevBudgetExceededError as error:
assert (error.limit, error.cap, error.spent) == ("attempts", 1, 1)
See Also
- judgevet.domain.spend: The cap, its counters and its citations.
- judgevet.adapters.outbound.http: Adapters that accept
spend_cap=. - judgevet.adapters.outbound.retries: Retry loop that stops on the refusal.
- judgevet.domain.errors.JevBudgetExceededError: The refusal error.
ametered(cap, send)
Wrap one asynchronous attempt with a claim before it and a settlement after.
The lock is taken inside claim and settle only, never across the await.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cap
|
SpendCap | None
|
The shared cap, or None to leave the attempt unchanged. |
required |
send
|
Callable[[], Awaitable[SystemOneResponse]]
|
One physical asynchronous attempt with translated errors. |
required |
Returns:
| Type | Description |
|---|---|
Callable[[], Awaitable[SystemOneResponse]]
|
The attempt itself when cap is None, otherwise the metered attempt. |
metered(cap, send)
Wrap one synchronous attempt with a claim before it and a settlement after.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cap
|
SpendCap | None
|
The shared cap, or None to leave the attempt unchanged. |
required |
send
|
Callable[[], SystemOneResponse]
|
One physical attempt with translated errors. |
required |
Returns:
| Type | Description |
|---|---|
Callable[[], SystemOneResponse]
|
The attempt itself when cap is None, otherwise the metered attempt. |