Architecture Overview
Two layers:- Runtime fetch - Live API calls cached for 1 hour
- Build-time snapshot - Static fallback when API unavailable
How It Works
1. Define the API Schema
Create a Zod schema for your model provider’s response:lib/ai/ai-gateway-models-schemas.ts
2. Create the Fallback Snapshot
Generate an initial empty file that will hold the snapshot:lib/ai/models.generated.ts
3. Implement the Fetcher with Fallback
lib/ai/models.ts
- All errors fall back to the snapshot (never throws)
- Zod validates the response before use
unstable_cachestores results for 1 hour- Cache tag enables manual invalidation via
revalidateTag()
4. Add Computed Fields (Optional)
The API returns capabilities as atags array (e.g., ["reasoning", "tool-use", "vision"]). You can transform these into boolean flags for easier filtering:
lib/ai/model-data.ts
tags array, skip this transformation and return AiGatewayModel[] directly from fetchModels().
5. Create the Sync Script
Write a script to refresh the fallback snapshot:scripts/fetch-models.ts
package.json
Caching Strategy
| Layer | Cache Key | TTL | Tag |
|---|---|---|---|
| API response + transform | ["ai-gateway-models"] | 1 hour | ai-gateway-models |
Key Files
| File | Purpose |
|---|---|
lib/ai/ai-gateway-models-schemas.ts | Zod schema for API response |
lib/ai/models.generated.ts | Static fallback snapshot |
lib/ai/models.ts | Runtime fetcher with cache |
lib/ai/model-data.ts | Type definitions and transforms |
scripts/fetch-models.ts | Build-time sync script |
Gotchas
- Dev vs Prod caching:
unstable_cachebehaves differently in development. Restart the dev server to see cache changes. - Next.js specific:
unstable_cacherequires Next.js. For other frameworks, use Redis or in-memory caching. - Snapshot size: The generated file can be large (40KB+). This is acceptable for build-time inclusion.
Related
- Multi-Model Support for user-facing model configuration