routerDynamic Models

This document describes the new dynamic model configuration system that replaces static enums with flexible, runtime-configurable model definitions.

🎯 Overview

The dynamic model system enables:

  • Runtime model discovery from external configuration sources

  • Automatic fallback to local configurations when external sources fail

  • Smart model resolution with fuzzy matching and aliases

  • Capability-based search to find models with specific features

  • Cost optimization by automatically selecting cheapest models for tasks

πŸ—οΈ Architecture

Components

  1. Model Configuration Server (scripts/modelServer.js)

    • Serves model configurations via REST API

    • Provides search and filtering capabilities

    • Can be hosted anywhere (GitHub, CDN, internal server)

  2. Dynamic Model Provider (src/lib/core/dynamicModels.ts)

    • Loads configurations from multiple sources with fallback

    • Caches configurations to reduce network requests

    • Validates configurations using Zod schemas

    • Provides intelligent model resolution

  3. Model Configuration (config/models.json)

    • JSON-based model definitions

    • Includes pricing, capabilities, and metadata

    • Supports aliases and provider defaults

πŸš€ Quick Start

1. Environment Setup

Before using the dynamic model system, ensure your provider configurations are set up correctly. See the Provider Configuration Guide for detailed instructions.

2. Start the Model Server

Server runs on http://localhost:3001 by default.

2. Test the System

3. Use in Code

πŸ“‘ API Endpoints

Model Server Endpoints

  • GET /health - Health check

  • GET /api/v1/models - Get all model configurations

  • GET /api/v1/models/:provider - Get models for specific provider

  • GET /api/v1/search?capability=X&maxPrice=Y - Search models by criteria

Example API Usage

πŸ”§ Configuration Schema

Model Configuration Structure

Key Fields

  • id: Provider-specific model identifier

  • displayName: Human-readable model name

  • capabilities: Array of model capabilities (functionCalling, vision, etc.)

  • deprecated: Whether the model is deprecated

  • pricing: Input/output token costs per 1K tokens

  • contextWindow: Maximum context window size

  • releaseDate: Model release date

πŸŽ›οΈ Advanced Usage

Configuration Sources

The system tries multiple sources in order:

  1. process.env.MODEL_CONFIG_URL - Custom URL override

  2. http://localhost:3001/api/v1/models - Local development server

  3. https://raw.githubusercontent.com/NeurosLink/docs/release/config/models.json - GitHub

  4. ./config/models.json - Local fallback

Model Resolution Logic

Capability Search Options

πŸ”„ Migration from Static Enums

Before (Static Enums)

After (Dynamic Resolution)

πŸ” Production Deployment

Environment Variables

Hosting Configuration

  1. GitHub Pages: Host models.json as static file

  2. CDN: Use CloudFlare/AWS CloudFront for global distribution

  3. Internal API: Integrate with existing infrastructure

  4. File System: Local configurations for air-gapped environments

Cache Strategy

  • 5-minute cache: Balances freshness with performance

  • Graceful degradation: Falls back to cached data on network failures

  • Manual refresh: dynamicModelProvider.refresh() for immediate updates

πŸ§ͺ Testing

The test suite verifies:

βœ… Model provider initialization βœ… Configuration loading from multiple sources βœ… Model resolution (exact, default, fuzzy, alias) βœ… Capability-based search βœ… Best model selection algorithms βœ… Error handling and fallbacks

Run tests with:

πŸš€ Benefits

  • πŸ”„ Future-Proof: New models automatically available

  • πŸ’° Cost-Optimized: Runtime selection based on pricing

  • πŸ›‘οΈ Reliable: Multiple fallback sources

  • ⚑ Fast: Cached configurations with smart invalidation

  • πŸ”’ Type-Safe: Zod schemas ensure runtime safety

  • πŸ”§ Backward Compatible: Existing code continues working

This system transforms static model definitions into a dynamic, self-updating platform that scales with the rapidly evolving AI landscape.

Last updated

Was this helpful?