"""Shared defaults for the Anthropic provider package.

This module exists to break a circular import between ``__init__`` (which
imports tool helpers from ``responses``) and ``responses`` (which needs the
same default constant).  Both modules import from here instead of
duplicating the value.
"""

from __future__ import annotations

import logging
from typing import Final

logger = logging.getLogger(__name__)

# Anthropic's Messages API **requires** ``max_tokens`` as a mandatory
# parameter — the SDK raises ``TypeError`` if omitted, and the server
# returns 400.  No other major provider (OpenAI, Google, xAI, etc.)
# imposes this constraint.
#
# This conservative fallback is used only when (a) the caller does not
# specify a limit AND (b) the model is not in the catalog.  For cataloged
# models, ``max_output_for_model()`` returns the per-model limit from
# ``ModelInfo.context_window_out``.
ANTHROPIC_MAX_TOKENS_DEFAULT: Final[int] = 16_384


def max_output_for_model(model_id: str) -> int:
    """Return the max output token limit for an Anthropic model.

    Queries the catalog's ``context_window_out`` field, which is the
    authoritative per-model output limit sourced from Anthropic's
    documentation.  Falls back to ``ANTHROPIC_MAX_TOKENS_DEFAULT`` for
    models not yet in the catalog (e.g., newly discovered models).

    Args:
        model_id: Canonical model identifier (e.g., ``"claude-opus-4-6"``).

    Returns:
        Maximum output tokens the model supports.
    """
    try:
        from ember.models.catalog import MODEL_CATALOG

        info = MODEL_CATALOG.get(model_id)
        if info is not None and info.context_window_out is not None:
            return info.context_window_out
    except Exception:
        logger.debug(
            "Catalog lookup failed for model '%s'; using default %d",
            model_id,
            ANTHROPIC_MAX_TOKENS_DEFAULT,
        )
    return ANTHROPIC_MAX_TOKENS_DEFAULT
