Skip to content

interlens.stop.stop_condition

interlens.stop.stop_condition

AnyStopCondition

AnyStopCondition(conditions: list[StopCondition])

Bases: StopCondition

Fires when any member condition fires. run(until=[...]) wraps a list in this.

Source code in src/interlens/stop/stop_condition.py
def __init__(self, conditions: list[StopCondition]):
	self.conditions = list(conditions)

StopCondition

Bases: ABC

A stateful predicate that ends a Conversation.run early.

Each condition is stateful (tracks its own counters) and is checked after every committed turn via should_stop(conversation, last_message). reset() clears state and is called at the start of each run (and branches get fresh copies), so one instance can be reused across runs without leaking state.

A condition may also cap the next generation via turn_cap (e.g. a token budget shrinks the last turn so it lands exactly on budget) and may be installed ambiently as a context manager (with TokenBudget(...): conv.rollout(...) applies it to every conversation in the block). New conditions subclass this without any change to run.

reset

reset() -> None

Clear any accumulated state. Default no-op for stateless conditions.

Source code in src/interlens/stop/stop_condition.py
def reset(self) -> None:
	"""Clear any accumulated state. Default no-op for stateless conditions."""

turn_cap

turn_cap(conversation: 'Conversation') -> int | None

An upper bound on the NEXT turn's generated tokens, or None for no cap. The run loop passes it as max_new_tokens (bounded by the speaker's own cap), so a budget condition can prevent both overshoot and a single turn from consuming the whole allowance. Default: no cap.

Source code in src/interlens/stop/stop_condition.py
def turn_cap(self, conversation: "Conversation") -> int | None:
	"""An upper bound on the NEXT turn's generated tokens, or ``None`` for no cap. The run loop passes it as
	``max_new_tokens`` (bounded by the speaker's own cap), so a budget condition can prevent both overshoot and a
	single turn from consuming the whole allowance. Default: no cap."""
	return None

active_stop_conditions

active_stop_conditions() -> tuple['StopCondition', ...]

The stop conditions currently installed by enclosing with condition: blocks (outermost first).

Source code in src/interlens/stop/stop_condition.py
def active_stop_conditions() -> tuple["StopCondition", ...]:
	"""The stop conditions currently installed by enclosing ``with condition:`` blocks (outermost first)."""
	return _ambient.get()