Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:ARCANGEL0 EVA Provider Request Dispatch

From Leeroopedia
Knowledge Sources
Domains LLM, API_Integration, Networking
Last Updated 2026-10-29 14:00 GMT

Overview

Abstraction layer for communicating with heterogeneous LLM provider APIs over HTTP, handling protocol differences, streaming, retry, and model capability discovery.

Description

Different LLM providers (OpenAI, Anthropic, OpenRouter, Ollama, etc.) expose different API protocols, authentication schemes, and response formats. A provider request dispatch layer abstracts these differences behind a unified interface, allowing the agent loop to call any provider without knowing the underlying protocol.

Key challenges this principle addresses:

  • Protocol divergence: OpenAI uses tool-call/tool message pairs; Anthropic uses tool_use/tool_result content blocks. The dispatcher must format requests and parse responses per protocol.
  • Streaming: Both protocols support SSE streaming for progressive token output. The dispatcher must parse stream events and forward them to a callback without blocking the main loop.
  • Model discovery: Each provider exposes model lists differently (pagination cursors, response shapes). The dispatcher normalizes these into a simple list of strings.
  • Capability detection: Not all models support the same features (reasoning parameters, image input). The dispatcher probes model metadata to set capability flags.
  • Retry: Transient failures (429 rate limits, 5xx server errors) should be retried with backoff; permanent failures should surface immediately.

Usage

Use this principle when:

  • Integrating with multiple LLM providers through a single codebase.
  • Supporting both OpenAI-compatible and Anthropic-native API protocols.
  • Needing streaming responses for real-time UI updates.
  • Discovering available models and their capabilities at runtime.

Theoretical Basis

The dispatcher follows the strategy pattern — selecting a protocol-specific handler based on the provider profile:

# Abstract algorithm (NOT real implementation)
def request_turn(profile, messages, tools, on_event):
    protocol = profile.get("protocol", "openai")
    if protocol == "anthropic":
        return anthropic_handler(base_url, model, key, messages, tools, on_event)
    return openai_handler(base_url, model, key, messages, tools, reasoning, on_event)

Streaming uses Server-Sent Events (SSE) parsed incrementally:

# Abstract SSE parsing (NOT real implementation)
for line in response:
    if line.startswith("data: "):
        chunk = json.loads(line[6:])
        if chunk["type"] == "content_block_delta":
            on_event("text", {"text": chunk["delta"]["text"]})
        elif chunk["type"] == "message_stop":
            break

Model list pagination handles three cursor styles: OpenAI `after`, Anthropic `after_id`, Cloudflare `page`.

Practical Guide

  • Hide API keys in error messages — replace them with `[hidden]` before raising exceptions.
  • Use stdlib urllib only — avoid external HTTP dependencies for portability.
  • Run provider requests in a background thread so the async event loop stays responsive to interrupts.
  • Implement cancellation via a token that closes the HTTP response on interrupt.
  • Probe reasoning support by checking `supported_parameters` or `capabilities` in model metadata.

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment