Skip to main content

Why a Model Layer?

Switching between OpenAI, Anthropic, or local models shouldn’t require rewriting your agent. The model layer gives you:
  • One API for all providers
  • Automatic fallback when a provider fails
  • Streaming with consistent callbacks
  • Type-safe parameters per model type
Provider is configured, not coded. Set OPENAI_API_KEY or ANTHROPIC_API_KEY - your code doesn’t change.

Model System

The model management system provides a unified interface for AI model access with automatic provider selection, priority-based routing, and type-safe parameters.

Model Types

Core Model Types

Model Parameters

Type-safe parameters for each model type:

Model Registration

Registering Model Handlers

Model Handler Interface

Using Models

Type-Safe Model Usage

Specifying Provider

Provider Priority

Priority System

The runtime selects providers based on priority:

Fallback Mechanism

Model Providers

OpenAI Provider

Anthropic Provider

Local Model Provider (Ollama)

Model Selection Strategy

Automatic Selection

Context-Based Selection

Cost Optimization

Model Caching

Response Caching

Model Monitoring

Usage Tracking

Error Handling

Retry Logic

Graceful Degradation

Best Practices

Model Selection

  • Right-Size Models: Use appropriate model size for task complexity
  • Cost Awareness: Consider cost when selecting providers
  • Latency Requirements: Use local models for low-latency needs
  • Fallback Strategy: Implement fallbacks for reliability
  • Caching: Cache responses for repeated queries

Performance

  • Batch Processing: Batch multiple requests when possible
  • Streaming: Use streaming for long responses
  • Timeout Handling: Set appropriate timeouts
  • Connection Pooling: Reuse HTTP connections
  • Rate Limiting: Respect provider rate limits

Monitoring

  • Track Usage: Monitor token usage and costs
  • Error Rates: Track provider error rates
  • Latency Metrics: Monitor response times
  • Quality Metrics: Track response quality
  • Cost Analysis: Analyze cost per request

See Also

Services

Build services that provide models

Messaging

Stream model responses in real-time

Sessions API

Use models in conversations

Providers

Supply context to models