Inkdown
Start writing

Merlin Backend

12 files·0 subfolders

Shared Workspace

Merlin Backend
01-Orchestration.md

06-MultiProviderLLM

Shared from "Merlin Backend" on Inkdown

Multi-Provider LLM Architecture

Overview

The LLM provider system routes requests to multiple AI models through a unified abstraction layer called Rune. It handles model selection, token cost calculation, caching, and provider-specific transformations.


Architecture

Plain text
02-DeepResearch.md
03-Search.md
04-Scraping.md
05-Streaming.md
06-MultiProviderLLM.md
07-MemoryAndContext.md
08-ErrorHandling.md
09-RateLimiting.md
10-TaskQueue.md
11-SecurityAndAuth.md
Orchestration-2nd-draft

Core Provider: chat()

File: src/server/repositories/provider/provider.ts:24

TypeScript

Rune Configuration

File: src/server/constantsSchemasAndTypes/endpointConstants.ts

TypeScript

Rune acts as a unified gateway to multiple LLM providers.


Model Configuration

Models are defined with costs, prompts, and capabilities:

TypeScript

Cost Multipliers Applied:

OperationBase CostMultiplierFinal Cost
Standard chat1001×100
With web search1002×200
With data analysis1001× + 15115
RAG (Project + ProFinder)1001× + 250350
Multiple searches (3×)100(3+1)×400

Payload Transformation

File: src/server/repositories/provider/rune.ts

TypeScript

Why Transform:

  • Each provider has different API formats
  • Rune normalizes, but we still need provider-specific tweaks
  • Tools, function calling, system prompts vary

Usage Tracking

Token Counting:

TypeScript

Usage Config:

TypeScript

Query Function (Simplified)

File: src/server/repositories/provider/provider.ts:322

For simple queries without conversation context:

TypeScript

Use Case:

  • One-off queries
  • No conversation history
  • Faster initialization
  • Used by background tasks

Error Handling

File: src/server/repositories/provider/functions/helpers.ts

TypeScript

Error Types:

  • content_filter: Input/output blocked by safety filters
  • rate_limited: Too many requests to provider
  • context_length_exceeded: Input too long for model
  • server_error: Provider downtime

Caching Strategy

Prompt Caching:

TypeScript

Why Only Last Message:

  • System prompt and history rarely change
  • Last message (user query) varies
  • Anthropic/Claude support prompt caching
  • Reduces costs for long conversations

Integration with Orchestrator

TypeScript

Summary

The multi-provider LLM architecture:

  1. Rune Gateway: Unified API for multiple providers
  2. Cost Calculation: Base cost × multipliers based on tools used
  3. Model Selection: Config per model (cost, limits, prompts)
  4. Dual Modes: Streaming (real-time) and Normal (blocking)
  5. Function Calling: Structured output via gpt-4o-mini
  6. Prompt Caching: Only last message cached
  7. Token Tracking: Input, output, cached, reasoning
  8. Error Mapping: Provider errors → Client/Server errors

Key Principle: One unified interface, multiple providers, accurate cost tracking, seamless failover between models.