|
| 1 | +"""Default model metadata lookup tables. |
| 2 | +
|
| 3 | +Provides context window limits for known model IDs across all providers. |
| 4 | +Values sourced from provider documentation and |
| 5 | +https://github.com/BerriAI/litellm/blob/litellm_internal_staging/model_prices_and_context_window.json |
| 6 | +
|
| 7 | +Applied to providers with well-known, fixed model IDs: Bedrock, Anthropic, OpenAI, |
| 8 | +OpenAI Responses, Gemini, and Mistral. Providers that use local/custom model IDs |
| 9 | +(Ollama, LlamaCpp, SageMaker) or proxy to other providers with their own prefixed |
| 10 | +ID format (LiteLLM) are excluded — their context windows depend on deployment config, |
| 11 | +not a static table. |
| 12 | +""" |
| 13 | + |
| 14 | +import logging |
| 15 | +from collections.abc import Mapping |
| 16 | +from typing import TypeVar |
| 17 | + |
| 18 | +logger = logging.getLogger(__name__) |
| 19 | + |
| 20 | +_C = TypeVar("_C", bound=Mapping[str, object]) |
| 21 | + |
| 22 | +# Context window limits (in tokens) for known model IDs. |
| 23 | +# |
| 24 | +# Best-effort lookup table — unknown models return None and callers |
| 25 | +# fall back gracefully (e.g. proactive compression is disabled). |
| 26 | +# Users can always override with an explicit context_window_limit in their model config. |
| 27 | +# |
| 28 | +# For Bedrock models with cross-region prefixes (e.g. us., eu., global.), |
| 29 | +# get_context_window_limit strips the prefix before lookup so only the base model ID is needed here. |
| 30 | +_CONTEXT_WINDOW_LIMITS: dict[str, int] = { |
| 31 | + # Anthropic (direct API) |
| 32 | + "claude-sonnet-4-6": 1_000_000, |
| 33 | + "claude-sonnet-4-20250514": 1_000_000, |
| 34 | + "claude-sonnet-4-5": 200_000, |
| 35 | + "claude-sonnet-4-5-20250929": 200_000, |
| 36 | + "claude-opus-4-6": 1_000_000, |
| 37 | + "claude-opus-4-6-20260205": 1_000_000, |
| 38 | + "claude-opus-4-7": 1_000_000, |
| 39 | + "claude-opus-4-7-20260416": 1_000_000, |
| 40 | + "claude-opus-4-5": 200_000, |
| 41 | + "claude-opus-4-5-20251101": 200_000, |
| 42 | + "claude-opus-4-20250514": 200_000, |
| 43 | + "claude-opus-4-1": 200_000, |
| 44 | + "claude-opus-4-1-20250805": 200_000, |
| 45 | + "claude-haiku-4-5": 200_000, |
| 46 | + "claude-haiku-4-5-20251001": 200_000, |
| 47 | + "claude-3-7-sonnet-20250219": 200_000, |
| 48 | + "claude-3-5-sonnet-20241022": 200_000, |
| 49 | + "claude-3-5-sonnet-20240620": 200_000, |
| 50 | + "claude-3-5-haiku-20241022": 200_000, |
| 51 | + "claude-3-opus-20240229": 200_000, |
| 52 | + "claude-3-haiku-20240307": 200_000, |
| 53 | + # Bedrock Anthropic (base model IDs — cross-region prefixes stripped by get_context_window_limit) |
| 54 | + "anthropic.claude-sonnet-4-6": 1_000_000, |
| 55 | + "anthropic.claude-sonnet-4-20250514-v1:0": 1_000_000, |
| 56 | + "anthropic.claude-sonnet-4-5-20250929-v1:0": 200_000, |
| 57 | + "anthropic.claude-opus-4-6-v1": 1_000_000, |
| 58 | + "anthropic.claude-opus-4-7": 1_000_000, |
| 59 | + "anthropic.claude-opus-4-5-20251101-v1:0": 200_000, |
| 60 | + "anthropic.claude-opus-4-20250514-v1:0": 200_000, |
| 61 | + "anthropic.claude-opus-4-1-20250805-v1:0": 200_000, |
| 62 | + "anthropic.claude-haiku-4-5-20251001-v1:0": 200_000, |
| 63 | + "anthropic.claude-haiku-4-5@20251001": 200_000, |
| 64 | + "anthropic.claude-3-7-sonnet-20250219-v1:0": 200_000, |
| 65 | + "anthropic.claude-3-7-sonnet-20240620-v1:0": 200_000, |
| 66 | + "anthropic.claude-3-5-sonnet-20241022-v2:0": 200_000, |
| 67 | + "anthropic.claude-3-5-sonnet-20240620-v1:0": 200_000, |
| 68 | + "anthropic.claude-3-5-haiku-20241022-v1:0": 200_000, |
| 69 | + "anthropic.claude-3-opus-20240229-v1:0": 200_000, |
| 70 | + "anthropic.claude-3-haiku-20240307-v1:0": 200_000, |
| 71 | + "anthropic.claude-3-sonnet-20240229-v1:0": 200_000, |
| 72 | + "anthropic.claude-mythos-preview": 1_000_000, |
| 73 | + # Bedrock Amazon Nova |
| 74 | + "amazon.nova-pro-v1:0": 300_000, |
| 75 | + "amazon.nova-lite-v1:0": 300_000, |
| 76 | + "amazon.nova-micro-v1:0": 128_000, |
| 77 | + "amazon.nova-premier-v1:0": 1_000_000, |
| 78 | + "amazon.nova-2-lite-v1:0": 1_000_000, |
| 79 | + "amazon.nova-2-pro-preview-20251202-v1:0": 1_000_000, |
| 80 | + # OpenAI |
| 81 | + "gpt-5.5": 1_050_000, |
| 82 | + "gpt-5.5-pro": 1_050_000, |
| 83 | + "gpt-5.4": 1_050_000, |
| 84 | + "gpt-5.4-pro": 1_050_000, |
| 85 | + "gpt-5.4-mini": 272_000, |
| 86 | + "gpt-5.4-nano": 272_000, |
| 87 | + "gpt-5.2": 272_000, |
| 88 | + "gpt-5.2-pro": 272_000, |
| 89 | + "gpt-5.1": 272_000, |
| 90 | + "gpt-5": 272_000, |
| 91 | + "gpt-5-mini": 272_000, |
| 92 | + "gpt-5-nano": 272_000, |
| 93 | + "gpt-5-pro": 128_000, |
| 94 | + "gpt-4.1": 1_047_576, |
| 95 | + "gpt-4.1-mini": 1_047_576, |
| 96 | + "gpt-4.1-nano": 1_047_576, |
| 97 | + "gpt-4o": 128_000, |
| 98 | + "gpt-4o-mini": 128_000, |
| 99 | + "gpt-4-turbo": 128_000, |
| 100 | + "o3": 200_000, |
| 101 | + "o3-mini": 200_000, |
| 102 | + "o3-pro": 200_000, |
| 103 | + "o4-mini": 200_000, |
| 104 | + "o1": 200_000, |
| 105 | + # Google Gemini |
| 106 | + "gemini-2.5-flash": 1_048_576, |
| 107 | + "gemini-2.5-flash-lite": 1_048_576, |
| 108 | + "gemini-2.5-pro": 1_048_576, |
| 109 | + "gemini-2.0-flash": 1_048_576, |
| 110 | + "gemini-2.0-flash-lite": 1_048_576, |
| 111 | + "gemini-3-pro-preview": 1_048_576, |
| 112 | + "gemini-3-flash-preview": 1_048_576, |
| 113 | + "gemini-3.1-pro-preview": 1_048_576, |
| 114 | + "gemini-3.1-flash-lite-preview": 1_048_576, |
| 115 | + # Mistral |
| 116 | + "mistral-large-latest": 262_144, |
| 117 | + "mistral-large-2512": 262_144, |
| 118 | + "mistral-large-3": 262_144, |
| 119 | + "mistral-medium-latest": 131_072, |
| 120 | + "mistral-medium-2505": 131_072, |
| 121 | + "mistral-small-latest": 131_072, |
| 122 | + "mistral-small-3-2-2506": 131_072, |
| 123 | +} |
| 124 | + |
| 125 | + |
| 126 | +def get_context_window_limit(model_id: str) -> int | None: |
| 127 | + """Look up the context window limit for a model ID. |
| 128 | +
|
| 129 | + For Bedrock cross-region model IDs (e.g. ``us.anthropic.claude-sonnet-4-6``), |
| 130 | + the region prefix is stripped as a fallback if the direct lookup fails. |
| 131 | +
|
| 132 | + Args: |
| 133 | + model_id: The model ID to look up. |
| 134 | +
|
| 135 | + Returns: |
| 136 | + The context window limit in tokens, or None if not found. |
| 137 | + """ |
| 138 | + direct = _CONTEXT_WINDOW_LIMITS.get(model_id) |
| 139 | + if direct is not None: |
| 140 | + return direct |
| 141 | + |
| 142 | + # Fallback: strip prefix before first dot and retry (handles cross-region prefixes) |
| 143 | + dot_index = model_id.find(".") |
| 144 | + if dot_index != -1: |
| 145 | + stripped = model_id[dot_index + 1 :] |
| 146 | + result = _CONTEXT_WINDOW_LIMITS.get(stripped) |
| 147 | + if result is not None: |
| 148 | + logger.debug( |
| 149 | + "model_id=<%s>, stripped_id=<%s> | resolved context window limit via prefix strip", model_id, stripped |
| 150 | + ) |
| 151 | + return result |
| 152 | + |
| 153 | + return None |
| 154 | + |
| 155 | + |
| 156 | +def resolve_config_metadata(config: _C, model_id: str) -> _C: |
| 157 | + """Resolve model metadata fields on a config dict from built-in lookup tables. |
| 158 | +
|
| 159 | + When ``context_window_limit`` is not explicitly set, looks it up from the built-in table. |
| 160 | + Explicit values pass through unchanged. Returns a new dict only when resolution adds a field; |
| 161 | + otherwise returns the original config to avoid unnecessary allocation. |
| 162 | +
|
| 163 | + Args: |
| 164 | + config: The stored model config dict. |
| 165 | + model_id: The model ID to look up. |
| 166 | +
|
| 167 | + Returns: |
| 168 | + The config with resolved metadata, or the original config if nothing to resolve. |
| 169 | + """ |
| 170 | + if "context_window_limit" in config: |
| 171 | + return config |
| 172 | + |
| 173 | + limit = get_context_window_limit(model_id) |
| 174 | + if limit is None: |
| 175 | + return config |
| 176 | + |
| 177 | + return {**config, "context_window_limit": limit} # type: ignore[return-value] |
0 commit comments