Swap the model or per-call settings from runtime.context.
Reads two optional keys from the runtime context dict:
'model' — a provider:model spec (e.g. "openai:gpt-5").
When present and different from the current model, the request is
re-routed to the new model.'model_params' — a dict of extra model settings (e.g.
{"temperature": 0}) that are shallow-merged into the
request's model_settings.This middleware is typically the outermost layer so it intercepts every
model call before provider-specific middleware (like
AnthropicPromptCachingMiddleware) runs.
Whether completed calls should write private resume metadata. Subagent instances disable this because they do not own the parent thread's resume state.
Whether to inject a per-thread OpenAI
prompt_cache_key. Left as None (the default) it is resolved
once here from models.openai_prompt_cache_key and cached, so no
per-call read happens. The one-time config.toml read assumes
current callers construct the middleware off the
blockbuster-guarded server loop (the server path offloads
create_cli_agent via asyncio.to_thread); if that assumption
is ever broken the read would trip BlockingError, which
_resolve_openai_prompt_cache_key_enabled re-raises rather than
masks. Pass an explicit bool to bypass the config read (mainly
for tests).
Apply runtime overrides and delegate to the next handler.
Apply runtime overrides and delegate to the next async handler.