Ch5/B.4 — return the model's context-window size in tokens. Defaults to 200_000 (the Claude family default) when the provider doesn't expose a real integer ``context_window`` attribute. The ``isinstance(_, int)`` guard handles MagicMock test fixtures: a MagicMock returns another mo
(provider: BaseProvider)
| 247 | |
| 248 | |
| 249 | def _get_context_window(provider: BaseProvider) -> int: |
| 250 | """Ch5/B.4 — return the model's context-window size in tokens. |
| 251 | |
| 252 | Defaults to 200_000 (the Claude family default) when the provider |
| 253 | doesn't expose a real integer ``context_window`` attribute. |
| 254 | |
| 255 | The ``isinstance(_, int)`` guard handles MagicMock test fixtures: |
| 256 | a MagicMock returns another mock for unset attributes, so a naive |
| 257 | ``getattr(..., None) or default`` would short-circuit past the |
| 258 | default. Be explicit about the type so the loop is robust to test |
| 259 | doubles. |
| 260 | """ |
| 261 | cw = getattr(provider, "context_window", None) |
| 262 | if isinstance(cw, int) and cw > 0: |
| 263 | return cw |
| 264 | return 200_000 |
| 265 | |
| 266 | |
| 267 | def _is_withheld_max_output_tokens(msg: Message | None) -> bool: |