@@ -217,6 +217,10 @@ def __init__(
217217 # the user asked to stop. Cleared at the start of each chat()
218218 # so a fresh turn may retry normally.
219219 self ._aborted = False
220+ # Per-model context-window override: set by the session when
221+ # switching to a model profile that carries its own
222+ # context_window. None means "resolve from config / table".
223+ self ._context_window : int | None = None
220224 # an explicit log file is inherited by clones so every request
221225 # of one session (main + all sub-agents) lands in a single log
222226 self .log_path = (
@@ -229,18 +233,21 @@ def __init__(
229233 def context_window (self ) -> int :
230234 """Get the context window for this model.
231235
232- Resolution order: config-file ``context_windows`` overrides
233- (via ``config.get_context_window_for_model``) -> CONTEXT_WINDOWS
234- pattern match -> DEFAULT_CONTEXT_WINDOW. Resolved on every
235- access (no caching), so a runtime model switch or config-file
236- edit takes effect immediately; a malformed config falls back to
237- the default for that access and recovers once the file is fixed.
236+ Resolution order: explicit ``_context_window`` override (set by
237+ the session on model switch) → ``llm.context_window`` from the
238+ config file → built-in CONTEXT_WINDOWS table (substring match)
239+ → DEFAULT_CONTEXT_WINDOW. Resolved on every access (no
240+ caching), so a runtime model switch or config-file edit takes
241+ effect immediately; a malformed config falls back to the default
242+ for that access and recovers once the file is fixed.
238243 """
244+ if self ._context_window is not None :
245+ return self ._context_window
239246 try :
240247 return config .get_context_window_for_model (self .model , config_path = self ._config_path )
241248 except Exception :
242- # a malformed context_windows section must not break the
243- # loop: use the safe default, retry on the next access
249+ # a malformed config must not break the loop: use the safe
250+ # default, retry on the next access
244251 return config .DEFAULT_CONTEXT_WINDOW
245252
246253 def close (self ) -> None :
@@ -259,7 +266,7 @@ def clone(self) -> Client:
259266 flag strictly per-request. The log file is shared so one
260267 session's LLM interactions stay in one log.
261268 """
262- return Client (
269+ c = Client (
263270 base_url = self .base_url ,
264271 api_key = self .api_key ,
265272 model = self .model ,
@@ -271,6 +278,8 @@ def clone(self) -> Client:
271278 config_path = self ._config_path ,
272279 log_path = self .log_path ,
273280 )
281+ c ._context_window = self ._context_window
282+ return c
274283
275284 def abort (self ) -> None :
276285 """Abort the in-flight request (called on cancel).
0 commit comments