Add `get_model`, `resolve_model_id`, and `for_agent` capability hooks

Merged
DO
5mo ago
+1979-89

Motivation

Capabilities can contribute almost every part of an agent runtime configuration, but could not bind reusable capability instances to an agent or choose and construct models. That blocks dependency-aware tenant, region, credential, adaptive-routing, and durability use cases, including model-less agents whose model is supplied by a capability.

Related design work: #3335 and #4977.

Design

This adds three composable capability hooks:

  • for_agent(agent) binds a reusable capability to constructor or per-run agent configuration. Constructor model strings remain raw during binding so capabilities can define their own interpretation and credentials. Static run/spec capabilities bind once per run; CapabilityFunc results bind before their own for_run(), while specialized run-bound values returned by an ordinary for_run() are not rebound.
  • get_model() supplies a static Model/model ID, or a sync/async ModelSelectionContext callable evaluated once per new logical request step. SelectModel(selector) is the convenience capability for adaptive policies.
  • resolve_model_id(ctx, *, model_id) turns a selected string into a Model, with the ResolveModelId convenience capability for user callables.

ModelSelectionContext exposes the agent, run dependencies, lower-precedence model, run step, messages, and usage without pretending a full RunContext exists before the model has been selected. A model-less agent can select its first model using live dependencies during run setup.

Precedence is:

override(model=...) / explicit run model › run-spec model › capability get_model() › constructor model.

Later model contributions win. Model ID resolvers instead form a chain of responsibility, where the first non-None result in user order wins. Explicit models skip selection but still use capability resolution when supplied as strings.

Bootstrap IDs use the post-for_agent, pre-for_run resolver chain. If for_run() replaces the capability, step-one and later selections use the replacement resolver chain. Static selections are resolved and entered once per run. Dynamic selections happen before model-dependent settings, profile, tool, message, token-counting, instrumentation, and request preparation. Same-step provider continuation remains pinned; ambiguous cross-run continuation is rejected.

Run-spec capabilities participate in bootstrap. Deferred capabilities do not contribute model hooks, even after loading; post-load model selection is explicitly deferred pending demand and a clear continuation contract. Dynamic selection under durable execution is also deferred. #4977 can carry raw constructor IDs through its durable proxy and resolve them worker-side.

Fallback models compose with selection today. Replacing the FallbackModel wrapper with a capability in v3 is plausible, but requires moving per-model message preparation and actual-attempt tracking into the request handler first; the current wrapper hook runs after initial-model message preparation.

Tests and documentation

Coverage includes binding identity and replacement, raw constructor IDs, per-run and CapabilityFunc binding, precedence and ordering, dependency-aware resolution, first-step adaptive selection, async/per-step selection, streaming, model lifecycle cleanup, static pinning, for_run() replacement, run-spec bootstrap, continuation safety, fallback composition, non-run consumers, and the web UI handling of resolver-owned IDs.

The capabilities documentation and bundled Pydantic AI agent skill describe the lifecycle order, adaptive-selection helper, fallback composition, and current deferred/durable limitations.

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.
  • PR title is fit for the release changelog.