import textwrap # local_grader: anything callable that takes a prompt and returns text. # we're running a distilled 1B model on the same box as the pipeline - # it doesn't need to be smart, it needs to be fast and not fall over GRADER_PROMPT = textwrap.dedent("""\ A downstream agent is about to receive this account history payload: {payload} It was requested for account_id: {account_id} Answer strictly as JSON: {{"is_plausible": bool, "reason": str, "confidence": float}} Flag it as implausible if the account_id doesn't match, if billing_records is empty for a subscription marked active, or if the payload looks like a default/fallback value rather than a real lookup result. """) def grade_handoff(request_account_id: str, payload: AccountHistoryPayload, local_grader) -> HandoffVerdict: prompt = GRADER_PROMPT.format(payload=payload.model_dump_json(), account_id=request_account_id) raw_response = local_grader(prompt) try: verdict = HandoffVerdict.model_validate_json(raw_response) except ValueError: # the grader itself can return garbage - if we can't parse its verdict, # don't just shrug and let the handoff through, that defeats the point logger.error("Grader returned unparseable output, blocking handoff: %r", raw_response[:200]) return HandoffVerdict(is_plausible=False, reason="grader output unparseable", confidence=0.0) if not verdict.is_plausible or verdict.confidence < CONFIDENCE_FLOOR: logger.warning( "Handoff rejected for account_id=%s: %s (confidence=%.2f)\npayload=%s", request_account_id, verdict.reason, verdict.confidence, payload.model_dump_json(), ) return verdict