Proposal: AdaptiveModel
WIP: submitted code in POC stage
Based on discussions from https://github.com/pydantic/pydantic-ai/issues/3023
AdaptiveModel is a new model type that provides full control over model selection at runtime. Unlike FallbackModel which tries models sequentially, AdaptiveModel allows custom logic to select the next model based on rich context including attempts, exceptions, and agent dependencies.
Core API
class AdaptiveModel[AgentDepsT](Model):
def __init__(
self,
models: Sequence[Model],
selector: Callable[[AdaptiveContext[AgentDepsT]], Model | None]
| Callable[[AdaptiveContext[AgentDepsT]], Awaitable[Model | None]],
*,
max_attempts: int | None = None,
):
"""
Args:
models: Pool of models to choose from
selector: Sync or async function that selects the next model to try
max_attempts: Maximum total attempts across all models (None = unlimited)
"""
Execution Flow
The AdaptiveModel automatically handles fallback and retry by:
- Calling
selector(context)to get the next model to try - Attempting the request with that model
- If the request succeeds, returning the result
- If the request fails:
- Recording the attempt (model + exception) in
context.attempts - Calling
selector(context)again with updated context - If selector returns a
Model, goto step 2 (retry/fallback) - If selector returns
None, raise exception group with all failures
- Recording the attempt (model + exception) in
- If
max_attemptsis reached, stop and raise exception group
The selector has full control over retry/fallback logic:
- Return the same model that failed → retry with same model
- Return a different model → fallback to another model
- Return
None→ stop trying - Inspect
ctx.attempts[-1].exceptionto make decisions - Use
time.sleep()orawait asyncio.sleep()for backoff/waiting (selector can be sync or async)
Context
@dataclass
class AdaptiveContext[AgentDepsT]:
"""Context provided to the selector function."""
run_context: RunContext[AgentDepsT] | None # Access to agent dependencies (None for non-streaming)
models: Sequence[Model] # Available models
attempts: list[AttemptResult] # History of attempts in this request
attempt_number: int # Current attempt number (1-indexed)
messages: list[ModelMessage] # The original request
model_settings: ModelSettings | None
model_request_parameters: ModelRequestParameters
@dataclass
class AttemptResult:
"""Record of a single attempt."""
model: Model
exception: Exception | None
timestamp: float
duration: float # seconds
Important Implementation Notes
Agent Dependencies Availability:
run_context(and thusctx.run_context.deps) is only available in streaming mode (run_stream)- For non-streaming requests (
run,run_sync),ctx.run_contextwill beNone - This is due to the base
Model.request()API not accepting arun_contextparameter
- For non-streaming requests (
Use Cases
1. Throttling with Timeout
Handle rate limiting by timing out models for 30 seconds after throttling errors.
import asyncio
import time
throttled_models = {} # model_id -> timestamp
async def throttle_aware_selector(ctx: AdaptiveContext) -> Model | None:
# Record throttling from last attempt
if ctx.attempts:
last = ctx.attempts[-1]
if last.exception and 'throttl' in str(last.exception).lower():
throttled_models[id(last.model)] = time.time()
# Find first available model
for model in ctx.models:
model_id = id(model)
if model_id in throttled_models:
if time.time() - throttled_models[model_id] < 30:
continue
del throttled_models[model_id]
return model
# All throttled - wait for soonest available
if throttled_models and ctx.attempt_number < 10:
soonest = min(throttled_models.items(), key=lambda x: x[1])
wait_time = 30 - (time.time() - soonest[1])
if wait_time > 0:
await asyncio.sleep(wait_time)
del throttled_models[soonest[0]]
return next(m for m in ctx.models if id(m) == soonest[0])
return None
adaptive = AdaptiveModel(
models=[primary, secondary, tertiary],
selector=throttle_aware_selector,
max_attempts=15
)
2. Load Balancing
Distribute requests evenly across multiple accounts/instances.
call_counts = {} # model_id -> count
def round_robin_selector(ctx: AdaptiveContext) -> Model | None:
if not ctx.attempts:
# Use least-used model
model = min(ctx.models, key=lambda m: call_counts.get(id(m), 0))
call_counts[id(model)] = call_counts.get(id(model), 0) + 1
return model
# On retry, try next least-used
failed = {id(a.model) for a in ctx.attempts}
available = [m for m in ctx.models if id(m) not in failed]
return min(available, key=lambda m: call_counts.get(id(m), 0)) if available else None
# Load balance across accounts
adaptive = AdaptiveModel(
models=[
OpenAIChatModel('gpt-4o', api_key=key1),
OpenAIChatModel('gpt-4o', api_key=key2),
],
selector=round_robin_selector
)
3. User Tier-Based Selection
Route to different models based on user subscription level.
@dataclass
class UserContext:
tier: str # 'free', 'pro', 'enterprise'
monthly_tokens_used: int
monthly_token_limit: int
def tier_based_selector(ctx: AdaptiveContext[UserContext]) -> Model | None:
user = ctx.run_context.deps
if not ctx.attempts:
if user.tier == 'enterprise':
return next((m for m in ctx.models if 'gpt-4o' in m.model_name), ctx.models[0])
elif user.tier == 'pro':
# Check usage limits
if user.monthly_tokens_used < user.monthly_token_limit * 0.9:
return next((m for m in ctx.models if 'gpt-4o-mini' in m.model_name), ctx.models[0])
return next((m for m in ctx.models if 'gpt-3.5' in m.model_name), ctx.models[0])
else:
return next((m for m in ctx.models if 'gpt-3.5' in m.model_name), ctx.models[0])
# Retry with next model
tried = {id(a.model) for a in ctx.attempts}
available = [m for m in ctx.models if id(m) not in tried]
return available[0] if available else None
adaptive = AdaptiveModel(
models=[gpt35, gpt4mini, gpt4],
selector=tier_based_selector
)
agent = Agent(adaptive, deps_type=UserContext)
result = agent.run_sync(
'Explain quantum computing',
deps=UserContext(tier='pro', monthly_tokens_used=80000, monthly_token_limit=100000)
)
4. Extended Context Model Upgrade
Automatically upgrade to a long-context model when conversation exceeds a threshold.
def context_aware_selector(ctx: AdaptiveContext) -> Model | None:
"""Upgrade to long-context model when conversation gets large."""
# Count messages in conversation
message_count = len(ctx.messages)
if not ctx.attempts:
# First attempt - choose based on context size
if message_count > 50:
# Use long-context model for large conversations
return next((m for m in ctx.models if 'claude-3-7-sonnet' in m.model_name), ctx.models[0])
else:
# Use standard model for normal conversations
return next((m for m in ctx.models if 'gpt-4o-mini' in m.model_name), ctx.models[0])
# On retry, try next available model
tried = {id(a.model) for a in ctx.attempts}
available = [m for m in ctx.models if id(m) not in tried]
return available[0] if available else None
# Models sorted by context length capability
adaptive = AdaptiveModel(
models=[
OpenAIChatModel('gpt-4o-mini'), # 128K context
OpenAIChatModel('gpt-4o'), # 128K context
AnthropicModel('claude-3-7-sonnet-latest'), # 1M context
],
selector=context_aware_selector
)
agent = Agent(adaptive)
# Short conversation uses cheap model
result1 = agent.run_sync('Hello') # Uses gpt-4o-mini
# Long conversation automatically upgrades
messages = result1.new_messages()
for i in range(60):
result = agent.run_sync(f'Message {i}', message_history=messages)
messages = result.new_messages()
# Automatically switched to claude-3-7-sonnet due to message count
5. Cost-Optimized with Quality Fallback
Start with cheap models, upgrade if quality is insufficient.
@dataclass
class QualityTracker:
quality_threshold: float = 0.8
model_history: dict[str, list[float]] = field(default_factory=dict) # model -> quality scores
def cost_quality_selector(ctx: AdaptiveContext[QualityTracker]) -> Model | None:
tracker = ctx.run_context.deps
if not ctx.attempts:
# Check if cheap model historically meets quality threshold
cheap_model = ctx.models[0]
avg_quality = (
sum(tracker.model_history.get(cheap_model.model_name, [1.0])) /
len(tracker.model_history.get(cheap_model.model_name, [1.0]))
)
if avg_quality >= tracker.quality_threshold:
return cheap_model
# Quality too low, start with better model
return ctx.models[1] if len(ctx.models) > 1 else cheap_model
# Upgrade on failure
tried = {id(a.model) for a in ctx.attempts}
available = [m for m in ctx.models if id(m) not in tried]
return available[0] if available else None
adaptive = AdaptiveModel(
models=[gpt35, gpt4mini, gpt4], # Sorted by cost
selector=cost_quality_selector
)
agent = Agent(adaptive, deps_type=QualityTracker)
6. Smart Retry with Exponential Backoff
Retry same model with backoff for transient errors, fallback for permanent errors.
import asyncio
async def exponential_backoff_selector(ctx: AdaptiveContext) -> Model | None:
if not ctx.attempts:
return ctx.models[0]
last = ctx.attempts[-1]
is_transient = (
last.exception and
hasattr(last.exception, 'status_code') and
500 <= last.exception.status_code < 600
)
if is_transient and ctx.attempt_number <= 5:
# Exponential backoff: 1s, 2s, 4s, 8s, 16s
await asyncio.sleep(2 ** (ctx.attempt_number - 2))
return last.model # Retry same model
# Try different model
tried = {id(a.model) for a in ctx.attempts}
available = [m for m in ctx.models if id(m) not in tried]
return available[0] if available else None
adaptive = AdaptiveModel(
models=[primary, backup],
selector=exponential_backoff_selector
)
7. Account Quota Management
Rotate across accounts based on remaining quota.
@dataclass
class AccountPool:
accounts: dict[str, dict] # account_id -> {api_key, quota_remaining, reset_time}
def get_available_account(self) -> str | None:
now = datetime.now()
for account_id, info in self.accounts.items():
if now >= info['reset_time']:
info['quota_remaining'] = info['quota_limit']
if info['quota_remaining'] > 0:
return account_id
return None
def quota_rotation_selector(ctx: AdaptiveContext[AccountPool]) -> Model | None:
pool = ctx.run_context.deps
tried = {id(a.model) for a in ctx.attempts}
account_id = pool.get_available_account()
if not account_id:
return None
# Find model for this account that hasn't been tried
return next(
(m for m in ctx.models
if m.api_key == pool.accounts[account_id]['api_key'] and id(m) not in tried),
None
)
adaptive = AdaptiveModel(
models=[
OpenAIChatModel('gpt-4o', api_key=key1),
OpenAIChatModel('gpt-4o', api_key=key2),
OpenAIChatModel('gpt-4o', api_key=key3),
],
selector=quota_rotation_selector
)
Key Benefits
- Full Control: Custom logic for model selection based on any criteria
- Stateful Logic: Maintain state across calls (throttle timers, usage counts, quality metrics)
- Smart Waiting: Wait for throttled models instead of giving up
- Load Distribution: Balance across identical models on different accounts
- User-Aware: Access agent dependencies for user-specific routing
- Cost Optimization: Dynamic model selection based on cost, quality, and usage
- Complex Retry: Implement exponential backoff, circuit breakers, etc.
Comparison with FallbackModel
| Feature | FallbackModel | AdaptiveModel |
|---|---|---|
| Model Selection | Sequential | Custom logic |
| State Management | External only | External + internal |
| Wait/Retry Logic | Not supported | Full control |
| Load Balancing | Not supported | Supported |
| User Context | Via callbacks | Direct access via RunContext |
| Complexity | Simple | Flexible |
AdaptiveModel complements FallbackModel by supporting sophisticated routing scenarios while FallbackModel remains the simpler choice for basic sequential fallback.