Inkdown
Start writing

Study

70 filesยท12 subfolders

Shared Workspace

Study
AI eng

06-MultiProviderLLM

Shared from "Study" 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
basic-ques
core
Revision w/ Whiteboard
CN Basics - 1
CN Basics - 2
DNS
Event loop
programming-language-concepts.md
zero-language-explanation.md
DB
Quick
databases-deep-dive.md
01-introduction.md
02-relational-databases.md
03-database-design.md
04-indexing.md
05-transactions-acid.md
06-nosql-databases.md
07-query-optimization.md
08-replication-ha.md
09-sharding-partitioning.md
10-caching-strategies.md
11-cap-theorem.md
12-connection-pooling.md
13-backup-recovery.md
14-monitoring.md
15-database-selection.md
README.md
JS
core topics
Event loop
Merlin Backend
01-Orchestration.md
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
Mobile
Build Alternative
Bundling
metro-bundler-deep-dive.md
OpenAI Agents Python
00_OVERVIEW.md
01_AGENT_SYSTEM.md
02_RUNNER_SYSTEM.md
03_TOOL_SYSTEM.md
04_ITEMS_SYSTEM.md
05_GUARDRAILS.md
06_HANDOFFS.md
07_MEMORY_SESSIONS.md
08_MODEL_PROVIDERS.md
09_SANDBOX_SYSTEM.md
10_TRACING.md
11_RUN_STATE.md
12_CONTEXT.md
13_LIFECYCLE_HOOKS.md
14_CONFIGURATION.md
15_ERROR_HANDLING.md
16_STREAMING.md
17_EXTENSIONS.md
18_MCP_INTEGRATION.md
19_BEST_PRACTICES.md
20_ARCHITECTURE_PATTERNS.md
opencode-study
context-handling
core
Python
Alembic
Basics
sqlalchemy - fastapi
SQLAlchemy overview
tweets
system_design_for_agentic_apps.md
Agent Loop

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.