An agent can ask a model for a final answer in a specific format and receive a plausible response that its code cannot parse. Generative models use probabilistic sampling, so repeated requests may produce different outputs. That variation is a reliability risk at the code–AI interface, but it can also provide alternative paths to a solution. section 5.0 section 6.0
Two agent patterns put this variation to work:
- A Resampler runs the same input through a worker repeatedly, assesses each result against a criterion, and returns the first acceptable one. It gives the worker no feedback about failed trials, so it depends on fresh outputs arising by chance. A trial limit bounds cost and permits an explicit failure. section 5.2
- A Voter samples multiple responses and reconciles them to improve a prediction or estimate confidence, rather than stopping at the first response that passes a criterion. section 6.0
A mathematics agent
A small model may reason through a student’s problem yet fail to deliver the requested final answer in a usable form. A Resampler can retry until an assessor accepts the output. For a harder problem where several reasoning paths may lead to different answers, a Voter can compare separate runs instead. section 5.0 section 5.2 section 6.0
Listing 5.1 resampler.py: Generic resampler
from ..utils import E, IE
from typing import Callable
class Resampler(IE[[str], str]):
def __init__(
self,
worker: IE[[str], str],
assessor: E[[str], str],
branching: Callable[[str], bool],
n_max_trials: int,
):
self.worker = worker
self.assessor = assessor
self.branching = branching
self.n_max_trials = n_max_trials
async def __call__(self, input: str) -> str:
n_trials: int = 0
while n_trials < self.n_max_trials:
result = await self.worker(input)
assessment: str = await self.assessor(result)
criterion_satisfied: bool = self.branching(
assessment
)
if criterion_satisfied:
return result
n_trials += 1
raise ResamplingFailureError(
f"Failed to produce a valid result in {n_trials} trials"
)
class ResamplingFailureError(Exception):
pass
The generic Resampler implementation makes the control flow explicit: invoke the worker, assess its result, branch on the assessment, and raise an error when the trial limit is reached. section 5.2
Retries are not refinement. A Refiner passes assessment feedback to the worker; a Resampler sends the same input again and relies on stochasticity alone. That distinction matters when choosing between a bounded, simple reliability measure and a feedback-driven correction loop. section 5.2.3





