Skip to content

latent.context.decorators

@context_check decorator and discovery for context engineering checks.

Mirrors the @guardrail decorator pattern but adds tier and trigger parameters and attaches _is_context_check / _context_check_tier to the resulting scanner so middleware can emit context-specific events.

Signature detection: - (self) -> factory, returns a scanner instance - (self, messages: list) -> pre inline logic - (self, messages, output) -> post inline logic

Functions

context_check

context_check(timing: Timing = 'pre', outcome: Outcome = 'active', threshold: float = 0.5, on_error: OnError = 'ignore', message: str = 'Request blocked by context check.', every: int = 0, tier: Literal['static', 'semantic'] = 'static', trigger: float = 0.5) -> Callable

Mark an agent method as a context check.

Works like @guardrail but sets _is_context_check and _context_check_tier on the resulting scanner so that GuardrailMiddleware can emit ContextCheckViolation and ContextCheckEvent instead of guardrail events.

Args: timing: "pre" (check input) or "post" (check output). outcome: "active" (block/retract) or "passive" (notify only). threshold: Score threshold for float-returning rules. on_error: "ignore" (swallow errors) or "raise" (propagate). message: User-facing message when blocked. every: For post rules during streaming -- 0=response-only, N=every N tokens. tier: "static" or "semantic" -- classifies the check type. trigger: Trigger threshold (stored on scanner for downstream use).

discover_context_checks

discover_context_checks(instance: Any) -> tuple[list[Any], list[Any]] | None

Scan an instance for @context_check methods.

Factory methods ((self) signature) are called once to obtain scanner instances. Inline methods are wrapped as scanner-protocol objects.

Returns (pre_scanners, post_scanners) or None if no context checks found.