{"repo": "aayushsoam/clawbo-agent", "files": {"agent/model_metadata.py": {"c735f26ed667f91de1e17f5b26978d98ee394ffb": {"t": 1785595311.2007687, "n": 48137, "h": "48cc9bd8890fa089", "c": "\"\"\"Model metadata, context lengths, and token estimation utilities.\n\nPure utility functions with no AIAgent dependency. Used by ContextCompressor\nand run_agent.py for pre-flight context checks.\n\"\"\"\n\nimport logging\nimport re\nimport time\nfrom pathlib import Path\nfrom typing import Any, Dict, List, Optional\nfrom urllib.parse import urlparse\n\nimport requests\nimport yaml\n\nfrom utils import base_url_host_matches, base_url_hostname\n\nfrom clawbot_constants import OPENROUTER_MODELS_URL\n\nlogger = logging.getLogger(__name__)\n\n# Provider names that can appear as a \"provider:\" prefix before a model ID.\n# Only these are stripped — Ollama-style \"model:tag\" colons (e.g. \"qwen3.5:27b\")\n# are preserved so the full model name reaches cache lookups and server queries.\n_PROVIDER_PREFIXES: frozenset[str] = frozenset({\n \"openrouter\", \"soam\", \"openai-codex\", \"copilot\", \"copilot-acp\",\n \"gemini\", \"ollama-cloud\", \"zai\", \"kimi-coding\", \"kimi-coding-cn\", \"minimax\", \"minimax-cn\", \"anthropic\", \"deepseek\",\n \"opencode-zen\", \"opencode-go\", \"ai-gateway\", \"kilocode\", \"alibaba\",\n \"qwen-oauth\",\n \"xiaomi\",\n \"arcee\",\n \"custom\", \"local\",\n # Common aliases\n \"google\", \"google-gemini\", \"google-ai-studio\",\n \"glm\", \"z-ai\", \"z.ai\", \"zhipu\", \"github\", \"github-copilot\",\n \"github-models\", \"kimi\", \"moonshot\", \"kimi-cn\", \"moonshot-cn\", \"claude\", \"deep-seek\",\n \"ollama\",\n \"opencode\", \"zen\", \"go\", \"vercel\", \"kilo\", \"dashscope\", \"aliyun\", \"qwen\",\n \"mimo\", \"xiaomi-mimo\",\n \"arcee-ai\", \"arceeai\",\n \"xai\", \"x-ai\", \"x.ai\", \"grok\",\n \"nvidia\", \"nim\", \"nvidia-nim\", \"nemotron\",\n \"qwen-portal\",\n})\n\n\n_OLLAMA_TAG_PATTERN = re.compile(\n r\"^(\\d+\\.?\\d*b|latest|stable|q\\d|fp?\\d|instruct|chat|coder|vision|text)\",\n re.IGNORECASE,\n)\n\n\ndef _strip_provider_prefix(model: str) -> str:\n \"\"\"Strip a recognised provider prefix from a model string.\n\n ``\"local:my-model\"`` → ``\"my-model\"``\n ``\"qwen3.5:27b\"`` → ``\"qwen3.5:27b\"`` (unchanged — not a provider prefix)\n ``\"qwen:0.5b\"`` → ``\"qwen:0.5b\"`` (unchanged — Ollama model:tag)\n ``\"deepseek:latest\"``→ ``\"deepseek:latest\"``(unchanged — Ollama model:tag)\n \"\"\"\n if \":\" not in model or model.startswith(\"http\"):\n return model\n prefix, suffix = model.split(\":\", 1)\n prefix_lower = prefix.strip().lower()\n if prefix_lower in _PROVIDER_PREFIXES:\n # Don't strip if suffix looks like an Ollama tag (e.g. \"7b\", \"latest\", \"q4_0\")\n if _OLLAMA_TAG_PATTERN.match(suffix.strip()):\n return model\n return suffix\n return model\n\n_model_metadata_cache: Dict[str, Dict[str, Any]] = {}\n_model_metadata_cache_time: float = 0\n_MODEL_CACHE_TTL = 3600\n_endpoint_model_metadata_cache: Dict[str, Dict[str, Dict[str, Any]]] = {}\n_endpoint_model_metadata_cache_time: Dict[str, float] = {}\n_ENDPOINT_MODEL_CACHE_TTL = 300\n\n# Descending tiers for context length probing when the model is unknown.\n# We start at 128K (a safe default for most modern models) and step down\n# on context-length errors until one works.\nCONTEXT_PROBE_TIERS = [\n 128_000,\n 64_000,\n 32_000,\n 16_000,\n 8_000,\n]\n\n# Default context length when no detection method succeeds.\nDEFAULT_FALLBACK_CONTEXT = CONTEXT_PROBE_TIERS[0]\n\n# Minimum context length required to run Clawbot Agent. Models with fewer\n# tokens cannot maintain enough working memory for tool-calling workflows.\n# Sessions, model switches, and cron jobs should reject models below this.\nMINIMUM_CONTEXT_LENGTH = 64_000\n\n# Thin fallback defaults — only broad model family patterns.\n# These fire only when provider is unknown AND models.dev/OpenRouter/Anthropic\n# all miss. Replaced the previous 80+ entry dict.\n# For provider-specific context lengths, models.dev is the primary source.\nDEFAULT_CONTEXT_LENGTHS = {\n # Anthropic Claude 4.6 (1M context) — bare IDs only to avoid\n # fuzzy-match collisions (e.g. \"anthropic/claude-sonnet-4\" is a\n # substring of \"anthropic/claude-sonnet-4.6\").\n # OpenRouter-prefixed models resolve via OpenRouter live API or models.dev.\n \"claude-opus-4-7\": 1000000,\n \"claude-opus-4.7\": 1000000,\n \"claude-opus-4-6\": 1000000,\n \"claude-sonnet-4-6\": 1000000,\n \"claude-opus-4.6\": 1000000,\n \"claude-sonnet-4.6\": 1000000,\n # Catch-all for older Claude models (must sort after specific entries)\n \"claude\": 200000,\n # OpenAI — GPT-5 family (most have 400k; specific overrides first)\n # Source: https://developers.openai.com/api/docs/models\n \"gpt-5.4-nano\": 400000, # 400k (not 1.05M like full 5.4)\n \"gpt-5.4-mini\": 400000, # 400k (not 1.05M like full 5.4)\n \"gpt-5.4\": 1050000, # GPT-5.4, GPT-5.4 Pro (1.05M context)\n \"gpt-5.1-chat\": 128000, # Chat variant has 128k context\n \"gpt-5\": 400000, # GPT-5.x base, mini, codex variants (400k)\n \"gpt-4.1\": 1047576,\n \"gpt-4\": 128000,\n # Google\n \"gemini\": 1048576,\n # Gemma (open models served via AI Studio)\n \"gemma-4-31b\": 256000,\n \"gemma-3\": 131072,\n \"gemma\": 8192, # fallback for older gemma models\n # DeepSeek\n \"deepseek\": 128000,\n # Meta\n \"llama\": 131072,\n # Qwen — specific model families before the catch-all.\n # Official docs: https://help.aliyun.com/zh/model-studio/developer-reference/\n \"qwen3-coder-plus\": 1000000, # 1M context\n \"qwen3-coder\": 262144, # 256K context\n \"qwen\": 131072,\n # MiniMax — official docs: 204,800 context for all models\n # https://platform.minimax.io/docs/api-reference/text-anthropic-api\n \"minimax\": 204800,\n # GLM\n \"glm\": 202752,\n # xAI Grok — xAI /v1/models does not return context_length metadata,\n # so these hardcoded fallbacks prevent Clawbot from probing-down to\n # the default 128k when the user points at https://api.x.ai/v1\n # via a custom provider. Values sourced from models.dev (2026-04).\n # Keys use substring matching (longest-first), so e.g. \"grok-4.20\"\n # matches \"grok-4.20-0309-reasoning\" / \"-non-reasoning\" / \"-multi-agent-0309\".\n \"grok-code-fast\": 256000, # grok-code-fast-1\n \"grok-4-1-fast\": 2000000, # grok-4-1-fast-(non-)reasoning\n \"grok-2-vision\": 8192, # grok-2-vision, -1212, -latest\n \"grok-4-fast\": 2000000, # grok-4-fast-(non-)reasoning\n \"grok-4.20\": 2000000, # grok-4.20-0309-(non-)reasoning, -multi-agent-0309\n \"grok-4\": 256000, # grok-4, grok-4-0709\n \"grok-3\": 131072, # grok-3, grok-3-mini, grok-3-fast, grok-3-mini-fast\n \"grok-2\": 131072, # grok-2, grok-2-1212, grok-2-latest\n \"grok\": 131072, # catch-all (grok-beta, unknown grok-*)\n # Kimi\n \"kimi\": 262144,\n # Nemotron — NVIDIA's open-weights series (128K context across all sizes)\n \"nemotron\": 131072,\n # Arcee\n \"trinity\": 262144,\n # OpenRouter\n \"elephant\": 262144,\n # Hugging Face Inference Providers — model IDs use org/name format\n \"Qwen/Qwen3.5-397B-A17B\": 131072,\n \"Qwen/Qwen3.5-35B-A3B\": 131072,\n \"deepseek-ai/DeepSeek-V3.2\": 65536,\n \"moonshotai/Kimi-K2.5\": 262144,\n \"moonshotai/Kimi-K2.6\": 262144,\n \"moonshotai/Kimi-K2-Thinking\": 262144,\n \"MiniMaxAI/MiniMax-M2.5\": 204800,\n \"XiaomiMiMo/MiMo-V2-Flash\": 256000,\n \"mimo-v2-pro\": 1000000,\n \"mimo-v2-omni\": 256000,\n \"mimo-v2-flash\": 256000,\n \"zai-org/GLM-5\": 202752,\n}\n\n_CONTEXT_LENGTH_KEYS = (\n \"context_length\",\n \"context_window\",\n \"max_context_length\",\n \"max_position_embeddings\",\n \"max_model_len\",\n \"max_input_tokens\",\n \"max_sequence_length\",\n \"max_seq_len\",\n \"n_ctx_train\",\n \"n_ctx\",\n)\n\n_MAX_COMPLETION_KEYS = (\n \"max_completion_tokens\",\n \"max_output_tokens\",\n \"max_tokens\",\n)\n\n# Local server hostnames / address patterns\n_LOCAL_HOSTS = (\"localhost\", \"127.0.0.1\", \"::1\", \"0.0.0.0\")\n# Docker / Podman / Lima DNS names that resolve to the host machine\n_CONTAINER_LOCAL_SUFFIXES = (\n \".docker.internal\",\n \".containers.internal\",\n \".lima.internal\",\n)\n\n\ndef _normalize_base_url(base_url: str) -> str:\n return (base_url or \"\").strip().rstrip(\"/\")\n\n\ndef _auth_headers(api_key: str = \"\") -> Dict[str, str]:\n token = str(api_key or \"\").strip()\n if not token:\n return {}\n return {\"Authorization\": f\"Bearer {token}\"}\n\n\ndef _is_openrouter_base_url(base_url: str) -> bool:\n return base_url_host_matches(base_url, \"openrouter.ai\")\n\n\ndef _is_custom_endpoint(base_url: str) -> bool:\n normalized = _normalize_base_url(base_url)\n return bool(normalized) and not _is_openrouter_base_url(normalized)\n\n\n_URL_TO_PROVIDER: Dict[str, str] = {\n \"api.openai.com\": \"openai\",\n \"chatgpt.com\": \"openai\",\n \"api.anthropic.com\": \"anthropic\",\n \"api.z.ai\": \"zai\",\n \"api.moonshot.ai\": \"kimi-coding\",\n \"api.moonshot.cn\": \"kimi-coding-cn\",\n \"api.kimi.com\": \"kimi-coding\",\n \"api.arcee.ai\": \"arcee\",\n \"api.minimax\": \"minimax\",\n \"dashscope.aliyuncs.com\": \"alibaba\",\n \"dashscope-intl.aliyuncs.com\": \"alibaba\",\n \"portal.qwen.ai\": \"qwen-oauth\",\n \"openrouter.ai\": \"openrouter\",\n \"generativelanguage.googleapis.com\": \"gemini\",\n \"inference-api.soamresearch.com\": \"soam\",\n \"api.deepseek.com\": \"deepseek\",\n \"api.githubcopilot.com\": \"copilot\",\n \"models.github.ai\": \"copilot\",\n \"api.fireworks.ai\": \"fireworks\",\n \"opencode.ai\": \"opencode-go\",\n \"api.x.ai\": \"xai\",\n \"integrate.api.nvidia.com\": \"nvidia\",\n \"api.xiaomimimo.com\": \"xiaomi\",\n \"xiaomimimo.com\": \"xiaomi\",\n \"ollama.com\": \"ollama-cloud\",\n}\n\n\ndef _infer_provider_from_url(base_url: str) -> Optional[str]:\n \"\"\"Infer the models.dev provider name from a base URL.\n\n This allows context length resolution via models.dev for custom endpoints\n like DashScope (Alibaba), Z.AI, Kimi, etc. without requiring the user to\n explicitly set the provider name in config.\n \"\"\"\n normalized = _normalize_base_url(base_url)\n if not normalized:\n return None\n parsed = urlparse(normalized if \"://\" in normalized else f\"https://{normalized}\")\n host = parsed.netloc.lower() or parsed.path.lower()\n for url_part, provider in _URL_TO_PROVIDER.items():\n if url_part in host:\n return provider\n return None\n\n\ndef _is_known_provider_base_url(base_url: str) -> bool:\n return _infer_provider_from_url(base_url) is not None\n\n\ndef is_local_endpoint(base_url: str) -> bool:\n \"\"\"Return True if base_url points to a local machine (localhost / RFC-1918 / WSL).\"\"\"\n normalized = _normalize_base_url(base_url)\n if not normalized:\n return False\n url = normalized if \"://\" in normalized else f\"http://{normalized}\"\n try:\n parsed = urlparse(url)\n host = parsed.hostname or \"\"\n except Exception:\n return False\n if host in _LOCAL_HOSTS:\n return True\n # Docker / Podman / Lima internal DNS names (e.g. host.docker.internal)\n if any(host.endswith(suffix) for suffix in _CONTAINER_LOCAL_SUFFIXES):\n return True\n # RFC-1918 private ranges and link-local\n import ipaddress\n try:\n addr = ipaddress.ip_address(host)\n return addr.is_private or addr.is_loopback or addr.is_link_local\n except ValueError:\n pass\n # Bare IP that looks like a private range (e.g. 172.26.x.x for WSL)\n parts = host.split(\".\")\n if len(parts) == 4:\n try:\n first, second = int(parts[0]), int(parts[1])\n if first == 10:\n return True\n if first == 172 and 16 <= second <= 31:\n return True\n if first == 192 and second == 168:\n return True\n except ValueError:\n pass\n return False\n\n\ndef detect_local_server_type(base_url: str, api_key: str = \"\") -> Optional[str]:\n \"\"\"Detect which local server is running at base_url by probing known endpoints.\n\n Returns one of: \"ollama\", \"lm-studio\", \"vllm\", \"llamacpp\", or None.\n \"\"\"\n import httpx\n\n normalized = _normalize_base_url(base_url)\n server_url = normalized\n if server_url.endswith(\"/v1\"):\n server_url = server_url[:-3]\n\n headers = _auth_headers(api_key)\n\n try:\n with httpx.Client(timeout=2.0, headers=headers) as client:\n # LM Studio exposes /api/v1/models — check first (most specific)\n try:\n r = client.get(f\"{server_url}/api/v1/models\")\n if r.status_code == 200:\n return \"lm-studio\"\n except Exception:\n pass\n # Ollama exposes /api/tags and responds with {\"models\": [...]}\n # LM Studio returns {\"error\": \"Unexpected endpoint\"} with status 200\n # on this path, so we must verify the response contains \"models\".\n try:\n r = client.get(f\"{server_url}/api/tags\")\n if r.status_code == 200:\n try:\n data = r.json()\n if \"models\" in data:\n return \"ollama\"\n except Exception:\n pass\n except Exception:\n pass\n # llama.cpp exposes /v1/props (older builds used /props without the /v1 prefix)\n try:\n r = client.get(f\"{server_url}/v1/props\")\n if r.status_code != 200:\n r = client.get(f\"{server_url}/props\") # fallback for older builds\n if r.status_code == 200 and \"default_generation_settings\" in r.text:\n return \"llamacpp\"\n except Exception:\n pass\n # vLLM: /version\n try:\n r = client.get(f\"{server_url}/version\")\n if r.status_code == 200:\n data = r.json()\n if \"version\" in data:\n return \"vllm\"\n except Exception:\n pass\n except Exception:\n pass\n\n return None\n\n\ndef _iter_nested_dicts(value: Any):\n if isinstance(value, dict):\n yield value\n for nested in value.values():\n yield from _iter_nested_dicts(nested)\n elif isinstance(value, list):\n for item in value:\n yield from _iter_nested_dicts(item)\n\n\ndef _coerce_reasonable_int(value: Any, minimum: int = 1024, maximum: int = 10_000_000) -> Optional[int]:\n try:\n if isinstance(value, bool):\n return None\n if isinstance(value, str):\n value = value.strip().replace(\",\", \"\")\n result = int(value)\n except (TypeError, ValueError):\n return None\n if minimum <= result <= maximum:\n return result\n return None\n\n\ndef _extract_first_int(payload: Dict[str, Any], keys: tuple[str, ...]) -> Optional[int]:\n keyset = {key.lower() for key in keys}\n for mapping in _iter_nested_dicts(payload):\n for key, value in mapping.items():\n if str(key).lower() not in keyset:\n continue\n coerced = _coerce_reasonable_int(value)\n if coerced is not None:\n return coerced\n return None\n\n\ndef _extract_context_length(payload: Dict[str, Any]) -> Optional[int]:\n return _extract_first_int(payload, _CONTEXT_LENGTH_KEYS)\n\n\ndef _extract_max_completion_tokens(payload: Dict[str, Any]) -> Optional[int]:\n return _extract_first_int(payload, _MAX_COMPLETION_KEYS)\n\n\ndef _extract_pricing(payload: Dict[str, Any]) -> Dict[str, Any]:\n alias_map = {\n \"prompt\": (\"prompt\", \"input\", \"input_cost_per_token\", \"prompt_token_cost\"),\n \"completion\": (\"completion\", \"output\", \"output_cost_per_token\", \"completion_token_cost\"),\n \"request\": (\"request\", \"request_cost\"),\n \"cache_read\": (\"cache_read\", \"cached_prompt\", \"input_cache_read\", \"cache_read_cost_per_token\"),\n \"cache_write\": (\"cache_write\", \"cache_creation\", \"input_cache_write\", \"cache_write_cost_per_token\"),\n }\n for mapping in _iter_nested_dicts(payload):\n normalized = {str(key).lower(): value for key, value in mapping.items()}\n if not any(any(alias in normalized for alias in aliases) for aliases in alias_map.values()):\n continue\n pricing: Dict[str, Any] = {}\n for target, aliases in alias_map.items():\n for alias in aliases:\n if alias in normalized and normalized[alias] not in (None, \"\"):\n pricing[target] = normalized[alias]\n break\n if pricing:\n return pricing\n return {}\n\n\ndef _add_model_aliases(cache: Dict[str, Dict[str, Any]], model_id: str, entry: Dict[str, Any]) -> None:\n cache[model_id] = entry\n if \"/\" in model_id:\n bare_model = model_id.split(\"/\", 1)[1]\n cache.setdefault(bare_model, entry)\n\n\ndef fetch_model_metadata(force_refresh: bool = False) -> Dict[str, Dict[str, Any]]:\n \"\"\"Fetch model metadata from OpenRouter (cached for 1 hour).\"\"\"\n global _model_metadata_cache, _model_metadata_cache_time\n\n if not force_refresh and _model_metadata_cache and (time.time() - _model_metadata_cache_time) < _MODEL_CACHE_TTL:\n return _model_metadata_cache\n\n try:\n response = requests.get(OPENROUTER_MODELS_URL, timeout=10)\n response.raise_for_status()\n data = response.json()\n\n cache = {}\n for model in data.get(\"data\", []):\n model_id = model.get(\"id\", \"\")\n entry = {\n \"context_length\": model.get(\"context_length\", 128000),\n \"max_completion_tokens\": model.get(\"top_provider\", {}).get(\"max_completion_tokens\", 4096),\n \"name\": model.get(\"name\", model_id),\n \"pricing\": model.get(\"pricing\", {}),\n }\n _add_model_aliases(cache, model_id, entry)\n canonical = model.get(\"canonical_slug\", \"\")\n if canonical and canonical != model_id:\n _add_model_aliases(cache, canonical, entry)\n\n _model_metadata_cache = cache\n _model_metadata_cache_time = time.time()\n logger.debug(\"Fetched metadata for %s models from OpenRouter\", len(cache))\n return cache\n\n except Exception as e:\n logging.warning(f\"Failed to fetch model metadata from OpenRouter: {e}\")\n return _model_metadata_cache or {}\n\n\ndef fetch_endpoint_model_metadata(\n base_url: str,\n api_key: str = \"\",\n force_refresh: bool = False,\n) -> Dict[str, Dict[str, Any]]:\n \"\"\"Fetch model metadata from an OpenAI-compatible ``/models`` endpoint.\n\n This is used for explicit custom endpoints where hardcoded global model-name\n defaults are unreliable. Results are cached in memory per base URL.\n \"\"\"\n normalized = _normalize_base_url(base_url)\n if not normalized or _is_openrouter_base_url(normalized):\n return {}\n\n if not force_refresh:\n cached = _endpoint_model_metadata_cache.get(normalized)\n cached_at = _endpoint_model_metadata_cache_time.get(normalized, 0)\n if cached is not None and (time.time() - cached_at) < _ENDPOINT_MODEL_CACHE_TTL:\n return cached\n\n candidates = [normalized]\n if normalized.endswith(\"/v1\"):\n alternate = normalized[:-3].rstrip(\"/\")\n else:\n alternate = normalized + \"/v1\"\n if alternate and alternate not in candidates:\n candidates.append(alternate)\n\n headers = {\"Authorization\": f\"Bearer {api_key}\"} if api_key else {}\n last_error: Optional[Exception] = None\n\n if is_local_endpoint(normalized):\n try:\n if detect_local_server_type(normalized, api_key=api_key) == \"lm-studio\":\n server_url = normalized[:-3].rstrip(\"/\") if normalized.endswith(\"/v1\") else normalized\n response = requests.get(\n server_url.rstrip(\"/\") + \"/api/v1/models\",\n headers=headers,\n timeout=10,\n )\n response.raise_for_status()\n payload = response.json()\n cache: Dict[str, Dict[str, Any]] = {}\n for model in payload.get(\"models\", []):\n if not isinstance(model, dict):\n continue\n model_id = model.get(\"key\") or model.get(\"id\")\n if not model_id:\n continue\n entry: Dict[str, Any] = {\"name\": model.get(\"name\", model_id)}\n\n context_length = None\n for inst in model.get(\"loaded_instances\", []) or []:\n if not isinstance(inst, dict):\n continue\n cfg = inst.get(\"config\", {})\n ctx = cfg.get(\"context_length\") if isinstance(cfg, dict) else None\n if isinstance(ctx, int) and ctx > 0:\n context_length = ctx\n break\n if context_length is None:\n context_length = _extract_context_length(model)\n if context_length is not None:\n entry[\"context_length\"] = context_length\n\n max_completion_tokens = _extract_max_completion_tokens(model)\n if max_completion_tokens is not None:\n entry[\"max_completion_tokens\"] = max_completion_tokens\n\n pricing = _extract_pricing(model)\n if pricing:\n entry[\"pricing\"] = pricing\n\n _add_model_aliases(cache, model_id, entry)\n alt_id = model.get(\"id\")\n if isinstance(alt_id, str) and alt_id and alt_id != model_id:\n _add_model_aliases(cache, alt_id, entry)\n\n _endpoint_model_metadata_cache[normalized] = cache\n _endpoint_model_metadata_cache_time[normalized] = time.time()\n return cache\n except Exception as exc:\n last_error = exc\n\n for candidate in candidates:\n url = candidate.rstrip(\"/\") + \"/models\"\n try:\n response = requests.get(url, headers=headers, timeout=10)\n response.raise_for_status()\n payload = response.json()\n cache: Dict[str, Dict[str, Any]] = {}\n for model in payload.get(\"data\", []):\n if not isinstance(model, dict):\n continue\n model_id = model.get(\"id\")\n if not model_id:\n continue\n entry: Dict[str, Any] = {\"name\": model.get(\"name\", model_id)}\n context_length = _extract_context_length(model)\n if context_length is not None:\n entry[\"context_length\"] = context_length\n max_completion_tokens = _extract_max_completion_tokens(model)\n if max_completion_tokens is not None:\n entry[\"max_completion_tokens\"] = max_completion_tokens\n pricing = _extract_pricing(model)\n if pricing:\n entry[\"pricing\"] = pricing\n _add_model_aliases(cache, model_id, entry)\n\n # If this is a llama.cpp server, query /props for actual allocated context\n is_llamacpp = any(\n m.get(\"owned_by\") == \"llamacpp\"\n for m in payload.get(\"data\", []) if isinstance(m, dict)\n )\n if is_llamacpp:\n try:\n # Try /v1/props first (current llama.cpp); fall back to /props for older builds\n base = candidate.rstrip(\"/\").replace(\"/v1\", \"\")\n props_resp = requests.get(base + \"/v1/props\", headers=headers, timeout=5)\n if not props_resp.ok:\n props_resp = requests.get(base + \"/props\", headers=headers, timeout=5)\n if props_resp.ok:\n props = props_resp.json()\n gen_settings = props.get(\"default_generation_settings\", {})\n n_ctx = gen_settings.get(\"n_ctx\")\n model_alias = props.get(\"model_alias\", \"\")\n if n_ctx and model_alias and model_alias in cache:\n cache[model_alias][\"context_length\"] = n_ctx\n except Exception:\n pass\n\n _endpoint_model_metadata_cache[normalized] = cache\n _endpoint_model_metadata_cache_time[normalized] = time.time()\n return cache\n except Exception as exc:\n last_error = exc\n\n if last_error:\n logger.debug(\"Failed to fetch model metadata from %s/models: %s\", normalized, last_error)\n _endpoint_model_metadata_cache[normalized] = {}\n _endpoint_model_metadata_cache_time[normalized] = time.time()\n return {}\n\n\ndef _get_context_cache_path() -> Path:\n \"\"\"Return path to the persistent context length cache file.\"\"\"\n from clawbot_constants import get_clawbot_home\n return get_clawbot_home() / \"context_length_cache.yaml\"\n\n\ndef _load_context_cache() -> Dict[str, int]:\n \"\"\"Load the model+provider -> context_length cache from disk.\"\"\"\n path = _get_context_cache_path()\n if not path.exists():\n return {}\n try:\n with open(path) as f:\n data = yaml.safe_load(f) or {}\n return data.get(\"context_lengths\", {})\n except Exception as e:\n logger.debug(\"Failed to load context length cache: %s\", e)\n return {}\n\n\ndef save_context_length(model: str, base_url: str, length: int) -> None:\n \"\"\"Persist a discovered context length for a model+provider combo.\n\n Cache key is ``model@base_url`` so the same model name served from\n different providers can have different limits.\n \"\"\"\n key = f\"{model}@{base_url}\"\n cache = _load_context_cache()\n if cache.get(key) == length:\n return # already stored\n cache[key] = length\n path = _get_context_cache_path()\n try:\n path.parent.mkdir(parents=True, exist_ok=True)\n with open(path, \"w\") as f:\n yaml.dump({\"context_lengths\": cache}, f, default_flow_style=False)\n logger.info(\"Cached context length %s -> %s tokens\", key, f\"{length:,}\")\n except Exception as e:\n logger.debug(\"Failed to save context length cache: %s\", e)\n\n\ndef get_cached_context_length(model: str, base_url: str) -> Optional[int]:\n \"\"\"Look up a previously discovered context length for model+provider.\"\"\"\n key = f\"{model}@{base_url}\"\n cache = _load_context_cache()\n return cache.get(key)\n\n\ndef get_next_probe_tier(current_length: int) -> Optional[int]:\n \"\"\"Return the next lower probe tier, or None if already at minimum.\"\"\"\n for tier in CONTEXT_PROBE_TIERS:\n if tier < current_length:\n return tier\n return None\n\n\ndef parse_context_limit_from_error(error_msg: str) -> Optional[int]:\n \"\"\"Try to extract the actual context limit from an API error message.\n\n Many providers include the limit in their error text, e.g.:\n - \"maximum context length is 32768 tokens\"\n - \"context_length_exceeded: 131072\"\n - \"Maximum context size 32768 exceeded\"\n - \"model's max context length is 65536\"\n \"\"\"\n error_lower = error_msg.lower()\n # Pattern: look for numbers near context-related keywords\n patterns = [\n r'(?:max(?:imum)?|limit)\\s*(?:context\\s*)?(?:length|size|window)?\\s*(?:is|of|:)?\\s*(\\d{4,})',\n r'context\\s*(?:length|size|window)\\s*(?:is|of|:)?\\s*(\\d{4,})',\n r'(\\d{4,})\\s*(?:token)?\\s*(?:context|limit)',\n r'>\\s*(\\d{4,})\\s*(?:max|limit|token)', # \"250000 tokens > 200000 maximum\"\n r'(\\d{4,})\\s*(?:max(?:imum)?)\\b', # \"200000 maximum\"\n ]\n for pattern in patterns:\n match = re.search(pattern, error_lower)\n if match:\n limit = int(match.group(1))\n # Sanity check: must be a reasonable context length\n if 1024 <= limit <= 10_000_000:\n return limit\n return None\n\n\ndef parse_available_output_tokens_from_error(error_msg: str) -> Optional[int]:\n \"\"\"Detect an \"output cap too large\" error and return how many output tokens are available.\n\n Background — two distinct context errors exist:\n 1. \"Prompt too long\" — the INPUT itself exceeds the context window.\n Fix: compress history and/or halve context_length.\n 2. \"max_tokens too large\" — input is fine, but input + requested_output > window.\n Fix: reduce max_tokens (the output cap) for this call.\n Do NOT touch context_length — the window hasn't shrunk.\n\n Anthropic's API returns errors like:\n \"max_tokens: 32768 > context_window: 200000 - input_tokens: 190000 = available_tokens: 10000\"\n\n Returns the number of output tokens that would fit (e.g. 10000 above), or None if\n the error does not look like a max_tokens-too-large error.\n \"\"\"\n error_lower = error_msg.lower()\n\n # Must look like an output-cap error, not a prompt-length error.\n is_output_cap_error = (\n \"max_tokens\" in error_lower\n and (\"available_tokens\" in error_lower or \"available tokens\" in error_lower)\n )\n if not is_output_cap_error:\n return None\n\n # Extract the available_tokens figure.\n # Anthropic format: \"… = available_tokens: 10000\"\n patterns = [\n r'available_tokens[:\\s]+(\\d+)',\n r'available\\s+tokens[:\\s]+(\\d+)',\n # fallback: last number after \"=\" in expressions like \"200000 - 190000 = 10000\"\n r'=\\s*(\\d+)\\s*$',\n ]\n for pattern in patterns:\n match = re.search(pattern, error_lower)\n if match:\n tokens = int(match.group(1))\n if tokens >= 1:\n return tokens\n return None\n\n\ndef _model_id_matches(candidate_id: str, lookup_model: str) -> bool:\n \"\"\"Return True if *candidate_id* (from server) matches *lookup_model* (configured).\n\n Supports two forms:\n - Exact match: \"nvidia-nemotron-super-49b-v1\" == \"nvidia-nemotron-super-49b-v1\"\n - Slug match: \"nvidia/nvidia-nemotron-super-49b-v1\" matches \"nvidia-nemotron-super-49b-v1\"\n (the part after the last \"/\" equals lookup_model)\n\n This covers LM Studio's native API which stores models as \"publisher/slug\"\n while users typically configure only the slug after the \"local:\" prefix.\n \"\"\"\n if candidate_id == lookup_model:\n return True\n # Slug match: basename of candidate equals the lookup name\n if \"/\" in candidate_id and candidate_id.rsplit(\"/\", 1)[1] == lookup_model:\n return True\n return False\n\n\ndef query_ollama_num_ctx(model: str, base_url: str, api_key: str = \"\") -> Optional[int]:\n \"\"\"Query an Ollama server for the model's context length.\n\n Returns the model's maximum context from GGUF metadata via ``/api/show``,\n or the explicit ``num_ctx`` from the Modelfile if set. Returns None if\n the server is unreachable or not Ollama.\n\n This is the value that should be passed as ``num_ctx`` in Ollama chat\n requests to override the default 2048.\n \"\"\"\n import httpx\n\n bare_model = _strip_provider_prefix(model)\n server_url = base_url.rstrip(\"/\")\n if server_url.endswith(\"/v1\"):\n server_url = server_url[:-3]\n\n try:\n server_type = detect_local_server_type(base_url, api_key=api_key)\n except Exception:\n return None\n if server_type != \"ollama\":\n return None\n\n headers = _auth_headers(api_key)\n\n try:\n with httpx.Client(timeout=3.0, headers=headers) as client:\n resp = client.post(f\"{server_url}/api/show\", json={\"name\": bare_model})\n if resp.status_code != 200:\n return None\n data = resp.json()\n\n # Prefer explicit num_ctx from Modelfile parameters (user override)\n params = data.get(\"parameters\", \"\")\n if \"num_ctx\" in params:\n for line in params.split(\"\\n\"):\n if \"num_ctx\" in line:\n parts = line.strip().split()\n if len(parts) >= 2:\n try:\n return int(parts[-1])\n except ValueError:\n pass\n\n # Fall back to GGUF model_info context_length (training max)\n model_info = data.get(\"model_info\", {})\n for key, value in model_info.items():\n if \"context_length\" in key and isinstance(value, (int, float)):\n return int(value)\n except Exception:\n pass\n return None\n\n\ndef _query_local_context_length(model: str, base_url: str, api_key: str = \"\") -> Optional[int]:\n \"\"\"Query a local server for the model's context length.\"\"\"\n import httpx\n\n # Strip recognised provider prefix (e.g., \"local:model-name\" → \"model-name\").\n # Ollama \"model:tag\" colons (e.g. \"qwen3.5:27b\") are intentionally preserved.\n model = _strip_provider_prefix(model)\n\n # Strip /v1 suffix to get the server root\n server_url = base_url.rstrip(\"/\")\n if server_url.endswith(\"/v1\"):\n server_url = server_url[:-3]\n\n headers = _auth_headers(api_key)\n\n try:\n server_type = detect_local_server_type(base_url, api_key=api_key)\n except Exception:\n server_type = None\n\n try:\n with httpx.Client(timeout=3.0, headers=headers) as client:\n # Ollama: /api/show returns model details with context info\n if server_type == \"ollama\":\n resp = client.post(f\"{server_url}/api/show\", json={\"name\": model})\n if resp.status_code == 200:\n data = resp.json()\n # Prefer explicit num_ctx from Modelfile parameters: this is\n # the *runtime* context Ollama will actually allocate KV cache\n # for. The GGUF model_info.context_length is the training max,\n # which can be larger than num_ctx — using it here would let\n # Clawbot grow conversations past the runtime limit and Ollama\n # would silently truncate. Matches query_ollama_num_ctx().\n params = data.get(\"parameters\", \"\")\n if \"num_ctx\" in params:\n for line in params.split(\"\\n\"):\n if \"num_ctx\" in line:\n parts = line.strip().split()\n if len(parts) >= 2:\n try:\n return int(parts[-1])\n except ValueError:\n pass\n # Fall back to GGUF model_info context_length (training max)\n model_info = data.get(\"model_info\", {})\n for key, value in model_info.items():\n if \"context_length\" in key and isinstance(value, (int, float)):\n return int(value)\n\n # LM Studio native API: /api/v1/models returns max_context_length.\n # This is more reliable than the OpenAI-compat /v1/models which\n # doesn't include context window information for LM Studio servers.\n # Use _model_id_matches for fuzzy matching: LM Studio stores models as\n # \"publisher/slug\" but users configure only \"slug\" after \"local:\" prefix.\n if server_type == \"lm-studio\":\n resp = client.get(f\"{server_url}/api/v1/models\")\n if resp.status_code == 200:\n data = resp.json()\n for m in data.get(\"models\", []):\n if _model_id_matches(m.get(\"key\", \"\"), model) or _model_id_matches(m.get(\"id\", \"\"), model):\n # Prefer loaded instance context (actual runtime value)\n for inst in m.get(\"loaded_instances\", []):\n cfg = inst.get(\"config\", {})\n ctx = cfg.get(\"context_length\")\n if ctx and isinstance(ctx, (int, float)):\n return int(ctx)\n # Fall back to max_context_length (theoretical model max)\n ctx = m.get(\"max_context_length\") or m.get(\"context_length\")\n if ctx and isinstance(ctx, (int, float)):\n return int(ctx)\n\n # LM Studio / vLLM / llama.cpp: try /v1/models/{model}\n resp = client.get(f\"{server_url}/v1/models/{model}\")\n if resp.status_code == 200:\n data = resp.json()\n # vLLM returns max_model_len\n ctx = data.get(\"max_model_len\") or data.get(\"context_length\") or data.get(\"max_tokens\")\n if ctx and isinstance(ctx, (int, float)):\n return int(ctx)\n\n # Try /v1/models and find the model in the list.\n # Use _model_id_matches to handle \"publisher/slug\" vs bare \"slug\".\n resp = client.get(f\"{server_url}/v1/models\")\n if resp.status_code == 200:\n data = resp.json()\n models_list = data.get(\"data\", [])\n for m in models_list:\n if _model_id_matches(m.get(\"id\", \"\"), model):\n ctx = m.get(\"max_model_len\") or m.get(\"context_length\") or m.get(\"max_tokens\")\n if ctx and isinstance(ctx, (int, float)):\n return int(ctx)\n except Exception:\n pass\n\n return None\n\n\ndef _normalize_model_version(model: str) -> str:\n \"\"\"Normalize version separators for matching.\n\n Soam uses dashes: claude-opus-4-6, claude-sonnet-4-5\n OpenRouter uses dots: claude-opus-4.6, claude-sonnet-4.5\n Normalize both to dashes for comparison.\n \"\"\"\n return model.replace(\".\", \"-\")\n\n\ndef _query_anthropic_context_length(model: str, base_url: str, api_key: str) -> Optional[int]:\n \"\"\"Query Anthropic's /v1/models endpoint for context length.\n\n Only works with regular ANTHROPIC_API_KEY (sk-ant-api*).\n OAuth tokens (sk-ant-oat*) from Claude Code return 401.\n \"\"\"\n if not api_key or api_key.startswith(\"sk-ant-oat\"):\n return None # OAuth tokens can't access /v1/models\n try:\n base = base_url.rstrip(\"/\")\n if base.endswith(\"/v1\"):\n base = base[:-3]\n url = f\"{base}/v1/models?limit=1000\"\n headers = {\n \"x-api-key\": api_key,\n \"anthropic-version\": \"2023-06-01\",\n }\n resp = requests.get(url, headers=headers, timeout=10)\n if resp.status_code != 200:\n return None\n data = resp.json()\n for m in data.get(\"data\", []):\n if m.get(\"id\") == model:\n ctx = m.get(\"max_input_tokens\")\n if isinstance(ctx, int) and ctx > 0:\n return ctx\n except Exception as e:\n logger.debug(\"Anthropic /v1/models query failed: %s\", e)\n return None\n\n\ndef _resolve_soam_context_length(model: str) -> Optional[int]:\n \"\"\"Resolve Soam Portal model context length via OpenRouter metadata.\n\n Soam model IDs are bare (e.g. 'claude-opus-4-6') while OpenRouter uses\n prefixed IDs (e.g. 'anthropic/claude-opus-4.6'). Try suffix matching\n with version normalization (dot↔dash).\n \"\"\"\n metadata = fetch_model_metadata() # OpenRouter cache\n # Exact match first\n if model in metadata:\n return metadata[model].get(\"context_length\")\n\n normalized = _normalize_model_version(model).lower()\n\n for or_id, entry in metadata.items():\n bare = or_id.split(\"/\", 1)[1] if \"/\" in or_id else or_id\n if bare.lower() == model.lower() or _normalize_model_version(bare).lower() == normalized:\n return entry.get(\"context_length\")\n\n # Partial prefix match for cases like gemini-3-flash → gemini-3-flash-preview\n # Require match to be at a word boundary (followed by -, :, or end of string)\n model_lower = model.lower()\n for or_id, entry in metadata.items():\n bare = or_id.split(\"/\", 1)[1] if \"/\" in or_id else or_id\n for candidate, query in [(bare.lower(), model_lower), (_normalize_model_version(bare).lower(), normalized)]:\n if candidate.startswith(query) and (\n len(candidate) == len(query) or candidate[len(query)] in \"-:.\"\n ):\n return entry.get(\"context_length\")\n\n return None\n\n\ndef get_model_context_length(\n model: str,\n base_url: str = \"\",\n api_key: str = \"\",\n config_context_length: int | None = None,\n provider: str = \"\",\n) -> int:\n \"\"\"Get the context length for a model.\n\n Resolution order:\n 0. Explicit config override (model.context_length or custom_providers per-model)\n 1. Persistent cache (previously discovered via probing)\n 2. Active endpoint metadata (/models for explicit custom endpoints)\n 3. Local server query (for local endpoints)\n 4. Anthropic /v1/models API (API-key users only, not OAuth)\n 5. OpenRouter live API metadata\n 6. Soam suffix-match via OpenRouter cache\n 7. models.dev registry lookup (provider-aware)\n 8. Thin hardcoded defaults (broad family patterns)\n 9. Default fallback (128K)\n \"\"\"\n # 0. Explicit config override — user knows best\n if config_context_length is not None and isinstance(config_context_length, int) and config_context_length > 0:\n return config_context_length\n\n # Normalise provider-prefixed model names (e.g. \"local:model-name\" →\n # \"model-name\") so cache lookups and server queries use the bare ID that\n # local servers actually know about. Ollama \"model:tag\" colons are preserved.\n model = _strip_provider_prefix(model)\n\n # 1. Check persistent cache (model+provider)\n if base_url:\n cached = get_cached_context_length(model, base_url)\n if cached is not None:\n return cached\n\n # 2. Active endpoint metadata for truly custom/unknown endpoints.\n # Known providers (Copilot, OpenAI, Anthropic, etc.) skip this — their\n # /models endpoint may report a provider-imposed limit (e.g. Copilot\n # returns 128k) instead of the model's full context (400k). models.dev\n # has the correct per-provider values and is checked at step 5+.\n if _is_custom_endpoint(base_url) and not _is_known_provider_base_url(base_url):\n endpoint_metadata = fetch_endpoint_model_metadata(base_url, api_key=api_key)\n matched = endpoint_metadata.get(model)\n if not matched:\n # Single-model servers: if only one model is loaded, use it\n if len(endpoint_metadata) == 1:\n matched = next(iter(endpoint_metadata.values()))\n else:\n # Fuzzy match: substring in either direction\n for key, entry in endpoint_metadata.items():\n if model in key or key in model:\n matched = entry\n break\n if matched:\n context_length = matched.get(\"context_length\")\n if isinstance(context_length, int):\n return context_length\n if not _is_known_provider_base_url(base_url):\n # 3. Try querying local server directly\n if is_local_endpoint(base_url):\n local_ctx = _query_local_context_length(model, base_url, api_key=api_key)\n if local_ctx and local_ctx > 0:\n save_context_length(model, base_url, local_ctx)\n return local_ctx\n logger.info(\n \"Could not detect context length for model %r at %s — \"\n \"defaulting to %s tokens (probe-down). Set model.context_length \"\n \"in config.yaml to override.\",\n model, base_url, f\"{DEFAULT_FALLBACK_CONTEXT:,}\",\n )\n return DEFAULT_FALLBACK_CONTEXT\n\n # 4. Anthropic /v1/models API (only for regular API keys, not OAuth)\n if provider == \"anthropic\" or (\n base_url and base_url_hostname(base_url) == \"api.anthropic.com\"\n ):\n ctx = _query_anthropic_context_length(model, base_url or \"https://api.anthropic.com\", api_key)\n if ctx:\n return ctx\n\n # 4b. AWS Bedrock — use static context length table.\n # Bedrock's ListFoundationModels doesn't expose context window sizes,\n # so we maintain a curated table in bedrock_adapter.py.\n if provider == \"bedrock\" or (\n base_url\n and base_url_hostname(base_url).startswith(\"bedrock-runtime.\")\n and base_url_host_matches(base_url, \"amazonaws.com\")\n ):\n try:\n from agent.bedrock_adapter import get_bedrock_context_length\n return get_bedrock_context_length(model)\n except ImportError:\n pass # boto3 not installed — fall through to generic resolution\n\n # 5. Provider-aware lookups (before generic OpenRouter cache)\n # These are provider-specific and take priority over the generic OR cache,\n # since the same model can have different context limits per provider\n # (e.g. claude-opus-4.6 is 1M on Anthropic but 128K on GitHub Copilot).\n # If provider is generic (openrouter/custom/empty), try to infer from URL.\n effective_provider = provider\n if not effective_provider or effective_provider in (\"openrouter\", \"custom\"):\n if base_url:\n inferred = _infer_provider_from_url(base_url)\n if inferred:\n effective_provider = inferred\n\n if effective_provider == \"soam\":\n ctx = _resolve_soam_context_length(model)\n if ctx:\n return ctx\n if effective_provider:\n from agent.models_dev import lookup_models_dev_context\n ctx = lookup_models_dev_context(effective_provider, model)\n if ctx:\n return ctx\n\n # 6. OpenRouter live API metadata (provider-unaware fallback)\n metadata = fetch_model_metadata()\n if model in metadata:\n return metadata[model].get(\"context_length\", 128000)\n\n # 8. Hardcoded defaults (fuzzy match — longest key first for specificity)\n # Only check `default_model in model` (is the key a substring of the input).\n # The reverse (`model in default_model`) causes shorter names like\n # \"claude-sonnet-4\" to incorrectly match \"claude-sonnet-4-6\" and return 1M.\n model_lower = model.lower()\n for default_model, length in sorted(\n DEFAULT_CONTEXT_LENGTHS.items(), key=lambda x: len(x[0]), reverse=True\n ):\n if default_model in model_lower:\n return length\n\n # 9. Query local server as last resort\n if base_url and is_local_endpoint(base_url):\n local_ctx = _query_local_context_length(model, base_url, api_key=api_key)\n if local_ctx and local_ctx > 0:\n save_context_length(model, base_url, local_ctx)\n return local_ctx\n\n # 10. Default fallback — 128K\n return DEFAULT_FALLBACK_CONTEXT\n\n\ndef estimate_tokens_rough(text: str) -> int:\n \"\"\"Rough token estimate (~4 chars/token) for pre-flight checks.\n\n Uses ceiling division so short texts (1-3 chars) never estimate as\n 0 tokens, which would cause the compressor and pre-flight checks to\n systematically undercount when many short tool results are present.\n \"\"\"\n if not text:\n return 0\n return (len(text) + 3) // 4\n\n\ndef estimate_messages_tokens_rough(messages: List[Dict[str, Any]]) -> int:\n \"\"\"Rough token estimate for a message list (pre-flight only).\"\"\"\n total_chars = sum(len(str(msg)) for msg in messages)\n return (total_chars + 3) // 4\n\n\ndef estimate_request_tokens_rough(\n messages: List[Dict[str, Any]],\n *,\n system_prompt: str = \"\",\n tools: Optional[List[Dict[str, Any]]] = None,\n) -> int:\n \"\"\"Rough token estimate for a full chat-completions request.\n\n Includes the major payload buckets Clawbot sends to providers:\n system prompt, conversation messages, and tool schemas. With 50+\n tools enabled, schemas alone can add 20-30K tokens — a significant\n blind spot when only counting messages.\n \"\"\"\n total_chars = 0\n if system_prompt:\n total_chars += len(system_prompt)\n if messages:\n total_chars += sum(len(str(msg)) for msg in messages)\n if tools:\n total_chars += len(str(tools))\n return (total_chars + 3) // 4\n"}}}}