Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

39.5 KB
Raw Blame
"""Classifier runner for versioned AI enrichment labels.

Pure functions only: no persistence, no client construction. Callers build the OpenAI
client via `build_openai_client(product="growth", ai_product="growth", properties=...)` and pass it in —
this module just turns an archived Harmonic payload plus a prompt config into a stamped verdict.
"""

import re
import json
import math
import weakref
import threading
from collections.abc import Callable
from datetime import timedelta
from typing import Any, Literal, TypeIs, cast
from urllib.parse import urlsplit, urlunsplit

from django.conf import settings
from django.db.models import QuerySet
from django.utils import timezone

import httpx
import anthropic
from anthropic.types import Message
from openai import APIConnectionError, InternalServerError, OpenAI, RateLimitError
from openai.types.chat import ChatCompletion, ChatCompletionMessageParam
from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_exponential, wait_random

from posthog.llm.gateway_client import anthropic_gateway_base_url
from posthog.llm.semantic_enrichment import extract_json_object
from posthog.models.organization import Organization, OrganizationMembership

from products.growth.backend.enrichment.tools import TOOLS, TRANSIENT_TOOL_ERRORS, FirecrawlPacer, ToolOutcome, run_tool
from products.growth.backend.models import EnrichmentPromptConfig, OrganizationEnrichmentFetch

UNKNOWN: Literal["unknown"] = "unknown"

# Keys the stored output dict uses for provenance (see classify_payload below).
# validate_output_fields rejects any configured output field that shadows one of these.
RESERVED_OUTPUT_FIELD_KEYS = frozenset({"meta", "inputs"})

# Schema constraints the API validates a config's output_fields against.
OUTPUT_FIELD_KEY_RE = re.compile(r"^[a-z][a-z0-9_]*$")

_TRUE_STRINGS = frozenset({"true", "yes", "y", "1"})
_FALSE_STRINGS = frozenset({"false", "no", "n", "0"})

# Per-call token bounds, both directions. A configured input_fields value can be an arbitrarily
# large payload field (e.g. tagsV2, funding.investors), so without these a single company's
# payload sets the bill for the whole run.
MAX_INPUT_COLUMNS = 40
MAX_INPUT_VALUE_CHARS = 4000
MAX_INPUT_LIST_ITEMS = 50
# The per-value bounds above compose multiplicatively (columns x list items x nested dict keys),
# so this is the one check that actually maps to spend: the serialized total.
MAX_INPUT_TOTAL_CHARS = 60000
# Past this nesting level, _bounded/to_domain stop recursing: deeply nested provider JSON would
# otherwise risk RecursionError inside the worker, which tenacity would then retry at full cost.
MAX_INPUT_DEPTH = 6
# A verdict is a handful of short keys, but on a reasoning model this budget also covers the
# reasoning tokens, which are invisible in the reply and can consume it entirely. It's a cap and
# not a reservation, so the headroom is free.
MAX_OUTPUT_TOKENS = 4000

AI_ENRICHMENT_FEATURE = "ai_enrichment"

# Bounds on the prompt config's own authored content (prompt_text, output_fields) - not the
# archived payload data above. Defined here, not in ai_enrichment_serializers.py, so any other
# consumer of EnrichmentPromptConfig content (e.g. the admin form) can share the same numbers
# instead of drifting to its own.
MAX_PROMPT_TEXT_CHARS = 20_000
MAX_OUTPUT_FIELDS = 20
MAX_OUTPUT_FIELD_DESCRIPTION_CHARS = 400

MAX_TOOL_ROUNDS = 4  # model turns allowed to contain tool calls
MAX_TOOL_CALLS = 4  # tool executions per classification

_TRUNCATED_AT_MAX_DEPTH = "…(truncated: exceeded max input nesting depth)"


class OutputParseError(ValueError):
    """The model's reply didn't satisfy the config's output schema. Deterministic: retrying the
    same prompt against the same config produces the same failure, so it is not retried."""


class PromptConfigError(ValueError):
    """The stored config's output schema is malformed. Deterministic and operator-fixable, not a
    model-reply problem, so it is raised before any LLM call rather than retried. This module
    stays free of the management-command layer (see the module docstring), so callers that need a
    CLI-style CommandError translate this at their own boundary."""


class TransientToolError(Exception):
    """A tool hit a retryable Firecrawl condition; the caller must defer the org rather than
    store a verdict."""


def verdict_field_key(config: EnrichmentPromptConfig) -> str | None:
    """Which output key represents pass/fail for unknown/summary accounting (the batch runner,
    the dry-run command): the first boolean-typed output field. None if the schema has no
    boolean field at all. Never config.name - the label is a human name, not a data key."""
    for field in config.output_fields:
        if field.get("type") == "boolean":
            return cast(str, field["key"])
    return None


_EMAIL_RE = re.compile(r"[^@\s]+@[^@\s]+\.[^@\s]+")


def to_domain(value: Any, depth: int = 0) -> Any:
    """Reduce a value that is an email address end to end to its domain, at any depth.

    Applied to every value on its way to the prompt, because the values also land in
    EnrichmentLabelResult.inputs and stay there: a provider field that happens to hold a personal
    address would otherwise be stored indefinitely. The local part carries no classification
    signal anyway - only the company domain does.

    Only a full string match on the email shape reduces - a merely-contains-"@" check would mangle
    free text like "we build AI @scale" or "contact [email protected] for a demo" beyond recognition.

    Recurses because the values are not all scalars: tagsV2 and funding.investors are lists, so a
    top-level-only check leaves nested addresses through. depth caps that recursion so a
    pathologically nested payload can't raise RecursionError.
    """
    if depth >= MAX_INPUT_DEPTH:
        return _TRUNCATED_AT_MAX_DEPTH
    if isinstance(value, str):
        return value.rsplit("@", 1)[1].lower() if _EMAIL_RE.fullmatch(value) else value
    if isinstance(value, dict):
        return {key: to_domain(item, depth + 1) for key, item in value.items()}
    if isinstance(value, list):
        return [to_domain(item, depth + 1) for item in value]
    return value


def extract_input_fields(payload: dict[str, Any], input_fields: list[str]) -> dict[str, Any]:
    """Resolve dotted paths into the archived payload.

    Keyed by the full dotted path so the LLM prompt shows provenance. Missing paths,
    None values, and paths that traverse through a non-dict are omitted rather than
    included as null: the prompt should only see what's actually known.
    """
    result: dict[str, Any] = {}
    for path in input_fields:
        value: Any = payload
        for part in path.split("."):
            if not isinstance(value, dict):
                value = None
                break
            value = value.get(part)
        if value is not None:
            result[path] = to_domain(value)
    return result


def _output_instruction(config: EnrichmentPromptConfig) -> str:
    fields_desc = ", ".join(
        f'"{field["key"]}" ({field["type"]}'
        # Stated rather than left implicit: a value outside the range is rejected, and asking for
        # it without saying so is how a config that wants 0-100 fails every row.
        + (f", between {rng[0]} and {rng[1]}" if (rng := field_range(field)) else "")
        + (f", meaning: {field['description']}" if field.get("description") else "")
        + ")"
        for field in config.output_fields
    )
    return f"a JSON object with exactly these keys: {fields_desc}."


def _bounded(value: Any, depth: int = 0) -> Any:
    """Cap a single value's contribution to the prompt. An input_fields value can be an
    arbitrarily long string or list (e.g. description, tagsV2), and one company payload with a
    200 KB description would otherwise set the bill for the whole run.

    Recurses for the same reason to_domain does: several provider fields hold lists of objects, so
    capping only the top level lets an oversized nested string through to the prompt and into the
    stored inputs. depth caps that recursion so a pathologically nested payload can't raise
    RecursionError; past it we return a placeholder instead of descending further.
    """
    if depth >= MAX_INPUT_DEPTH:
        return _TRUNCATED_AT_MAX_DEPTH
    if isinstance(value, str):
        return value[:MAX_INPUT_VALUE_CHARS] + "…" if len(value) > MAX_INPUT_VALUE_CHARS else value
    if isinstance(value, dict):
        return {key: _bounded(item, depth + 1) for key, item in list(value.items())[:MAX_INPUT_LIST_ITEMS]}
    if isinstance(value, list):
        return [_bounded(item, depth + 1) for item in value[:MAX_INPUT_LIST_ITEMS]]
    return value


def bound_inputs(inputs: dict[str, Any]) -> dict[str, Any]:
    """Cap what one company can contribute, in both directions.

    Applied once, before the prompt is built, so the same dict is what the model sees and what
    EnrichmentLabelResult.inputs stores. Bounding inside build_messages instead would leave the
    stored record claiming an untruncated value was sent.

    The per-value bounding above composes multiplicatively (columns x list items x nested dict
    keys), so it caps nothing on its own - the serialized-size check below is the one bound that
    actually maps to spend, and drops trailing columns until the whole payload fits.
    """
    bounded = {key: _bounded(value) for key, value in list(inputs.items())[:MAX_INPUT_COLUMNS]}
    while bounded and len(json.dumps(bounded)) > MAX_INPUT_TOTAL_CHARS:
        bounded.popitem()
    return bounded


def bounding_report(original: dict[str, Any], bounded: dict[str, Any]) -> dict[str, list[str]]:
    """Which columns bound_inputs altered, for meta.bounded.

    Only strings carry a visible "…" once truncated; a dropped column and a shortened list leave
    no trace at all, so a stored tagsV2 of exactly MAX_INPUT_LIST_ITEMS reads identically to a
    truncated one. inputs is the durable record of what the model saw, so anyone re-scoring a
    verdict needs to know the record is partial.

    Compares the two dicts rather than instrumenting the recursion: dropped keys are an exact set
    difference, and a surviving column that serializes shorter was capped somewhere inside it.
    """
    dropped = [key for key in original if key not in bounded]
    truncated = [key for key, value in bounded.items() if len(json.dumps(value)) != len(json.dumps(original[key]))]
    report: dict[str, list[str]] = {}
    if dropped:
        report["dropped"] = dropped
    if truncated:
        report["truncated"] = truncated
    return report


def build_messages(
    config: EnrichmentPromptConfig, inputs: dict[str, Any], signup_domain: str | None
) -> list[dict[str, str]]:
    # .replace, not .format: prompt_text is free-form and may itself contain braces.
    # Domain only, never the full address: the signup email's local part is personal data
    # with no classification signal, and nothing else internal sends PII to the gateway.
    system = config.prompt_text.replace("{email}", signup_domain or "unknown")
    user = "Company data:\n" + json.dumps(inputs, indent=2) + "\n\nRespond with " + _output_instruction(config)
    return [
        {"role": "system", "content": system},
        {"role": "user", "content": user},
    ]


def _coerce_bool(value: Any) -> bool:
    """Anything the model might plausibly mean by true/false, and nothing else.

    Mapping every unrecognized string to False turned "maybe" and "n/a" into a confident negative
    verdict with no error, which is the outcome the unknown path exists to avoid.
    """
    if isinstance(value, bool):
        return value
    if isinstance(value, int | float):
        return bool(value)
    if isinstance(value, str):
        text = value.strip().lower()
        if text in _TRUE_STRINGS:
            return True
        if text in _FALSE_STRINGS:
            return False
    raise OutputParseError(f"{value!r} is not a recognizable boolean")


def _coerce_number(value: Any) -> float:
    number = float(value)
    # json.dumps writes bare NaN/Infinity, which isn't valid JSON: it breaks the run stream's
    # JSON.parse in the browser and any downstream reader of the stored output.
    if not math.isfinite(number):
        raise OutputParseError(f"{value!r} is not a finite number")
    return number


def _coerce_str(value: Any) -> str:
    """None and list/dict are rejected rather than stringified: `str(None)` and a list/dict's repr
    are both indistinguishable from a model that genuinely wrote "None" or a Python literal, which
    is exactly the fail-silent outcome _coerce_bool was hardened against for booleans."""
    if value is None or isinstance(value, list | dict):
        raise OutputParseError(f"{value!r} is not a string-coercible scalar")
    return str(value)


_OUTPUT_FIELD_COERCERS: dict[str, Callable[[Any], Any]] = {
    "boolean": _coerce_bool,
    "number": _coerce_number,
    "string": _coerce_str,
}

# Derived from the coercer table so the API can never accept a type this module has no coercer for.
OUTPUT_FIELD_TYPES = tuple(_OUTPUT_FIELD_COERCERS)

# Default range for these keys when a field declares no explicit min/max, so configs written
# before ranges were expressible keep their old behavior.
_UNIT_INTERVAL_KEYS = frozenset({"confidence"})


def field_range(field: dict[str, Any]) -> tuple[float, float] | None:
    """The declared numeric range for an output field, or the legacy key-name default."""
    if field.get("min") is not None and field.get("max") is not None:
        return float(field["min"]), float(field["max"])
    return (0.0, 1.0) if field.get("key") in _UNIT_INTERVAL_KEYS else None


def validate_input_fields(config: EnrichmentPromptConfig) -> None:
    """More input_fields than bound_inputs will carry means the tail is dropped from both the
    prompt and the stored record, with nothing in either to say so. Refusing the config is the
    only point where that's still fixable."""
    if len(config.input_fields) > MAX_INPUT_COLUMNS:
        raise PromptConfigError(
            f"enrichment config declares {len(config.input_fields)} input fields, "
            f"more than the {MAX_INPUT_COLUMNS} that reach the prompt"
        )


def validate_output_fields(config: EnrichmentPromptConfig) -> None:
    """Checked once, before any LLM call: a config typo or omission is deterministic per org, so
    without this it would only surface after paying for a call, and then get retried three times
    against a schema it could never satisfy - the exact amplification the retry predicate on
    OutputParseError exists to prevent. Raises PromptConfigError, not OutputParseError, since this
    is a config problem, not a model reply problem."""
    for field in config.output_fields:
        key = field.get("key")
        field_type = field.get("type")
        if not key or not field_type:
            raise PromptConfigError(f"enrichment output field {field!r} is missing a 'key' or 'type'")
        if key in RESERVED_OUTPUT_FIELD_KEYS:
            raise PromptConfigError(
                f"enrichment output field {key!r} shadows a reserved key {sorted(RESERVED_OUTPUT_FIELD_KEYS)}"
            )
        if field_type not in _OUTPUT_FIELD_COERCERS:
            raise PromptConfigError(f"enrichment output field {key!r} has unknown type {field_type!r}")
        if (field.get("min") is None) != (field.get("max") is None):
            raise PromptConfigError(f"enrichment output field {key!r} declares only one of 'min' and 'max'")
        if field.get("min") is not None:
            if field_type != "number":
                raise PromptConfigError(f"enrichment output field {key!r} has a range but is not a number")
            try:
                low, high = float(field["min"]), float(field["max"])
            except (TypeError, ValueError) as e:
                raise PromptConfigError(f"enrichment output field {key!r} has a non-numeric range") from e
            if low > high:
                raise PromptConfigError(f"enrichment output field {key!r} has min {low} above max {high}")


_PLAIN_HOST = re.compile(r"[a-z0-9-]+(\.[a-z0-9-]+)+")


def _normalize_url(url: str) -> str:
    parts = urlsplit(url)
    normalized = urlunsplit(parts._replace(scheme=parts.scheme.lower(), netloc=parts.netloc.lower(), fragment=""))
    return normalized[:-1] if normalized.endswith("/") else normalized


def _without_query(url: str) -> str:
    parts = urlsplit(url)
    return urlunsplit((parts.scheme, parts.netloc, parts.path, "", ""))


def _plain_host(url: str) -> str | None:
    """Firecrawl's URL parser can split a host where urlsplit keeps it whole, so only a host both
    read the same way is compared against the allowlist."""
    if "\\" in url or not url.isascii() or not url.isprintable():
        return None
    host = urlsplit(url).hostname
    if host is None:
        return None
    host = host.lower()
    return host if _PLAIN_HOST.fullmatch(host) else None


def _on_signup_domain(host: str, signup_domain: str | None) -> bool:
    if signup_domain is None:
        return False
    domain = signup_domain.lower()
    return host == domain or host.endswith(f".{domain}")


def _url_is_trusted(url: str, presented: set[str], signup_domain: str | None) -> bool:
    """Whether url is one a tool call actually surfaced, or the company's own domain - never a
    URL a page's own text could have steered the model into asking for."""
    host = _plain_host(url)
    if host is None:
        return False
    return _normalize_url(url) in presented or _on_signup_domain(host, signup_domain)


def _fetch_url_for(url: str, presented: set[str], signup_domain: str | None) -> str | None:
    """Page text can steer the model into putting the organization's inputs in a query string, so the
    fetched URL is the stored search result or the signup domain page with no query."""
    host = _plain_host(url)
    if host is None:
        return None
    presented_by_page = {_normalize_url(_without_query(candidate)): candidate for candidate in presented}
    page = _normalize_url(_without_query(url))
    if page in presented_by_page:
        return presented_by_page[page]
    return _without_query(url) if _on_signup_domain(host, signup_domain) else None


def _parse_custom_output(config: EnrichmentPromptConfig, data: dict[str, Any]) -> dict[str, Any]:
    """Validate presence and coerce basic types for a configurable output schema — the stored
    output ends up with exactly the configured keys, nothing more.

    validate_output_fields already runs before any of this, so field/type shape is guaranteed —
    the .get() lookup here is belt-and-braces, not the primary line of defence.
    """
    output: dict[str, Any] = {}
    for field in config.output_fields:
        key = field["key"]
        if key not in data:
            raise OutputParseError(f"LLM response is missing the {key!r} key")
        coercer = _OUTPUT_FIELD_COERCERS.get(field.get("type"))
        if coercer is None:
            raise OutputParseError(f"LLM response key {key!r} has unrecognized output type {field.get('type')!r}")
        allowed = field_range(field)
        try:
            value = coercer(data[key])
            # Bare message: the except clause below owns the "LLM response key ..." prefix.
            if allowed is not None and not allowed[0] <= value <= allowed[1]:
                raise OutputParseError(f"{value} is outside the {allowed[0]}-{allowed[1]} range")
        except OutputParseError as e:
            raise OutputParseError(f"LLM response key {key!r}: {e}") from e
        except (TypeError, ValueError) as e:
            raise OutputParseError(f"LLM response key {key!r} could not be coerced to {field['type']}: {e}") from e
        output[key] = value
    return output


def _accumulate_meta(combined: dict[str, Any], turn: dict[str, Any]) -> None:
    for key in ("prompt_tokens", "completion_tokens"):
        if key in turn:
            combined[key] = combined.get(key, 0) + turn[key]
    for key in ("response_model", "system_fingerprint", "finish_reason"):
        if key in turn:
            combined[key] = turn[key]


def _run_tool_call(
    call: Any, *, presented_urls: set[str], signup_domain: str | None, pacer: FirecrawlPacer | None
) -> ToolOutcome:
    try:
        arguments = json.loads(call.function.arguments or "{}")
    except (TypeError, ValueError):
        return ToolOutcome(
            name=call.function.name,
            arguments={},
            result={"error": "arguments were not valid JSON"},
            urls=(),
            error="bad_arguments",
        )
    if not isinstance(arguments, dict):
        return ToolOutcome(
            name=call.function.name,
            arguments={},
            result={"error": "arguments must be an object"},
            urls=(),
            error="bad_arguments",
        )
    # A missing or non-string url falls through so fetch_page reports it as bad_arguments.
    if call.function.name == "fetch_page" and isinstance(arguments.get("url"), str):
        fetch_url = _fetch_url_for(arguments["url"], presented_urls, signup_domain)
        if fetch_url is None:
            return ToolOutcome(
                name="fetch_page",
                arguments=arguments,
                result={"error": "url must come from a search result or the company's own domain"},
                urls=(),
                error="invalid_url",
            )
        arguments["url"] = fetch_url
    return run_tool(call.function.name, arguments, pacer=pacer)


_RETRYABLE_ERRORS = (
    APIConnectionError,
    RateLimitError,
    InternalServerError,
    anthropic.APIConnectionError,
    anthropic.RateLimitError,
    anthropic.InternalServerError,
)

_ANTHROPIC_TOOLS = [
    {
        "name": tool["function"]["name"],
        "description": tool["function"]["description"],
        "input_schema": tool["function"]["parameters"],
    }
    for tool in TOOLS
]

_anthropic_clients: "weakref.WeakKeyDictionary[OpenAI, anthropic.Anthropic]" = weakref.WeakKeyDictionary()
_anthropic_clients_lock = threading.Lock()


@retry(
    # Allowlist, not a blacklist: only failures that can plausibly succeed on a retry earn one.
    # APIConnectionError covers its APITimeoutError subclass too. Everything else — most
    # importantly AuthenticationError and OutputParseError — fails identically on every attempt,
    # so retrying it just triples the spend for the same outcome. This also stops tenacity's
    # three attempts from stacking with the SDK's own retries on a hard failure like a bad
    # gateway credential; see max_retries=0 at every client construction site for the other half
    # of that fix.
    retry=retry_if_exception_type(_RETRYABLE_ERRORS),
    stop=stop_after_attempt(3),
    # Jitter so a worker pool that all hit a 429 together doesn't retry in lockstep.
    wait=wait_exponential(multiplier=1, min=2, max=30) + wait_random(0, 2),
    reraise=True,
)
def _complete(client: OpenAI, request: dict[str, Any]) -> ChatCompletion:
    if _routes_through_anthropic_messages(client, request["model"]):
        return _complete_with_anthropic_messages(client, request)
    return client.chat.completions.create(**request)


def _routes_through_anthropic_messages(client: OpenAI, model: str) -> bool:
    if not model.startswith("claude-"):
        return False
    if not settings.AI_GATEWAY_URL:
        return False
    return str(client.base_url).rstrip("/") == settings.AI_GATEWAY_URL.rstrip("/")


def _anthropic_client_for(client: OpenAI) -> anthropic.Anthropic:
    with _anthropic_clients_lock:
        anthropic_client = _anthropic_clients.get(client)
        if anthropic_client is None:
            anthropic_client = anthropic.Anthropic(
                api_key=client.api_key,
                base_url=anthropic_gateway_base_url(str(client.base_url)),
                default_headers={
                    name: value
                    for name, value in client.default_headers.items()
                    if name.lower().startswith("x-posthog-") and isinstance(value, str)
                },
                max_retries=client.max_retries,
                http_client=httpx.Client(trust_env=False),
            )
            _anthropic_clients[client] = anthropic_client
        return anthropic_client


def _complete_with_anthropic_messages(client: OpenAI, request: dict[str, Any]) -> ChatCompletion:
    message = _anthropic_client_for(client).messages.create(**_anthropic_request(request))
    return _chat_completion_from_message(message)


def _anthropic_request(request: dict[str, Any]) -> dict[str, Any]:
    system, *history = request["messages"]
    assert system["role"] == "system"
    assert history
    messages: list[dict[str, Any]] = []
    for m in history:
        if m["role"] == "tool":
            result = {"type": "tool_result", "tool_use_id": m["tool_call_id"], "content": m["content"]}
            if messages[-1]["role"] == "user":
                messages[-1]["content"].append(result)
            else:
                messages.append({"role": "user", "content": [result]})
            continue
        if m["role"] == "assistant" and m.get("tool_calls"):
            blocks: list[dict[str, Any]] = [{"type": "text", "text": m["content"]}] if m.get("content") else []
            blocks.extend(
                {
                    "type": "tool_use",
                    "id": call["id"],
                    "name": call["function"]["name"],
                    "input": json.loads(call["function"]["arguments"]),
                }
                for call in m["tool_calls"]
            )
            messages.append({"role": "assistant", "content": blocks})
            continue
        messages.append({"role": m["role"], "content": m["content"]})
    anthropic_request: dict[str, Any] = {
        "model": request["model"],
        "system": system["content"],
        "messages": messages,
        "max_tokens": request["max_completion_tokens"],
        "timeout": request["timeout"],
    }
    if "tools" in request:
        assert request["tools"] is TOOLS
        anthropic_request["tools"] = _ANTHROPIC_TOOLS
    elif any(m["role"] == "tool" for m in history):
        anthropic_request["tools"] = _ANTHROPIC_TOOLS
        anthropic_request["tool_choice"] = {"type": "none"}
    return anthropic_request


_ANTHROPIC_FINISH_REASONS = {
    "end_turn": "stop",
    "stop_sequence": "stop",
    "max_tokens": "length",
    "tool_use": "tool_calls",
}


def _chat_completion_from_message(message: Message) -> ChatCompletion:
    text = "".join(block.text for block in message.content if block.type == "text")
    tool_calls = [
        {"id": block.id, "type": "function", "function": {"name": block.name, "arguments": json.dumps(block.input)}}
        for block in message.content
        if block.type == "tool_use"
    ]
    return ChatCompletion.model_validate(
        {
            "id": message.id,
            "object": "chat.completion",
            "created": int(timezone.now().timestamp()),
            "model": message.model,
            "choices": [
                {
                    "index": 0,
                    "finish_reason": _ANTHROPIC_FINISH_REASONS.get(message.stop_reason or "", "stop"),
                    "message": {"role": "assistant", "content": text or None, "tool_calls": tool_calls or None},
                }
            ],
            "usage": {
                "prompt_tokens": message.usage.input_tokens,
                "completion_tokens": message.usage.output_tokens,
                "total_tokens": message.usage.input_tokens + message.usage.output_tokens,
            },
        }
    )


def _call_and_parse(
    config: EnrichmentPromptConfig,
    messages: list[dict[str, Any]],
    client: OpenAI,
    *,
    signup_domain: str | None,
    pacer: FirecrawlPacer | None,
) -> tuple[dict[str, Any], dict[str, Any]]:
    meta: dict[str, Any] = {}
    tool_log: list[dict[str, Any]] = []
    tool_urls: set[str] = set()
    tool_calls_used = 0
    tool_rounds_used = 0

    while True:
        request: dict[str, Any] = {
            "model": config.model,
            "messages": cast(list[ChatCompletionMessageParam], messages),
            "response_format": {"type": "json_object"},
            # Not max_tokens: the OpenAI API rejects it for gpt-5 and o-series models, and
            # config.model is an operator-editable row so the family isn't known here. The
            # gateway's litellm normalizes this one for every route it serves, including
            # Anthropic's native max_tokens.
            "max_completion_tokens": MAX_OUTPUT_TOKENS,
            "timeout": 60,
        }
        if tool_calls_used < MAX_TOOL_CALLS:
            request["tools"] = TOOLS
            request["tool_choice"] = "auto"
        response = _complete(client, request)
        _accumulate_meta(meta, _response_meta(response))
        # Content filtering and some upstream routes reply with an empty choices list; indexing
        # it unguarded raises IndexError, which (unlike OutputParseError) tenacity retries at
        # full cost for a response shape that will not change.
        if not response.choices:
            raise OutputParseError("LLM response had no choices (likely content filtering)")
        choice = response.choices[0]
        if choice.finish_reason == "length":
            raise OutputParseError("response truncated at max_completion_tokens")
        message = choice.message
        tool_calls = getattr(message, "tool_calls", None)
        if isinstance(tool_calls, list) and tool_calls:
            if tool_rounds_used >= MAX_TOOL_ROUNDS:
                raise OutputParseError("no final answer after tool rounds")
            tool_rounds_used += 1
            messages.append(
                {
                    "role": "assistant",
                    "content": message.content,
                    "tool_calls": [
                        {
                            "id": call.id,
                            "type": "function",
                            "function": {"name": call.function.name, "arguments": call.function.arguments},
                        }
                        for call in tool_calls
                    ],
                }
            )
            for call in tool_calls:
                if tool_calls_used >= MAX_TOOL_CALLS:
                    messages.append(
                        {
                            "role": "tool",
                            "tool_call_id": call.id,
                            "content": json.dumps({"error": "tool budget exhausted"}),
                        }
                    )
                    continue
                tool_calls_used += 1
                outcome = _run_tool_call(call, presented_urls=tool_urls, signup_domain=signup_domain, pacer=pacer)
                tool_log.append(
                    {
                        "name": outcome.name,
                        "arguments": outcome.arguments,
                        "result": outcome.result,
                        "error": outcome.error,
                    }
                )
                tool_urls.update(_normalize_url(url) for url in outcome.urls)
                messages.append({"role": "tool", "tool_call_id": call.id, "content": json.dumps(outcome.result)})
                if outcome.error in TRANSIENT_TOOL_ERRORS:
                    raise TransientToolError(f"tool {outcome.name!r} hit a transient error: {outcome.error}")
            continue

        # Shared with the other products that talk to the gateway: response_format isn't reliably
        # honored on the Anthropic route, so the reply can arrive fenced or wrapped in prose.
        data = extract_json_object(message.content or "")
        if data is None:
            raise OutputParseError("LLM response was not a JSON object")
        output = _parse_custom_output(config, data)
        if tool_log:
            meta["tool_calls"] = tool_log
            meta["tool_urls"] = sorted(tool_urls)
        return output, meta


def _response_meta(response: Any) -> dict[str, Any]:
    """What the gateway actually served, stamped per result: config.model is an alias the
    provider can silently repoint, and usage tokens are the raw input to spend-per-config."""
    meta: dict[str, Any] = {}
    served_model = getattr(response, "model", None)
    if isinstance(served_model, str):
        meta["response_model"] = served_model
    fingerprint = getattr(response, "system_fingerprint", None)
    if isinstance(fingerprint, str):
        meta["system_fingerprint"] = fingerprint
    usage = getattr(response, "usage", None)
    prompt_tokens = getattr(usage, "prompt_tokens", None)
    completion_tokens = getattr(usage, "completion_tokens", None)
    if isinstance(prompt_tokens, int) and isinstance(completion_tokens, int):
        meta["prompt_tokens"] = prompt_tokens
        meta["completion_tokens"] = completion_tokens
    choices = getattr(response, "choices", None)
    finish_reason = getattr(choices[0], "finish_reason", None) if choices else None
    if isinstance(finish_reason, str):
        meta["finish_reason"] = finish_reason
    return meta


def unknown_output(config: EnrichmentPromptConfig, signup_domain: str | None, reason: str) -> dict[str, Any]:
    """Only the configured keys, same as a real call: why the run was skipped goes in meta, which
    is reserved for provenance, because a schema need not have a "reasoning" key. Public: besides
    classify_payload's own short-circuits below, enrichment/lab.py's classify_fetch_for_run calls
    this directly for its own consent-revoked-mid-run short-circuit, which must never reach
    classify_payload (and therefore the LLM) in the first place.
    """
    output: dict[str, Any] = {}
    verdict_key = verdict_field_key(config)
    if verdict_key is not None:
        output[verdict_key] = UNKNOWN
    output["meta"] = {"skipped": reason}
    output["inputs"] = {"signup_domain": signup_domain, "fields": {}}
    return output


def is_unknown_output(output: dict[str, Any]) -> bool:
    """The authoritative "was this skipped" marker for accounting. verdict_field_key can be None
    (a schema with no boolean field), in which case unknown_output writes no verdict key at all -
    so callers that count unknowns by checking a verdict key miss every skipped row under such a
    schema. meta.skipped is set on every unknown_output call regardless of schema shape."""
    return bool(output.get("meta", {}).get("skipped"))


def has_usable_payload(payload: dict[str, Any] | None) -> TypeIs[dict[str, Any]]:
    """Whether classify_payload will look at payload's fields at all, rather than
    short-circuiting straight to unknown_output for a missing or not-found archived fetch."""
    if not payload:
        return False
    return payload.get("companyFound") is not False


def _reject_unsupported_evidence_url(
    output: dict[str, Any], signup_domain: str | None, presented: set[str], meta: dict[str, Any]
) -> None:
    """Nulls an evidence_url the model could not actually have seen: not a tool result it was
    shown and not the signup domain itself, rather than failing the whole verdict over one bad
    citation."""
    evidence_url = output.get("evidence_url")
    if not evidence_url or not isinstance(evidence_url, str):
        return
    if _url_is_trusted(evidence_url, presented, signup_domain):
        return
    meta["evidence_url_rejected"] = evidence_url
    output["evidence_url"] = None


def _stored_tool_result(result: dict[str, Any]) -> dict[str, Any]:
    """The verdict keeps a fetched page's url and size, never its text: the evidence_url is the
    audit pointer, and the page can be fetched again."""
    stored = {key: value for key, value in result.items() if key not in ("note", "markdown")}
    if "markdown" in result:
        stored["chars"] = len(result["markdown"])
    return _bounded(stored)


def classify_payload(
    config: EnrichmentPromptConfig,
    payload: dict[str, Any] | None,
    signup_domain: str | None,
    client: OpenAI,
    *,
    pacer: FirecrawlPacer | None = None,
) -> dict[str, Any]:
    validate_input_fields(config)
    validate_output_fields(config)
    if not has_usable_payload(payload):
        return unknown_output(config, signup_domain, "missing or empty archived payload")

    # Checked after resolving, not before: a payload that's present but has none of the configured
    # paths would otherwise bill a call to ask the model about "Company data: {}".
    extracted = extract_input_fields(payload, config.input_fields)
    inputs = bound_inputs(extracted)
    if not inputs:
        return unknown_output(config, signup_domain, "archived payload has none of the configured input fields")

    messages = build_messages(config, inputs, signup_domain)
    output, meta = _call_and_parse(config, messages, client, signup_domain=signup_domain, pacer=pacer)
    tool_calls = meta.pop("tool_calls", None)
    _reject_unsupported_evidence_url(output, signup_domain, set(meta.get("tool_urls", ())), meta)
    inputs_record: dict[str, Any] = {"signup_domain": signup_domain, "fields": inputs}
    output["inputs"] = inputs_record
    if tool_calls:
        inputs_record["tool_calls"] = [
            {"name": call["name"], "arguments": call["arguments"], "result": _stored_tool_result(call["result"])}
            for call in tool_calls
        ]
        meta["tool_calls"] = [
            {"name": call["name"], "arguments": call["arguments"], "error": call["error"]} for call in tool_calls
        ]
    bounded = bounding_report(extracted, inputs)
    if bounded:
        meta["bounded"] = bounded
    if meta:
        output["meta"] = meta
    return output


_HOSTNAME_RE = re.compile(r"[a-z0-9.-]+\.[a-z]{2,}")


def ai_processing_approved(organization_id: Any) -> bool:
    """Whether this org allows its data to be sent to an LLM, read fresh rather than off an
    Organization loaded earlier.

    A full-archive run spans hours, so an admin revoking consent partway through must be honored
    for every org still queued, not just the ones enumerated after the change. Only an explicit
    True approves: the column is nullable, and the rest of the codebase treats unset as unapproved
    (posthog/temporal/ai/sync_vectors.py).
    """
    return Organization.objects.filter(pk=organization_id, is_ai_data_processing_approved=True).exists()


def signup_domain_for_organization(organization: Organization) -> str | None:
    """Earliest member's email domain, standing in for the signup company identity."""
    membership = (
        OrganizationMembership.objects.filter(organization=organization)
        .select_related("user")
        .order_by("joined_at")
        .first()
    )
    if membership is None or not membership.user.email or "@" not in membership.user.email:
        return None
    domain = membership.user.email.rsplit("@", 1)[1].lower()
    # User.email is only validated through full_clean()/serializers, so rows written by
    # createsuperuser, imports, or backfills can hold anything after the "@" - and this domain is
    # interpolated into the system prompt's highest-trust segment via build_messages.
    return domain if _HOSTNAME_RE.fullmatch(domain) else None


def latest_fetches_qs() -> QuerySet[OrganizationEnrichmentFetch]:
    """One row per org: its most recent archived fetch."""
    return OrganizationEnrichmentFetch.objects.order_by("organization_id", "-fetched_at", "-id").distinct(
        "organization_id"
    )


def recent_latest_fetches_qs(lookback_days: int | None = None) -> QuerySet[OrganizationEnrichmentFetch]:
    """latest_fetches_qs, but orderable and sliceable: DISTINCT ON pins the inner
    ORDER BY to organization_id, so callers wanting `-fetched_at` need this subquery wrapper."""
    fetches = OrganizationEnrichmentFetch.objects.filter(id__in=latest_fetches_qs().values("id")).order_by(
        "-fetched_at"
    )
    if lookback_days is None:
        return fetches
    return fetches.filter(fetched_at__gte=timezone.now() - timedelta(days=lookback_days))


def get_active_config(label: str) -> EnrichmentPromptConfig | None:
    return EnrichmentPromptConfig.objects.filter(name=label, is_active=True).first()