|
| 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 | + |
| 8 | +from typing import Any |
| 9 | + |
| 10 | +# Context window limits (in tokens) for known model IDs. |
| 11 | +# |
| 12 | +# Best-effort lookup table — unknown models return None and callers |
| 13 | +# fall back gracefully (e.g. proactive compression is disabled). |
| 14 | +# Users can always override with an explicit context_window_limit in their model config. |
| 15 | +# |
| 16 | +# For Bedrock models with cross-region prefixes (e.g. us., eu., global.), |
| 17 | +# get_context_window_limit strips the prefix before lookup so only the base model ID is needed here. |
| 18 | +_CONTEXT_WINDOW_LIMITS: dict[str, int] = { |
| 19 | + # Anthropic (direct API) |
| 20 | + "claude-sonnet-4-6": 1_000_000, |
| 21 | + "claude-sonnet-4-20250514": 1_000_000, |
| 22 | + "claude-sonnet-4-5": 200_000, |
| 23 | + "claude-sonnet-4-5-20250929": 200_000, |
| 24 | + "claude-opus-4-6": 1_000_000, |
| 25 | + "claude-opus-4-6-20260205": 1_000_000, |
| 26 | + "claude-opus-4-7": 1_000_000, |
| 27 | + "claude-opus-4-7-20260416": 1_000_000, |
| 28 | + "claude-opus-4-5": 200_000, |
| 29 | + "claude-opus-4-5-20251101": 200_000, |
| 30 | + "claude-opus-4-20250514": 200_000, |
| 31 | + "claude-opus-4-1": 200_000, |
| 32 | + "claude-opus-4-1-20250805": 200_000, |
| 33 | + "claude-haiku-4-5": 200_000, |
| 34 | + "claude-haiku-4-5-20251001": 200_000, |
| 35 | + "claude-3-7-sonnet-20250219": 200_000, |
| 36 | + "claude-3-5-sonnet-20241022": 200_000, |
| 37 | + "claude-3-5-sonnet-20240620": 200_000, |
| 38 | + "claude-3-5-haiku-20241022": 200_000, |
| 39 | + "claude-3-opus-20240229": 200_000, |
| 40 | + "claude-3-haiku-20240307": 200_000, |
| 41 | + # Bedrock Anthropic (base model IDs — cross-region prefixes stripped by get_context_window_limit) |
| 42 | + "anthropic.claude-sonnet-4-6": 1_000_000, |
| 43 | + "anthropic.claude-sonnet-4-20250514-v1:0": 1_000_000, |
| 44 | + "anthropic.claude-sonnet-4-5-20250929-v1:0": 200_000, |
| 45 | + "anthropic.claude-opus-4-6-v1": 1_000_000, |
| 46 | + "anthropic.claude-opus-4-7": 1_000_000, |
| 47 | + "anthropic.claude-opus-4-5-20251101-v1:0": 200_000, |
| 48 | + "anthropic.claude-opus-4-20250514-v1:0": 200_000, |
| 49 | + "anthropic.claude-opus-4-1-20250805-v1:0": 200_000, |
| 50 | + "anthropic.claude-haiku-4-5-20251001-v1:0": 200_000, |
| 51 | + "anthropic.claude-haiku-4-5@20251001": 200_000, |
| 52 | + "anthropic.claude-3-7-sonnet-20250219-v1:0": 200_000, |
| 53 | + "anthropic.claude-3-7-sonnet-20240620-v1:0": 200_000, |
| 54 | + "anthropic.claude-3-5-sonnet-20241022-v2:0": 200_000, |
| 55 | + "anthropic.claude-3-5-sonnet-20240620-v1:0": 200_000, |
| 56 | + "anthropic.claude-3-5-haiku-20241022-v1:0": 200_000, |
| 57 | + "anthropic.claude-3-opus-20240229-v1:0": 200_000, |
| 58 | + "anthropic.claude-3-haiku-20240307-v1:0": 200_000, |
| 59 | + "anthropic.claude-3-sonnet-20240229-v1:0": 200_000, |
| 60 | + "anthropic.claude-mythos-preview": 1_000_000, |
| 61 | + # Bedrock Amazon Nova |
| 62 | + "amazon.nova-pro-v1:0": 300_000, |
| 63 | + "amazon.nova-lite-v1:0": 300_000, |
| 64 | + "amazon.nova-micro-v1:0": 128_000, |
| 65 | + "amazon.nova-premier-v1:0": 1_000_000, |
| 66 | + "amazon.nova-2-lite-v1:0": 1_000_000, |
| 67 | + "amazon.nova-2-pro-preview-20251202-v1:0": 1_000_000, |
| 68 | + # OpenAI |
| 69 | + "gpt-5.5": 1_050_000, |
| 70 | + "gpt-5.5-pro": 1_050_000, |
| 71 | + "gpt-5.4": 1_050_000, |
| 72 | + "gpt-5.4-pro": 1_050_000, |
| 73 | + "gpt-5.4-mini": 272_000, |
| 74 | + "gpt-5.4-nano": 272_000, |
| 75 | + "gpt-5.2": 272_000, |
| 76 | + "gpt-5.2-pro": 272_000, |
| 77 | + "gpt-5.1": 272_000, |
| 78 | + "gpt-5": 272_000, |
| 79 | + "gpt-5-mini": 272_000, |
| 80 | + "gpt-5-nano": 272_000, |
| 81 | + "gpt-5-pro": 128_000, |
| 82 | + "gpt-4.1": 1_047_576, |
| 83 | + "gpt-4.1-mini": 1_047_576, |
| 84 | + "gpt-4.1-nano": 1_047_576, |
| 85 | + "gpt-4o": 128_000, |
| 86 | + "gpt-4o-mini": 128_000, |
| 87 | + "gpt-4-turbo": 128_000, |
| 88 | + "o3": 200_000, |
| 89 | + "o3-mini": 200_000, |
| 90 | + "o3-pro": 200_000, |
| 91 | + "o4-mini": 200_000, |
| 92 | + "o1": 200_000, |
| 93 | + # Google Gemini |
| 94 | + "gemini-2.5-flash": 1_048_576, |
| 95 | + "gemini-2.5-flash-lite": 1_048_576, |
| 96 | + "gemini-2.5-pro": 1_048_576, |
| 97 | + "gemini-2.0-flash": 1_048_576, |
| 98 | + "gemini-2.0-flash-lite": 1_048_576, |
| 99 | + "gemini-3-pro-preview": 1_048_576, |
| 100 | + "gemini-3-flash-preview": 1_048_576, |
| 101 | + "gemini-3.1-pro-preview": 1_048_576, |
| 102 | + "gemini-3.1-flash-lite-preview": 1_048_576, |
| 103 | +} |
| 104 | + |
| 105 | +# Known Bedrock cross-region routing prefixes. |
| 106 | +# See: https://docs.aws.amazon.com/bedrock/latest/userguide/cross-region-inference.html |
| 107 | +_BEDROCK_REGION_PREFIXES = frozenset({"us", "eu", "ap", "global", "apac", "au", "jp", "us-gov"}) |
| 108 | + |
| 109 | + |
| 110 | +def get_context_window_limit(model_id: str) -> int | None: |
| 111 | + """Look up the context window limit for a model ID. |
| 112 | +
|
| 113 | + For Bedrock cross-region model IDs (e.g. ``us.anthropic.claude-sonnet-4-6``), |
| 114 | + the region prefix is stripped before lookup. |
| 115 | +
|
| 116 | + Args: |
| 117 | + model_id: The model ID to look up. |
| 118 | +
|
| 119 | + Returns: |
| 120 | + The context window limit in tokens, or None if not found. |
| 121 | + """ |
| 122 | + direct = _CONTEXT_WINDOW_LIMITS.get(model_id) |
| 123 | + if direct is not None: |
| 124 | + return direct |
| 125 | + |
| 126 | + # Strip known Bedrock cross-region prefixes |
| 127 | + dot_index = model_id.find(".") |
| 128 | + if dot_index != -1: |
| 129 | + prefix = model_id[:dot_index] |
| 130 | + if prefix in _BEDROCK_REGION_PREFIXES: |
| 131 | + return _CONTEXT_WINDOW_LIMITS.get(model_id[dot_index + 1 :]) |
| 132 | + |
| 133 | + return None |
| 134 | + |
| 135 | + |
| 136 | +def resolve_config_metadata(config: dict[str, Any], model_id: str) -> dict[str, Any]: |
| 137 | + """Resolve model metadata fields on a config dict from built-in lookup tables. |
| 138 | +
|
| 139 | + When ``context_window_limit`` is not explicitly set, looks it up from the built-in table. |
| 140 | + Explicit values pass through unchanged. Returns a new dict only when resolution adds a field; |
| 141 | + otherwise returns the original config to avoid unnecessary allocation. |
| 142 | +
|
| 143 | + Args: |
| 144 | + config: The stored model config dict. |
| 145 | + model_id: The model ID to look up. |
| 146 | +
|
| 147 | + Returns: |
| 148 | + The config with resolved metadata, or the original config if nothing to resolve. |
| 149 | + """ |
| 150 | + if config.get("context_window_limit") is not None: |
| 151 | + return config |
| 152 | + |
| 153 | + limit = get_context_window_limit(model_id) |
| 154 | + if limit is None: |
| 155 | + return config |
| 156 | + |
| 157 | + return {**config, "context_window_limit": limit} |
0 commit comments