Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

13.2 KB
Raw Blame
"""Shared utilities for LLM provider construction.

These helpers were previously inlined in ``config/llm_config.get_llm``'s
procedural ``if/elif`` chain (which is now removed). They live here so
provider-class ``create_llm`` methods are the single source of truth for
LLM construction logic — no per-provider drift.
"""

import math
from typing import Any, Final

from ...settings.exceptions import SettingsValueError


DEFAULT_LOCAL_CONTEXT_WINDOW_SIZE = 8192
DEFAULT_CLOUD_CONTEXT_WINDOW_SIZE = 128000
LOCAL_PROVIDERS = ("ollama", "llamacpp", "lmstudio")
# compute_max_tokens caps llm.max_tokens at this share of a known context
# window, which leaves the rest of the window for the prompt.
MAX_TOKENS_CONTEXT_SHARE = 0.8

# ``llm.request_timeout`` is a *per-network-operation* bound, not a cap on
# the total wall-clock duration of a response. httpx (which every provider
# SDK here sits on) has no total-deadline knob: a scalar expands to
# ``Timeout(connect=X, read=X, write=X, pool=X)`` where ``read`` is the
# time allowed *between* received bytes. A provider that keeps dripping
# data therefore keeps the request alive past X. See
# ``docs/CONFIGURATION.md`` — the setting is documented as an inactivity
# bound for exactly this reason.
DEFAULT_REQUEST_TIMEOUT: Final = 1800
MIN_REQUEST_TIMEOUT: Final = 1
# Deliberately no ceiling: a long-running local model on modest hardware
# can legitimately think for hours, and a cap that is generous enough for
# those is not a meaningful guard against a typo anyway. Non-finite values
# are still rejected below, which is what actually prevents an unbounded
# wait; MAX_MAX_RETRIES stays, because retries MULTIPLY this bound.

# Connecting is not inference: a blackholed IP should fail fast instead of
# holding a worker for the full inference budget. Matches the OpenAI and
# Anthropic SDK defaults (``Timeout(connect=5.0, ...)``), which a scalar
# timeout would otherwise overwrite.
CONNECT_TIMEOUT_SECONDS: Final = 5.0

# Limit the number of SDK retries. Each attempt has its own per-operation
# timeouts, with SDK backoff or server-directed delays between attempts.
DEFAULT_MAX_RETRIES: Final = 2
MIN_MAX_RETRIES: Final = 0
MAX_MAX_RETRIES: Final = 5

MODEL_DISCOVERY_TIMEOUT_SECONDS: Final = 30


def get_local_context_window(settings_snapshot=None):
    """Resolve ``llm.local_context_window_size``, the local providers' window."""
    from ...config.thread_settings import get_setting_from_snapshot

    window_size = get_setting_from_snapshot(
        "llm.local_context_window_size",
        DEFAULT_LOCAL_CONTEXT_WINDOW_SIZE,
        settings_snapshot=settings_snapshot,
    )
    return (
        int(window_size)
        if window_size is not None
        else DEFAULT_LOCAL_CONTEXT_WINDOW_SIZE
    )


def get_context_window_for_provider(provider_type, settings_snapshot=None):
    """Resolve effective context window size for a provider.

    Local providers (ollama, llamacpp, lmstudio) use a smaller default to
    prevent memory issues. Cloud providers respect
    ``llm.context_window_unrestricted`` and return ``None`` when
    unrestricted (the provider auto-handles its own context window).
    """
    from ...config.thread_settings import get_setting_from_snapshot

    if provider_type in LOCAL_PROVIDERS:
        return get_local_context_window(settings_snapshot)
    use_unrestricted = get_setting_from_snapshot(
        "llm.context_window_unrestricted",
        True,
        settings_snapshot=settings_snapshot,
    )
    if use_unrestricted:
        return None
    window_size = get_setting_from_snapshot(
        "llm.context_window_size",
        DEFAULT_CLOUD_CONTEXT_WINDOW_SIZE,
        settings_snapshot=settings_snapshot,
    )
    return (
        int(window_size)
        if window_size is not None
        else DEFAULT_CLOUD_CONTEXT_WINDOW_SIZE
    )


def compute_max_tokens(
    settings_snapshot=None, context_window_size=None
) -> int | None:
    """Resolve effective max_tokens for ``ChatXxx(max_tokens=...)``.

    Caps at 80% of ``context_window_size`` (when provided) to leave room
    for the prompt. Returns ``None`` (caller should omit the kwarg) when:

    - ``llm.supports_max_tokens`` is False, OR
    - ``llm.max_tokens`` is unset / explicitly None in the snapshot.

    Omitting the kwarg when the setting is absent matches the pre-refactor
    live-class behavior (the provider SDK's own default applies); a
    hardcoded fallback like the dead chain's 100000 exceeds the output
    limit of most cloud models. Production users have ``llm.max_tokens``
    populated from ``default_settings.json`` (currently 30000), so the
    unset branch only fires for partial-snapshot programmatic callers.

    ``llm.max_tokens`` is read with an explicit ``None`` default (#5984),
    so an absent key resolves to ``None`` (kwarg omitted) instead of
    raising ``NoSettingsContextError``. Provider-level ``except
    NoSettingsContextError`` wrappers around this helper are defensive
    only.
    """
    from ...config.thread_settings import get_setting_from_snapshot

    if not get_setting_from_snapshot(
        "llm.supports_max_tokens",
        True,
        settings_snapshot=settings_snapshot,
    ):
        return None
    raw = get_setting_from_snapshot(
        "llm.max_tokens",
        default=None,
        settings_snapshot=settings_snapshot,
    )
    if raw is None:
        return None
    max_tokens = int(raw)
    if context_window_size is not None:
        max_tokens = min(
            max_tokens, int(context_window_size * MAX_TOKENS_CONTEXT_SHARE)
        )
    return max_tokens


def _get_setting_value(settings_snapshot, key: str, default: Any) -> Any:
    """Read an optional setting; invalid configured values must propagate."""
    from ...config.thread_settings import (
        get_setting_from_snapshot,
        NoSettingsContextError,
    )

    try:
        return get_setting_from_snapshot(
            key, default, settings_snapshot=settings_snapshot
        )
    except NoSettingsContextError:
        return default


def _validate_number(
    key: str,
    value: Any,
    *,
    default: float,
    minimum: float,
    maximum: float | None,
) -> float:
    """Validate ``value`` as a finite float inside ``[minimum, maximum]``.

    ``maximum=None`` means no upper bound; the value is still required to be
    finite, so ``inf`` cannot pass as 'no timeout'.

    Only an unset value uses ``default``. Invalid configured values raise
    without including the value or a parser exception in the error.
    """
    if value is None:
        return float(default)
    if isinstance(value, bool):
        raise SettingsValueError(
            key, source="snapshot", reason="expected a finite number"
        )
    number = None
    try:
        number = float(value)
    except (TypeError, ValueError, OverflowError):
        pass
    # Raise outside the parser handler so its exception cannot expose a
    # configured value through __context__.
    if number is None or not math.isfinite(number):
        raise SettingsValueError(
            key, source="snapshot", reason="expected a finite number"
        )
    if number < minimum:
        raise SettingsValueError(
            key, source="snapshot", reason=f"must be at least {minimum}"
        )
    if maximum is not None and number > maximum:
        raise SettingsValueError(
            key, source="snapshot", reason=f"must be at most {maximum}"
        )
    return number


def _resolve_bounded_number(
    settings_snapshot,
    key: str,
    *,
    default: float,
    minimum: float,
    maximum: float | None,
) -> float:
    """Read a numeric setting and validate its supported bounds.

    ``maximum=None`` means no upper bound; the value is still required to
    be a finite number, so ``inf`` cannot slip through as 'no timeout'.
    """
    return _validate_number(
        key,
        _get_setting_value(settings_snapshot, key, default),
        default=default,
        minimum=minimum,
        maximum=maximum,
    )


def resolve_request_timeout(settings_snapshot=None) -> float:
    """Resolve the per-operation request timeout shared by all clients.

    The returned value bounds each individual network operation (connect,
    write, and the wait for the *next* response byte) — not the total
    duration of a response. Always returns a finite number of at least
    ``MIN_REQUEST_TIMEOUT``; there is no upper bound.
    """
    return _resolve_bounded_number(
        settings_snapshot,
        "llm.request_timeout",
        default=DEFAULT_REQUEST_TIMEOUT,
        minimum=MIN_REQUEST_TIMEOUT,
        maximum=None,
    )


def resolve_max_retries(settings_snapshot=None) -> int:
    """Resolve the bounded SDK retry count shared by all clients.

    Retries multiply the number of attempts, so the configured value must
    not exceed ``MAX_MAX_RETRIES``. Each attempt has per-operation timeouts;
    SDK backoff and server-directed delays also affect total elapsed time.
    """
    return int(
        _resolve_bounded_number(
            settings_snapshot,
            "llm.max_retries",
            default=DEFAULT_MAX_RETRIES,
            minimum=MIN_MAX_RETRIES,
            maximum=MAX_MAX_RETRIES,
        )
    )


def build_timeout(seconds: float) -> tuple[float, float, float, float]:
    """Expand a scalar bound into a hashable per-operation timeout tuple.

    Returns ``(connect, read, write, pool)``, the 4-tuple form httpx
    accepts wherever it accepts a ``Timeout`` object
    (``httpx._config.Timeout.__init__`` unpacks a tuple in exactly this
    order). The effective client timeout is identical either way:
    ``Timeout(connect=min(5, X), read=X, write=X, pool=X)``. The connect
    slot is bounded *at most* at 5 seconds, not pinned to it — below a
    5-second ``seconds`` the ``min`` yields ``seconds``, so raising the
    setting from 2 to 4 does raise the connect bound as well.

    **For client construction only.** ``openai._models
    .FinalRequestOptions.timeout`` is pydantic-validated and rejects a
    tuple, so a per-request ``timeout=`` keyword (e.g.
    ``client.models.list(timeout=...)``) must use ``build_httpx_timeout``
    instead. Only the client-level ``timeout=`` is untyped.

    The openai SDK also stringifies whatever it is given into the
    informational ``x-stainless-read-timeout`` request header, so the
    tuple's repr rides along on the clients that take it. The effective
    bounds are unaffected; the header is not read by this codebase.

    **The tuple shape is load-bearing, not cosmetic.** ``langchain_openai``
    builds ``ChatOpenAI``'s httpx clients through an ``@lru_cache``'d
    factory and silently falls back to a per-instance client when the
    timeout is unhashable — ``_client_utils._get_default_httpx_client``:
    *"Uses cached client unless timeout is ``httpx.Timeout``, which is not
    hashable"*. ``httpx.Timeout`` defines ``__eq__`` without ``__hash__``,
    so handing a ``Timeout`` object to ``ChatOpenAI(request_timeout=...)``
    would give every LLM its own connection pool. That would invert the
    invariants recorded in ``utilities/llm_utils.py`` (``_close_base_llm``
    deliberately does *not* close ChatOpenAI/ChatAnthropic clients because
    they are shared) and in ``docs/developing/resource-cleanup.md``. With
    the tuple those stay true, so neither needs changing.

    Sharing is also why sync entry points must keep calling the sync
    ``invoke``: the cached *async* client is bound to the event loop that
    created it, so a second ``asyncio.run()`` in the same process reuses a
    client whose loop is closed (issue #6293). That is a property of
    langchain's cache, not of this bound — an unhashable timeout only
    masked it by accident.

    Passing a bare scalar instead would set ``connect`` to the same large
    value, so a TCP connect to a blackholed address would hold a worker
    for the whole inference budget. A 2-tuple is not an option either: it
    leaves ``write`` and ``pool`` at ``None`` (unbounded), so all four
    slots are always filled.
    """
    bounded = float(seconds)
    return (min(CONNECT_TIMEOUT_SECONDS, bounded), bounded, bounded, bounded)


def build_httpx_timeout(seconds: float, timeout_cls):
    """``build_timeout`` as the SDK's own ``Timeout`` object.

    For every client whose httpx client is per-instance regardless of
    hashability, which is all of them except ``ChatOpenAI``:
    ``ollama.Client`` is constructed once per ``ChatOllama``
    /``OllamaEmbeddings``; ``OpenAIEmbeddings`` never consults
    ``_get_default_httpx_client`` at all; and the model-discovery clients
    are fresh SDK instances built per call. Only
    ``ChatOpenAI`` routes through the ``@lru_cache``'d factory, so only
    there does the tuple of ``build_timeout`` buy anything.

    Use the target SDK's exported type — ``anthropic.Timeout``,
    ``openai.Timeout``, or ``httpx.Timeout`` for Ollama — to match its
    native transport. This is not interchangeable: Anthropic and Ollama
    ship ``httpx`` while openai ships ``httpx2``, and the two ``Timeout``
    classes do not recognise each other. Handing an ``httpx.Timeout`` to
    ``openai.OpenAI(timeout=...)`` is silently treated as a scalar, which
    nests it into all four slots of an ``httpx2.Timeout``.
    """
    connect, read, write, pool = build_timeout(seconds)
    return timeout_cls(connect=connect, read=read, write=write, pool=pool)