Bridle
Reference

Wrappers

cache, retry, timeout, with_model, fallback, mock, log. Signatures, parameters, returns.

Each wrapper takes a Call and returns a new Call. Compose freely. See the wrappers guide for usage patterns.

cache

bridle.cache(
    call: Call,
    *,
    key: Callable[[Call], str] | str | None = None,
    backend: CacheBackend | None = None,
    ttl: float | None = None,
    label: str | None = None,
) -> Call

Memoize call.

ParameterTypeDescription
callCallThe call to memoize.
keystr | Callable | NoneFixed string, callable taking the inner Call, or None for the default deterministic key.
backendCacheBackend | NoneOverride the active backend. Defaults to the one set via bridle.set_cache, or an in-memory cache.
ttlfloat | NoneSeconds to live. None means no expiry.
labelstr | NoneTrace label. Defaults to "cache".

The default key hashes kind | schema fingerprint | prompt | context | tools fingerprint. Two calls with the same five parts share a key across processes.

Trace events: cache_hit on key found, cache_miss on miss (and again with write_error if a write fails).

retry

bridle.retry(
    call: Call,
    *,
    attempts: int = 3,
    on: type[BaseException] | tuple[...] = BridleError,
    backoff: float | Callable[[int], float] | None = None,
    label: str | None = None,
) -> Call

Re-evaluate call when it raises a matching exception. Each attempt clones the inner call.

ParameterTypeDescription
callCallThe call to retry.
attemptsintTotal attempts. Must be positive. Default 3.
onexception type or tupleWhat to catch. Default BridleError.
backofffloat | Callable[[int], float] | NoneFixed seconds, or a callable taking the 0-based attempt index.
labelstr | NoneTrace label. Defaults to "retry".

Trace events: retry per attempt with attempt number, remaining count, and error string.

Raises: the last error from the inner call after all attempts exhausted.

timeout

bridle.timeout(
    call: Call,
    *,
    seconds: float,
    label: str | None = None,
) -> Call

Abort call if it takes longer than seconds. Implemented with a single-thread executor and a copied contextvars.Context.

ParameterTypeDescription
callCallThe call to bound.
secondsfloatWall-clock deadline.
labelstr | NoneTrace label. Defaults to "timeout".

Raises: bridle.TimeoutError on deadline. The underlying work continues until the SDK's own cancellation kicks in.

with_model

bridle.with_model(
    call: Call,
    model: str,
    *,
    label: str | None = None,
) -> Call

Override the model for call. Highest-precedence layer of model resolution.

ParameterTypeDescription
callCallThe call to override.
modelstrModel id passed to the active model client.
labelstr | NoneTrace label. Defaults to "with_model[<model>]".

fallback

bridle.fallback(
    call: Call,
    *alternates: Call,
    label: str | None = None,
) -> Call

Try call; on BridleError, try each alternate in order. The first to resolve wins.

ParameterTypeDescription
callCallPrimary candidate.
*alternatesCallSubsequent candidates, tried in order.
labelstr | NoneTrace label. Defaults to "fallback".

Trace events: retry per failed candidate with reason: "fallback", candidate index, remaining count.

Raises: the last BridleError if every candidate failed.

mock

bridle.mock(
    call: Call,
    value: Any,
    *,
    label: str | None = None,
) -> Call

Replace call's dispatch with a constant. The wrapper's call_start and call_end still fire, so trace assertions on shape remain valid.

ParameterTypeDescription
callCallThe call whose dispatch to short-circuit.
valueAnyThe constant returned in place of the inner work.
labelstr | NoneTrace label. Defaults to "mock".

log

bridle.log(
    call: Call,
    *,
    level: Literal["DEBUG", "INFO", "WARNING", "ERROR"] = "INFO",
    logger_name: str = "bridle",
    label: str | None = None,
) -> Call

Stream the active trace's events to a Python logger for call's subtree.

ParameterTypeDescription
callCallThe subtree to log.
levellog level literalLogging level for emitted events. Default "INFO".
logger_namestrLogger name. Default "bridle".
labelstr | NoneTrace label. Defaults to "log".

Each event is logged as bridle.<kind> call_kind=<...> label=<...> error=<...>.

On this page