Add `context_window` to `ModelProfile` and `context_window_used` to `RunContext`

Merged
AD
3mo ago
+844-147

Adds context_window to ModelProfile and context_window_used to RunContext, so history processors and tools can tell how full the context window is and decide when to compact.

The window is filled from genai-prices data when no profile layer sets it, using the same URL-then-provider-name lookup as pricing. There is no provider-specific handling in pydantic-ai: coverage of models without a recorded window (e.g. via Bedrock) improves purely through genai-prices data updates (#465, #641, #643). For custom or local models, set it explicitly: profile={'context_window': 128_000}.

context_window_used is the fraction of the window occupied: the last response's total_tokens over the window. It returns None (never 0.0) when either side is unknown.

Every model exposes the window as model.context_window, read from its profile: wrappers forward it, a FallbackModel reports the smallest among its candidates (history that fits it fits whichever candidate answers), and realtime models get the same RealtimeModelProfile.context_window with the genai-prices fill. context_window_used reads that property, so it also works for fallback and realtime models.

Two things subclass authors may notice: Model.profile now reads model_name, system, and base_url while resolving (so a model must be fully constructed before its profile is read), and RealtimeModel gained a keyword-only constructor storing settings and profile that concrete realtime models call.

Checklist

  • Any AI generated code has been reviewed line-by-line by the human PR author, who stands by it.
  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
  • PR title is fit for the release changelog.